Class: Pcli::Services::Commands::User::Password

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/pcli/services/commands/user/password.rb

Instance Method Summary collapse

Instance Method Details

#callObject



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pcli/services/commands/user/password.rb', line 19

def call(*)
  spinner = nil
  response = api_manager.ensure_authenticated do
    spinner = SimpleSpinnerBar.start('Retrieving user...', output)
    r = api.me
    if r.failure?
      spinner.failure
    end
    r
  end

  if response.success?
    spinner.success
  else
    output.puts
    Output::ServerError.show(response, output, screen)
    return CommandOutput.continue
  end

  output.puts
  output.puts "This action will regenerate #{response.json['name']}'s password and display it once."
  output.puts

  if prompt.no?(Pl.red('Are you sure you want to continue?'))
    output.puts 'Command aborted.'
    return CommandOutput.continue
  end

  output.puts
  spinner = nil
  response = api_manager.ensure_authenticated do
    spinner = SimpleSpinnerBar.start('Regenerating password...', output)
    r = api.rotate_password
    if r.failure?
      spinner.failure
    end
    r
  end

  if response.success?
    spinner.success("Password #{Pl.green('regenerated')}")
    output.puts
    output.puts Pl.yellow('New password: ') + response.json['password']
    output.puts
    output.print 'Copy the password and press any key when finished...'
    input.gets
    output.clear_screen
  else
    output.puts
    Output::ServerError.show(response, output, screen)
  end

  CommandOutput.continue
end