Class: GoodData::Command::Auth

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

Class Method Summary collapse

Class Method Details

.ask_for_credentials(credentials_file_path = Helpers::AuthHelper.credentials_file) ⇒ Hash

Ask for credentials

Parameters:

  • credentials_file_path (String) (defaults to: Helpers::AuthHelper.credentials_file)

    (credentials_file) Path to .gooddata file

Returns:

  • (Hash)
    • :username [String] Username (email address)
    • :password [String] Password
    • :auth_token [String] Authorization token
    • :environment [String] Environment - DEVELOPMENT, TEST, PRODUCTION
    • :server => [String] Server - https://secure.gooddata.com


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gooddata/commands/auth.rb', line 24

def ask_for_credentials(credentials_file_path = Helpers::AuthHelper.credentials_file)
  puts 'Enter your GoodData credentials.'

  old_credentials = Helpers::AuthHelper.read_credentials(credentials_file_path)

  # Ask for user email
  user = GoodData::CLI.terminal.ask('Email') do |q|
    set_default_value(q, old_credentials[:username])
  end

  # Ask for password
  password = GoodData::CLI.terminal.ask('Password') do |q|
    # set_default_value(q, old_credentials[:password])
    q.echo = '*'
  end

  # Ask for token
  auth_token = GoodData::CLI.terminal.ask('Authorization Token') do |q|
    set_default_value(q, old_credentials[:auth_token])
  end

  # Read environment
  environment = GoodData::CLI.terminal.ask('Environment') do |q|
    set_default_value(q, old_credentials[:environment], GoodData::Project::DEFAULT_ENVIRONMENT)
  end

  # Read server
  server = GoodData::CLI.terminal.ask('Server') do |q|
    set_default_value(q, old_credentials[:server], 'https://secure.gooddata.com')
  end

  # Return as struct
  {
    :username => user,
    :password => password,
    :auth_token => auth_token,
    :environment => environment,
    :server => server
  }
end

.set_default_value(q, value, default = nil) ⇒ Object

Conditionally sets default value for prompt. Default value is set to 'value' or 'default'

Parameters:

  • q (Highline)

    Highline instance used

  • value (String)

    Value used for ask default if not nil and not empty

  • default (String) (defaults to: nil)

    Value used for ask default iv 'value' is nil or empty



97
98
99
100
101
102
103
# File 'lib/gooddata/commands/auth.rb', line 97

def set_default_value(q, value, default = nil)
  if !value.nil? && !value.empty?
    q.default = value
  elsif default
    q.default = default
  end
end

.store(credentials_file_path = Helpers::AuthHelper.credentials_file) ⇒ Object

Ask for credentials and store them



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gooddata/commands/auth.rb', line 66

def store(credentials_file_path = Helpers::AuthHelper.credentials_file)
  puts 'This will store credntials to GoodData in an enencrypted form to your harddrive to file ~/.gooddata.'
  overwrite = GoodData::CLI.terminal.ask('Do you want to continue? (y/n)')
  return if overwrite != 'y'

  credentials = ask_for_credentials

  ovewrite = if File.exist?(credentials_file_path)
               GoodData::CLI.terminal.ask('Overwrite existing stored credentials (y/n)')
             else
               'y'
             end

  if ovewrite == 'y'
    Helpers::AuthHelper.write_credentials(credentials, credentials_file_path)
  else
    puts 'Aborting...'
  end
end

.unstore(credentials_file_path = Helpers::AuthHelper.credentials_file) ⇒ Object

Delete stored credentials



87
88
89
# File 'lib/gooddata/commands/auth.rb', line 87

def unstore(credentials_file_path = Helpers::AuthHelper.credentials_file)
  Helpers::AuthHelper.remove_credentials_file(credentials_file_path)
end