124 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			124 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| require "devops-client/handler/handler"
 | |
| require "devops-client/options/user_options"
 | |
| require "devops-client/output/user"
 | |
| 
 | |
| class User < Handler
 | |
| 
 | |
|   output_with Output::User
 | |
| 
 | |
|   def initialize(host, def_options={})
 | |
|     @host, @options = host, def_options
 | |
|     @options_parser = UserOptions.new(ARGV, def_options)
 | |
|   end
 | |
| 
 | |
|   def handle
 | |
|     current_command = ARGV[1].to_sym
 | |
|     @options, @args = @options_parser.parse_options_for!(current_command)
 | |
|     case current_command
 | |
|     when :list
 | |
|       list_handler
 | |
|       output
 | |
|     when :create
 | |
|       create_handler
 | |
|     when :delete
 | |
|       delete_handler
 | |
|     when :grant
 | |
|       grant_handler
 | |
|     when :password
 | |
|       password_handler
 | |
|     when :email
 | |
|       email_handler
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def list_handler
 | |
|     @list = get("/users")
 | |
|   end
 | |
| 
 | |
|   def create_handler
 | |
|     r = inspect_parameters @options_parser.create_params, @args[2], @args[3]
 | |
|     unless r.nil?
 | |
|       @options_parser.invalid_create_command
 | |
|       abort(r)
 | |
|     end
 | |
| 
 | |
|     password = self.options[:new_password] || enter_password(@args[2])
 | |
| 
 | |
|     q = {
 | |
|       "username" => @args[2],
 | |
|       "password" => password,
 | |
|       "email" => @args[3]
 | |
|     }
 | |
|     post "/user", q
 | |
|   end
 | |
| 
 | |
|   def delete_handler
 | |
|     r = inspect_parameters @options_parser.delete_params, @args[2]
 | |
|     unless r.nil?
 | |
|       @options_parser.invalid_delete_command
 | |
|       abort(r)
 | |
|     end
 | |
| 
 | |
|     if question(I18n.t("handler.user.question.delete", :name => @args[2]))
 | |
|       delete "/user/#{@args[2]}"
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def password_handler
 | |
|     r = inspect_parameters @options_parser.password_params, @args[2]
 | |
|     unless r.nil?
 | |
|       @options_parser.invalid_password_command
 | |
|       abort(r)
 | |
|     end
 | |
| 
 | |
|     password = enter_password(@args[2])
 | |
|     q = {
 | |
|         "password" => password
 | |
|     }
 | |
| 
 | |
|     put "/user/#{@args[2]}/password", q
 | |
|   end
 | |
| 
 | |
|   def email_handler
 | |
|     r = inspect_parameters @options_parser.email_params, @args[2], @args[3]
 | |
|     unless r.nil?
 | |
|       @options_parser.invalid_email_command
 | |
|       abort(r)
 | |
|     end
 | |
|     q = {
 | |
|         "email" => @args[3]
 | |
|     }
 | |
|     put "/user/#{@args[2]}/email", q
 | |
|   end
 | |
| 
 | |
|   def grant_handler
 | |
|     r = inspect_parameters @options_parser.grant_params, @args[2], @args[3], @args[4]
 | |
|     unless r.nil?
 | |
|       @options_parser.invalid_grant_command
 | |
|       abort(r)
 | |
|     end
 | |
| 
 | |
|     @args[3] = '' if @args[3].nil?
 | |
|     q = {
 | |
|       'cmd' => @args[3],
 | |
|       'privileges' => @args[4]
 | |
|     }
 | |
| 
 | |
|     put "/user/#{@args[2]}", q
 | |
|   end
 | |
| 
 | |
|   def enter_password user
 | |
|     print "Enter password for '#{user}': "
 | |
|     password = ""
 | |
|     begin
 | |
|       system("stty -echo")
 | |
|       password = STDIN.gets.strip
 | |
|       puts
 | |
|     ensure
 | |
|       system("stty echo")
 | |
|     end
 | |
|     password
 | |
|   end
 | |
| 
 | |
| end
 | 
