Class: Cyclid::Cli::User

Inherits:
Thor
  • Object
show all
Defined in:
lib/cyclid/cli/user.rb

Overview

‘user’ sub-command

Instance Method Summary collapse

Instance Method Details

#authenticateObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
# File 'lib/cyclid/cli/user.rb', line 97

def authenticate
  url = options[:server] || 'https://api.cyclid.io'
  username = options[:username]

  # Get the username if one wasn't provided
  if username.nil?
    print 'Username: '
    username = STDIN.gets.chomp
  end

  # Get the users password
  print 'Password: '
  password = STDIN.noecho(&:gets).chomp
  puts

  # Create a client that can authenticate with HTTP BASIC
  Formatter.colorize "Authenticating #{username} with #{url}"

  basic_client = Cyclid::Client::Tilapia.new(auth: Cyclid::Client::AuthMethods::AUTH_BASIC,
                                             url: url,
                                             username: username,
                                             password: password,
                                             log_level: debug?)

  # Get the user information
  user = basic_client.user_get(username)

  # Ensure the configuration directory exists
  Dir.mkdir(CYCLID_CONFIG_DIR, 0o700) unless Dir.exist? CYCLID_CONFIG_DIR

  # Generate a configuration file for each organization
  user['organizations'].each do |org|
    Formatter.colorize "Creating configuration file for organization #{org}"

    org_config = File.new(File.join(CYCLID_CONFIG_DIR, org), 'w+', 0o600)
    org_config.write "url: #{url}\n"
    org_config.write "organization: #{org}\n"
    org_config.write "username: #{username}\n"
    org_config.write "secret: #{user['secret']}\n"
  end
rescue StandardError => ex
  abort "Failed to authenticate user: #{ex}"
end

#modifyObject



58
59
60
61
62
63
64
65
# File 'lib/cyclid/cli/user.rb', line 58

def modify
  client.user_modify(client.config.username,
                     email: options[:email],
                     password: options[:password],
                     secret: options[:secret])
rescue StandardError => ex
  abort "Failed to modify user: #{ex}"
end

#passwdObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cyclid/cli/user.rb', line 68

def passwd
  # Get the new password
  print 'Password: '
  password = STDIN.noecho(&:gets).chomp
  print "\nConfirm password: "
  confirm = STDIN.noecho(&:gets).chomp
  print "\n"
  abort 'Passwords do not match' unless password == confirm

  # Modify the user with the new password
  begin
    client.user_modify(client.config.username, password: password)
  rescue StandardError => ex
    abort "Failed to modify user: #{ex}"
  end
end

#showObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cyclid/cli/user.rb', line 25

def show
  user = client.user_get(client.config.username)

  # Pretty print the user details
  Formatter.colorize 'Username', user['username']
  Formatter.colorize 'Name', (user['name'] || '')
  Formatter.colorize 'Email', user['email']
  Formatter.colorize 'Organizations'
  if user['organizations'].any?
    user['organizations'].each do |org|
      Formatter.puts "\t#{org}"
    end
  else
    Formatter.puts "\tNone"
  end
rescue StandardError => ex
  abort "Failed to get user: #{ex}"
end