class Sfctl::Commands::Auth::Init

Public Class Methods

new(options) click to toggle source
# File lib/sfctl/commands/auth/init.rb, line 11
def initialize(options)
  @options = options
  @pastel = Pastel.new(enabled: !@options['no-color'])
end

Public Instance Methods

execute(output: $stdout) click to toggle source
# File lib/sfctl/commands/auth/init.rb, line 16
def execute(output: $stdout)
  access_token = ::TTY::Prompt.new.ask("Access token(#{@options['starfish-host']}):", required: true)
  spinner = ::TTY::Spinner.new('[:spinner] Checking token ...')
  spinner.auto_spin
  token_valid?(access_token) ? update_config!(spinner, output, access_token) : render_error(spinner, output)
end

Private Instance Methods

render_error(spinner, output) click to toggle source
# File lib/sfctl/commands/auth/init.rb, line 44
def render_error(spinner, output)
  spinner.error
  output.puts wrong_token_message
end
token_accepted_message() click to toggle source
# File lib/sfctl/commands/auth/init.rb, line 29
def token_accepted_message
  @pastel.green('Credentials are accepted.')
end
token_valid?(access_token) click to toggle source
# File lib/sfctl/commands/auth/init.rb, line 25
def token_valid?(access_token)
  Starfish::Client.check_authorization(@options['starfish-host'], access_token)
end
update_config!(spinner, output, access_token) click to toggle source
# File lib/sfctl/commands/auth/init.rb, line 37
def update_config!(spinner, output, access_token)
  config.set :access_token, value: access_token
  save_config!
  spinner.success
  output.puts token_accepted_message
end
wrong_token_message() click to toggle source
# File lib/sfctl/commands/auth/init.rb, line 33
def wrong_token_message
  @pastel.red('Token is not accepted, please make sure you copy and paste it correctly.')
end