Class: Supso::Commands
- Inherits:
-
Object
- Object
- Supso::Commands
- Defined in:
- lib/supso/commands.rb
Class Method Summary collapse
- .advanced_commands ⇒ Object
- .all_commands ⇒ Object
- .help ⇒ Object
- .login ⇒ Object
- .logout ⇒ Object
- .prepare_command_mode! ⇒ Object
- .prompt_choices(choices = []) ⇒ Object
- .prompt_confirmation_token(to_email) ⇒ Object
- .prompt_email ⇒ Object
- .route_command(args) ⇒ Object
- .show ⇒ Object
- .simple_commands ⇒ Object
- .update(*project_names) ⇒ Object
- .version ⇒ Object
- .whereami ⇒ Object
- .whoami ⇒ Object
Class Method Details
.advanced_commands ⇒ Object
52 53 54 |
# File 'lib/supso/commands.rb', line 52 def self.advanced_commands ['logout', 'login', 'show', 'update', 'whereami', 'whoami'] end |
.all_commands ⇒ Object
60 61 62 |
# File 'lib/supso/commands.rb', line 60 def self.all_commands self.advanced_commands + self.simple_commands end |
.help ⇒ Object
153 154 155 156 |
# File 'lib/supso/commands.rb', line 153 def self.help puts "Usage: supso command" puts "Commands: #{ all_commands.sort.join(' ') }" end |
.login ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/supso/commands.rb', line 84 def self.login email = Commands.prompt_email without_password_response = Util.http_post("#{ Supso.supso_api_root }sign_in_request_token_api", email: email) if !without_password_response['success'] puts without_password_response['reason'] return end if without_password_response['confirmation_token_sent'] succeeded = false while !succeeded token = Commands.prompt_confirmation_token(email) succeeded, reason = User.log_in_with_confirmation_token!(email, token) if !succeeded && reason puts reason end end else succeeded = false while !succeeded puts "Enter your password:" password = STDIN.noecho(&:gets).chomp succeeded, reason = User.log_in_with_password!(email, password) if !succeeded && reason puts reason end end end puts "Successfully signed in as #{ email }." end |
.prepare_command_mode! ⇒ Object
9 10 11 |
# File 'lib/supso/commands.rb', line 9 def self.prepare_command_mode! require File.dirname(__FILE__) + '/../super_source/project' end |
.prompt_choices(choices = []) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/supso/commands.rb', line 13 def self.prompt_choices(choices = []) while true print "Enter a number 0 - #{ choices.length - 1}\n" choices.each_with_index do |choice, idx| choice_name = choice.is_a?(String) ? choice : choice[1] print "#{ idx }: #{ choice_name }\n" end choice = STDIN.gets.strip.to_i if 0 <= choice && choice < choices.length selected = choices[choice] return selected.is_a?(String) ? selected : selected[0] end end end |
.prompt_confirmation_token(to_email) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/supso/commands.rb', line 28 def self.prompt_confirmation_token(to_email) token = '' while token.length < 4 puts "Enter the confirmation token that was sent to #{ to_email }: (or enter 'resend')" token = STDIN.gets.strip if token.length < 4 puts "Sorry, #{ token } is not a valid confirmation token." end end token end |
.prompt_email ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/supso/commands.rb', line 40 def self.prompt_email email = '' while !Util.is_email?(email) puts "Enter your email address:" email = STDIN.gets.strip if !Util.is_email?(email) puts "Sorry, #{ email } is not a valid email address." end end email end |
.route_command(args) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/supso/commands.rb', line 129 def self.route_command(args) if args.length == 0 return Commands.help end command = args[0] if !Commands.all_commands.include?(command) puts "No such command: #{ command }" return Commands.help end if Commands.simple_commands.include?(command) return Commands.public_send(command) end Commands.prepare_command_mode! begin Commands.public_send(command, *args[1..-1]) rescue StandardError => err Logs.error(err) end end |
.show ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/supso/commands.rb', line 117 def self.show Project.detect_all_projects! if Project.projects.length == 0 puts "0 projects using Super Source." else puts "#{ Project.projects.length } #{ Util.pluralize(Project.projects.length, 'project') } using Super Source.\n" Project.projects.each do |project| project.puts_info end end end |
.simple_commands ⇒ Object
56 57 58 |
# File 'lib/supso/commands.rb', line 56 def self.simple_commands ['help', 'version'] end |
.update(*project_names) ⇒ Object
64 65 66 |
# File 'lib/supso/commands.rb', line 64 def self.update(*project_names) Updater.update(project_names) end |
.version ⇒ Object
158 159 160 |
# File 'lib/supso/commands.rb', line 158 def self.version puts Supso::VERSION end |
.whereami ⇒ Object
68 69 70 71 72 73 |
# File 'lib/supso/commands.rb', line 68 def self.whereami puts "Project = #{ Supso.project_root }" puts "Project Config = #{ Supso.project_supso_config_root }" puts "User Config = #{ Supso.user_supso_config_root }" puts "Supso Gem = #{ Supso.gem_root }" end |
.whoami ⇒ Object
75 76 77 78 |
# File 'lib/supso/commands.rb', line 75 def self.whoami user = User.current_user puts user ? user.email : nil end |