aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/util.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/leap_cli/util.rb')
-rw-r--r--lib/leap_cli/util.rb171
1 files changed, 171 insertions, 0 deletions
diff --git a/lib/leap_cli/util.rb b/lib/leap_cli/util.rb
new file mode 100644
index 0000000..67fca8d
--- /dev/null
+++ b/lib/leap_cli/util.rb
@@ -0,0 +1,171 @@
+require 'md5'
+
+module LeapCli
+ module Util
+ extend self
+
+ ##
+ ## QUITTING
+ ##
+
+ #
+ # quit and print help
+ #
+ def help!(message=nil)
+ ENV['GLI_DEBUG'] = "false"
+ help_now!(message)
+ #say("ERROR: " + message)
+ end
+
+ #
+ # quit with a message that we are bailing out.
+ #
+ def bail!(message="")
+ say(message)
+ say("Bailing out.")
+ raise SystemExit.new
+ #ENV['GLI_DEBUG'] = "false"
+ #exit_now!(message)
+ end
+
+ #
+ # quit with no message
+ #
+ def quit!(message='')
+ say(message)
+ raise SystemExit.new
+ end
+
+ #
+ # bails out with message if assertion is false.
+ #
+ def assert!(boolean, message)
+ bail!(message) unless boolean
+ end
+
+ #
+ # assert that the command is available
+ #
+ def assert_bin!(cmd_name)
+ assert! `which #{cmd_name}`.strip.any?, "Sorry, bailing out, the command '%s' is not installed." % cmd_name
+ end
+
+ ##
+ ## FILES AND DIRECTORIES
+ ##
+
+ def relative_path(path)
+ path.sub(/^#{Regexp.escape(Path.provider)}\//,'')
+ end
+
+ def progress_created(path)
+ progress 'created %s' % relative_path(path)
+ end
+
+ def progress_updated(path)
+ progress 'updated %s' % relative_path(path)
+ end
+
+ def progress_nochange(path)
+ progress 'no change %s' % relative_path(path)
+ end
+
+ def progress_removed(path)
+ progress 'removed %s' % relative_path(path)
+ end
+
+ #
+ # creates a directory if it doesn't already exist
+ #
+ def ensure_dir(dir)
+ unless File.directory?(dir)
+ if File.exists?(dir)
+ bail! 'Unable to create directory "%s", file already exists.' % dir
+ else
+ FileUtils.mkdir_p(dir)
+ unless dir =~ /\/$/
+ dir = dir + '/'
+ end
+ progress_created dir
+ end
+ end
+ end
+
+ NAMED_PATHS = {
+ :user_ssh => 'users/#{arg}/#{arg}_ssh.pub',
+ :user_pgp => 'users/#{arg}/#{arg}_pgp.pub'
+ }
+
+ #
+ # read a file, exit if the file doesn't exist.
+ #
+ def read_file!(file_path)
+ if !File.exists?(file_path)
+ bail!("File '%s' does not exist." % file_path)
+ else
+ File.readfile(file_path)
+ end
+ end
+
+ def write_file!(*args)
+ if args.first.is_a? Symbol
+ write_named_file!(*args)
+ else
+ write_to_path!(*args)
+ end
+ end
+
+ def remove_file!(file_path)
+ if File.exists?(file_path)
+ File.unlink(file_path)
+ progress_removed(file_path)
+ end
+ end
+
+ #
+ # saves a named file
+ #
+ def write_named_file!(name, arg, contents)
+ assert!(NAMED_PATHS[name], "Error, I don't know the path for #{arg}")
+
+ filename = eval('"' + NAMED_PATHS[name] + '"')
+ fullpath = Path.provider + '/' + filename
+
+ write_to_path!(fullpath, contents)
+ end
+
+ def write_to_path!(filepath, contents)
+ ensure_dir File.dirname(filepath)
+ existed = File.exists?(filepath)
+ if existed
+ if file_content_is?(filepath, contents)
+ progress_nochange filepath
+ return
+ end
+ end
+
+ File.open(filepath, 'w') do |f|
+ f.write contents
+ end
+
+ if existed
+ progress_updated filepath
+ else
+ progress_created filepath
+ end
+ end
+
+ private
+
+ def file_content_is?(filepath, contents)
+ output = `md5sum '#{filepath}'`.strip
+ if $?.to_i == 0
+ return output.split(" ").first == MD5.md5(contents).to_s
+ else
+ return false
+ end
+ end
+
+ end
+end
+