Class: PI::Cli::Command::User

Inherits:
Base show all
Includes:
PI::Cli::ChooseHelper
Defined in:
lib/cli/commands/user.rb

Constant Summary collapse

YES_SET =
Set.new(["y", "Y", "yes", "YES"])
NO_SET =
Set.new(["n", "N", "no", "NO"])

Constants included from PI::Cli::ChooseHelper

PI::Cli::ChooseHelper::DEFAULTS

Instance Method Summary collapse

Methods included from PI::Cli::ChooseHelper

#check_envname_valid?, #check_envvalue_valid?, #check_name_valid?, #check_status, #check_version_name_valid?, #choose_app, #choose_app_help, #choose_app_help_target_not_all, #choose_dnsid, #choose_project, #choose_serviceid, #choose_target, #get_graceful, #select_apps, #total_opts

Methods inherited from Base

#auth_token, #client, #initialize, #target_url

Constructor Details

This class inherits a constructor from PI::Cli::Command::Base

Instance Method Details

#frameworksObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/cli/commands/user.rb', line 183

def frameworks
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  runtimes = client.runtimes
  java_runtime = runtimes[0].downcase
  ruby_runtime = runtimes[1].downcase
  java_frameworks = client.frameworks(java_runtime)
  ruby_frameworks = client.frameworks(ruby_runtime)
  frameworks = java_frameworks << ruby_frameworks[0] << ruby_frameworks[1]
  return display JSON.pretty_generate(frameworks) if @options[:json]
  return display "No Frameworks" if frameworks.empty?
  rtable = table do |t|
    t.headings = ['Name']
    frameworks.each { |f| t << [f]}
  end
  display "\n"
  display rtable
end

#github(name = nil) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cli/commands/user.rb', line 144

def github(name=nil)
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(email|password)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end 
  client.
  email = @options[:email]
  password = @options[:password]           
  name = ask "Please input your name" unless name
  email = ask "Please input your email" unless email
  password = ask "Please input your password", :echo => '*' unless password
  manifest = {
    :name => "#{name}",
    :password     => "#{password}",
    :email   => "#{email}",
    :passwordBase64 => "#{password}"
  }
  github = client.github(manifest)
  display "ok".green
end

#infoObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cli/commands/user.rb', line 57

def info      
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  info = client.info
  return display JSON.pretty_generate(info) if @options[:json]
  display "\nName: #{info[:name]}"
  display "Description: #{info[:description]}"
  display "Build: #{info[:build]}"
  display "Version: #{info[:version]}"      
end

#login(url = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cli/commands/user.rb', line 10

def (url=nil)
  user = @options[:user]
  password = @options[:password]
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(user|password)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  unless url
    url = ask "Attempting login to '#{target_url}'?", :default => true
    if url == true
      url = "#{target_url}"
    else 
      url = ask "Please input URL"
    end
  end      
  url = "#{target_url}" if url.nil? || url.empty?
  eval("PI::Cli::Command::Misc").new().send("set_target", url)
  unless client.target_valid?        
    display "Host is not available or is not valid: '#{url}'".red
    display "\n<<<\n#{client.raw_info}\n>>>\n"                   
    exit(false)    
  end
     
  tries ||= 0
  user = ask "User" unless user
  password = ask "Password", :echo => '*' unless password      
  (user, password)
  say "Successfully logged into [#{target_url}]".green
rescue PI::Client::TargetError
  display "Problem with login, invalid account or password.".red
  retry if (tries += 1) < 3 && !@options[:passwd]
  exit 1
rescue => e
  display "Problem with login, #{e}, try again.".red
  exit 1
end

#logoutObject



48
49
50
51
52
53
54
55
# File 'lib/cli/commands/user.rb', line 48

def logout
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  PI::Cli::Config.remove_token_file
  say "Successfully logged out of [#{target_url}]".green
end

#password(oldpassword = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cli/commands/user.rb', line 113

def password(oldpassword=nil)  
  all_opts = total_opts
  all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(password)/i}
  all_opts_except_valid_opts.each_value do |value|
    err "Invalid options" if value != nil
  end 
  client.   
  newpassword = @options[:password]
  oldpassword = ask "Old Password", :echo => '*' unless oldpassword
  unless newpassword
    loop{
    newpassword = ask "New Password", :echo => '*'
    newpassword2 = ask "Verify Password", :echo => '*'
    if newpassword != newpassword2
      display "Passwords did not match, try again" 
      newpassword =nil 
      next
    else 
      break
    end
    }  
  end
  err "New password is same with old one. Don't need to change password." if oldpassword == newpassword
  manifest = {
    :newPassword => newpassword,
    :oldPassword => oldpassword
  }
  client.password(manifest)
  display "ok".green
end

#runtimesObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cli/commands/user.rb', line 166

def runtimes
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  runtimes_info = client.runtimes
  return display JSON.pretty_generate(runtimes_info) if @options[:json]
  return display "No Runtimes" if runtimes_info.empty?
  rtable = table do |t|
    t.headings = ['Name']
    runtimes_info.each { |rt|  t << [rt] }
  end
  display "\n"
  display rtable
end

#targetsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cli/commands/user.rb', line 94

def targets
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  targets = client.targets
  return display JSON.pretty_generate(targets) if @options[:json]
  return display 'No Targets' if targets.empty?
  targets_table = table do |t|
    t.headings = 'Name', 'URL'
    targets.each do |target| 
      t << [target[:name], target[:targetUrl]]
    end
  end
  display "\n"
  display targets_table
end

#userObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cli/commands/user.rb', line 70

def user
  all_opts = total_opts
  all_opts.each_value do |value|
    err "Invalid options" if value != nil
  end
  client.
  user = client.
  github_info = client.github_info
  if github_info.nil?
    github_info = Hash.new
    github_info[:name] = "null"
    github_info[:email] = "null"
  end
  user_merge =user.merge(github_info) 
  return display JSON.pretty_generate(user_merge) if @options[:json]
  return display 'No user info' if user.empty?     
  display "\nUserName: #{user[:userName]}"
  display "DomainName: #{user[:domainName]}"
  display "RoleName: #{user[:roleName]}"
  display "State: #{user[:state]}"
  display "Github Account Name: #{github_info[:name]}"
  display "Github Account Email: #{github_info[:email]}"
end