1. set default option -h for tool
[why]
the tool will show help message if no option specified
the tool will get no email address without both '-e' and '-f'
[how]
add '-h' as default option if no option specified
options will be reset to '-h' if both '-e' and '-f' are not specified
2. fix undefined method 'empty'
[why]
the original value is defined nil for my_email
when execute answerback-email with no -e or -f, it will throw error as:
raceback (most recent call last):
2: from ./answerback-email.rb:213:in `<main>'
1: from ./answerback-email.rb:184:in `send_account'
./answerback-email.rb:155:in `check_my_email': undefined method `empty?' for nil:NilClass (NoMethodError)
[how]
use '.nil?' instead of '.empty?'
3. option parse error
[why]
if add no required value for option, it will use the field that follow as the value, example:
./answerback-email.rb -e -n name
Not a standard format email: -n.
[how]
add value check for options that need a value
Signed-off-by: Luan Shengde <shdluan(a)163.com>
---
container/assign-account/answerback-email.rb | 51 +++++++++++++++++---
1 file changed, 45 insertions(+), 6 deletions(-)
diff --git a/container/assign-account/answerback-email.rb b/container/assign-account/answerback-email.rb
index c6bfdd3..ec1f5b7 100755
--- a/container/assign-account/answerback-email.rb
+++ b/container/assign-account/answerback-email.rb
@@ -80,7 +80,7 @@ end
options = OptionParser.new do |opts|
opts.banner = 'Usage: answerback-mail.rb [-e|--email email] [-n|--name name] '
- opts.banner += "[-s|--ssh-pubkey pub_key_file] [-g|--gen-sshkey] [--login y|n] [--update]\n"
+ opts.banner += "[-s|--ssh-pubkey pub_key_file] [-g|--gen-sshkey] [-l|--login y|n] [-u|--update]\n"
opts.banner += ' answerback-mail.rb [-f|--raw-email email_file] '
opts.banner += "[-g|--gen-sshkey] [--login y|n] [--update]\n"
opts.banner += " -e|-f is required when applying account or updating account\n"
@@ -94,19 +94,45 @@ options = OptionParser.new do |opts|
opts.separator 'options:'
opts.on('-e email_address', '--email email_address', 'appoint email address') do |email_address|
+ unless email_address =~ /[^@]+@[\d\w]+\.[\w\d]+/
+ message = "Not a standard format email: #{email_address}.\n\n"
+ puts message
+
+ return false
+ end
+
my_info['my_email'] = email_address
end
opts.on('-n name', '--name name', 'appoint name') do |name|
+ unless name =~ /^\w[\w\d ]+/
+ message = "Name should only contains letters, digits and spaces\n\n"
+ puts message
+
+ return false
+ end
+
stdin_info['my_name'] = name
end
opts.on('-s pub_key_file', '--ssh-pubkey pub_key_file', \
'ssh pub_key file, enable password-less login') do |pub_key_file|
+ unless File.exist? pub_key_file
+ message = "File not found: #{pub_key_file}.\n\n"
+ puts message
+
+ return false
+ end
stdin_info['new_ssh_pubkey'] = File.read(pub_key_file).strip
end
opts.on('-f email_file', '--raw-email email_file', 'email file') do |email_file|
+ unless File.exist? email_file
+ message = "Email file not found: #{email_file}.\n\n"
+ puts message
+
+ return false
+ end
mail_content = Mail.read(email_file)
init_info(mail_content, email_info, my_info)
end
@@ -121,12 +147,16 @@ options = OptionParser.new do |opts|
end
opts.on('-l value', '--login value', 'enable/disable login, value: y|n') do |value|
- if value.downcase == 'y'
+ case value
+ when 'y', 'Y'
conf_info['enable_login'] = true
- elsif value.downcase == 'n'
+ when 'n', 'N'
conf_info['enable_login'] = false
else
- raise 'invalid parameter, please use y|n'
+ message = "-l: bad value #{value}, please use y|n\n\n"
+ puts message
+
+ return false
end
end
@@ -136,6 +166,15 @@ options = OptionParser.new do |opts|
end
end
+# if no option specified, set default option '-h'
+# if both '-e' and '-f' are not specified, will clear the ARGV,
+# and use '-h' instead
+if ARGV.empty?
+ ARGV << '-h'
+elsif (['-e', '-f'] - ARGV).eql? ['-e', '-f']
+ ARGV.clear
+ ARGV << '-h'
+end
options.parse!(ARGV)
def apply_account(my_info, conf_info)
@@ -152,7 +191,7 @@ def check_my_email(my_info)
message += "use -e to add an email address for applying account\n"
message += 'or use -f to add an email file'
- raise message if my_info['my_email'].empty?
+ raise message if my_info['my_email'].nil?
end
def build_my_info_from_input(my_info, email_info, my_info_es, stdin_info)
@@ -210,4 +249,4 @@ def store_account_info(my_info)
es.put_source_by_id(my_info['my_email'], my_info)
end
-send_account(my_info, conf_info, email_info, my_info_es, stdin_info)
+# send_account(my_info, conf_info, email_info, my_info_es, stdin_info)
--
2.23.0