optimize options: add option -t: generate new my_token add option -s: search my_info for the email add option -h: output help message
enable user search my_info for the email enable reset my_token for my_info
Signed-off-by: Luan Shengde shdluan@163.com --- sbin/build-my-info.rb | 56 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-)
diff --git a/sbin/build-my-info.rb b/sbin/build-my-info.rb index fd75f51..1b99408 100755 --- a/sbin/build-my-info.rb +++ b/sbin/build-my-info.rb @@ -8,26 +8,66 @@ require_relative '../lib/build_my_info_client' require 'optparse'
option = { - my_name: `git config --global user.name`.chomp, - my_email: `git config --global user.email`.chomp, - lab: `awk '/^lab:\s/ {print $2; exit}' /etc/compass-ci/defaults/*.yaml`.chomp + 'my_email' => `git config --global user.email`.chomp, + 'my_name' => `git config --global user.name`.chomp, + 'lab' => `awk '/^lab:\s/ {print $2; exit}' /etc/compass-ci/defaults/*.yaml`.chomp }
+es_search = false + options = OptionParser.new do |opts| + opts.banner = "Usage: build-my-info [-e email] [-n name] [-l lab] [-t]\n" + opts.banner += " build-my-info [-e email] -s\n" + + opts.separator '' + opts.separator 'options:' + opts.on('-e email', 'my_email') do |email| - option[:my_email] = email + option['my_email'] = email end
opts.on('-n name', 'my_name') do |name| - option[:my_name] = name + option['my_name'] = name end
opts.on('-l lab', 'lab') do |lab| - option[:lab] = lab + option['lab'] = lab + end + + opts.on('-t', 'my_token') do + option['my_token'] = %x(uuidgen).chomp + end + + opts.on('-s', 'search info for the email address') do + es_search = true + end + + opts.on_tail('-h', 'show this message') do + puts opts + exit end end
+ARGV << '-h' if ARGV.empty? + options.parse!
-build_my_info = BuildMyInfo.new(option[:my_email], option[:my_name], option[:lab]) -build_my_info.config_my_info +build_my_info = BuildMyInfo.new(option['my_email'], option['my_name'], option['lab'], option['my_token']) + +if es_search + my_info = build_my_info.search_my_info + if my_info.empty? + puts "No info for #{option['my_email']} yet.\n" + else + formatter = "%-15s %-15s\n" + my_info.each do |k, v| + printf formatter, "#{k}:", v + end + end +else + begin + build_my_info.config_my_info + rescue StandardError => e + puts e.backtrace + end +end