Class: Pcli::Services::Commands::Users::Remove

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

Instance Method Summary collapse

Instance Method Details

#call(user: nil, **args) ⇒ Object



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
73
74
75
76
77
78
79
80
# File 'lib/pcli/services/commands/users/remove.rb', line 21

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

  if response.failure?
    output.puts
    Output::ServerError.show(response, output, screen)
    return
  end

  users = response.json

  unless user
    choices = users.map { |u| { name: "#{u['name']} (#{u['username']})", value: u['id'] } }
    user = prompt.select('Which user would you like to remove?', choices)
  end

  user = users.find { |u| u['id'] === user || u['username'].downcase === user.to_s.downcase }
  unless user
    output.puts(Pl.yellow('That user does not exist.'))
    return CommandOutput.continue
  end

  output.puts
  output.puts "This action will permanently remove #{user['name']} as an administrator."
  output.puts

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

  spinner = nil
  response = api_manager.ensure_authenticated do
    spinner = SimpleSpinnerBar.start('Removing user...', output)
    r = api.remove_user(user['id'])
    if r.failure?
      spinner.failure
    end
    r
  end

  if response.success?
    spinner.success("User #{Pl.green('removed')}")
  else
    output.puts
    Output::ServerError.show(response, output, screen)
  end

  CommandOutput.continue
end