diff options
author | elijah <elijah@riseup.net> | 2012-12-08 20:02:27 -0800 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2012-12-08 20:02:27 -0800 |
commit | 8572dfd59c21d2032b030adc9dc9a973c6e1c3f5 (patch) | |
tree | 3d3fb221339ddf4fa0037cbd88133431307f2e4e /lib/leap_cli/util.rb | |
parent | 155d744f6c6f94709925f0674f510b3064b6608e (diff) |
added commands 'node add' 'node rm' and 'node mv'
Diffstat (limited to 'lib/leap_cli/util.rb')
-rw-r--r-- | lib/leap_cli/util.rb | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/lib/leap_cli/util.rb b/lib/leap_cli/util.rb index 98c3002..d12c5a6 100644 --- a/lib/leap_cli/util.rb +++ b/lib/leap_cli/util.rb @@ -28,9 +28,9 @@ module LeapCli LeapCli.log_level = 3 yield elsif message - puts message + log 0, message end - log :bail, "" + log 0, :bail, "" raise SystemExit.new end @@ -105,11 +105,11 @@ module LeapCli def assert_config!(conf_path) value = nil - begin + #begin value = manager.instance_eval(conf_path) - rescue NoMethodError - rescue NameError - end + #rescue NoMethodError + #rescue NameError + #end assert! !value.nil? && value != "REQUIRED" do log :missing, "required configuration value for #{conf_path}" end @@ -199,19 +199,39 @@ module LeapCli def remove_file!(filepath) filepath = Path.named_path(filepath) if File.exists?(filepath) - File.unlink(filepath) - log :removed, filepath + if File.directory?(filepath) + remove_directory!(filepath) + else + begin + File.unlink(filepath) + log :removed, filepath + rescue Exception => exc + bail! do + log :failed, "to remove file #{filepath}" + log "error message: " + exc.to_s + end + end + end end end def remove_directory!(filepath) filepath = Path.named_path(filepath) if filepath !~ /^#{Regexp.escape(Path.provider)}/ || filepath =~ /\.\./ - raise "sanity check on rm -r did not pass for #{filepath}" + bail! "sanity check on rm -r did not pass for #{filepath}" end if File.directory?(filepath) - FileUtils.rm_r(filepath) - log :removed, filepath + begin + FileUtils.rm_r(filepath) + log :removed, filepath + rescue Exception => exc + bail! do + log :failed, "to remove directory #{filepath}" + log "error message: " + exc.to_s + end + end + else + log :failed, "to remove '#{filepath}', it is not a directory" end end @@ -237,6 +257,21 @@ module LeapCli end end + def rename_file!(oldpath, newpath) + oldpath = Path.named_path(oldpath) + newpath = Path.named_path(newpath) + if File.exists? newpath + log :skipping, "#{Path.relative_path(newpath)}, file already exists" + return + end + if !File.exists? oldpath + log :skipping, "#{Path.relative_path(oldpath)}, file is missing" + return + end + FileUtils.mv oldpath, newpath + log :moved, "#{Path.relative_path(oldpath)} to #{Path.relative_path(newpath)}" + end + def cmd_exists?(cmd) `which #{cmd}`.strip.chars.any? end |