Class: Pcli::Services::Commands::Users::Create

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

Instance Method Summary collapse

Instance Method Details

#call(**args) ⇒ Object



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
# File 'lib/pcli/services/commands/users/create.rb', line 19

def call(**args)
  payload = {}

  name = prompt.ask('Name')
  payload['name'] = name

  username = prompt.ask('Username')
  payload['username'] = username

  spinner = nil
  response = api_manager.ensure_authenticated do
    spinner = SimpleSpinnerBar.start('Creating user...', output)
    r = api.create_user(payload)
    if r.failure?
      spinner.failure
    end
    r
  end

  if response.success?
    spinner.success("User #{Pl.green('created')}")

    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

    output.puts
    output.puts TTY::Table.new(rows: [
                                 [Pl.bold('ID'), response.json['id']],
                                 [Pl.bold('Name'), response.json['name']],
                                 [Pl.bold('Username'), response.json['username']]
                               ]).render(:ascii)
  else
    spinner.failure

    output.puts
    Output::ServerError.show(response, output, screen)
  end

  CommandOutput.continue
end