Class: Bosh::Cli::BasicLoginStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/basic_login_strategy.rb

Instance Method Summary collapse

Constructor Details

#initialize(terminal, director, config, interactive) ⇒ BasicLoginStrategy


7
8
9
10
11
12
# File 'lib/cli/basic_login_strategy.rb', line 7

def initialize(terminal, director, config, interactive)
  @terminal = terminal
  @director = director
  @config = config
  @interactive = interactive
end

Instance Method Details

#login(target, username, password) ⇒ Object


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
# File 'lib/cli/basic_login_strategy.rb', line 14

def (target, username, password)
  if @interactive
    username = @terminal.ask("Your username: ") if username.blank?
    password = @terminal.ask_password("Enter password: ") if password.blank?
  end

  if username.blank? || password.blank?
    err("Please provide username and password")
  end

  if @director.(username, password)
    @terminal.say_green("Logged in as '#{username}'")
    @config.set_credentials(target, {
        "username" => username,
        "password" => password
      })
    @config.save
  else
    if @interactive
      @terminal.say_red("Cannot log in as '#{username}', please try again")
      (target, username, '')
    else
      err("Cannot log in as '#{username}'")
    end
  end
end