Class: Supso::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/supso/commands.rb

Class Method Summary collapse

Class Method Details

.advanced_commandsObject



52
53
54
# File 'lib/supso/commands.rb', line 52

def self.advanced_commands
  ['logout', 'login', 'show', 'update', 'whereami', 'whoami']
end

.all_commandsObject



60
61
62
# File 'lib/supso/commands.rb', line 60

def self.all_commands
  self.advanced_commands + self.simple_commands
end

.helpObject



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

.loginObject



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.
  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.(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.(email, password)
      if !succeeded && reason
        puts reason
      end
    end
  end

  puts "Successfully signed in as #{ email }."
end

.logoutObject



80
81
82
# File 'lib/supso/commands.rb', line 80

def self.logout
  User.log_out!
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_emailObject



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

.showObject



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_commandsObject



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

.versionObject



158
159
160
# File 'lib/supso/commands.rb', line 158

def self.version
  puts Supso::VERSION
end

.whereamiObject



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

.whoamiObject



75
76
77
78
# File 'lib/supso/commands.rb', line 75

def self.whoami
  user = User.current_user
  puts user ? user.email : nil
end