Skip to content

Commit

Permalink
Use guard clauses instead of large conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
dalen committed Feb 17, 2016
1 parent ad15228 commit 06924fb
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions lib/puppet/face/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,30 @@
(x[:path] || x.title) <=> (y[:path] || y.title)
end.each do |file|
filepath = (file[:path] || file.title)
next unless filepath.start_with? path
rel_path = filepath[path.length + 1 .. - 1]
if not options[:recursive]
next if rel_path.nil?
next if rel_path.split(File::SEPARATOR).length > 1
end
if filepath.start_with? path
description = nil
case file[:ensure]
when 'directory'
color = :blue
description = "content from #{file[:source]}" unless file[:source].nil?
when 'link'
color = :cyan
description = "link target: #{file[:target]}"
when 'absent'
color = :red
description = 'GETS REMOVED'
else
color = :reset
source = file[:source]
source = 'a "content" parameter' if file[:content]
description = "content from #{source}" unless source.nil?
end
next if rel_path.split(File::SEPARATOR).length > 1 && !options[:recursive]

This comment has been minimized.

Copy link
@fe80

fe80 May 27, 2016

We have a problem whith this commit with this path :
puppet ls sites-enabled/
Error: undefined local variable or method `rel_path' for #Puppet::Interface:0x00000001ad90c0
Error: Try 'puppet help ls list' for usage

I fix this issue with :
next if rel_path.nil? || rel_path.split(File::SEPARATOR).length > 1 && !options[:recursive]

If you have a best fix..


puts "#{colorize(color, rel_path)}\n declared in #{file.file}:#{file.line}\n"
puts " #{description}" if description
description = nil
case file[:ensure]
when 'directory'
color = :blue
description = "content from #{file[:source]}" unless file[:source].nil?
when 'link'
color = :cyan
description = "link target: #{file[:target]}"
when 'absent'
color = :red
description = 'GETS REMOVED'
else
color = :reset
source = file[:source]
source = 'a "content" parameter' if file[:content]
description = "content from #{source}" unless source.nil?
end

puts "#{colorize(color, rel_path)}\n declared in #{file.file}:#{file.line}\n"
puts " #{description}" if description
end
nil
end
Expand Down

0 comments on commit 06924fb

Please sign in to comment.