Class: Pcli::Services::Commands::Users::Change

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.field_optionsObject (readonly)

Returns the value of attribute field_options.



11
12
13
# File 'lib/pcli/services/commands/users/change.rb', line 11

def field_options
  @field_options
end

Class Method Details

.field_option(name, *args, **kw_args) ⇒ Object



13
14
15
16
# File 'lib/pcli/services/commands/users/change.rb', line 13

def field_option(name, *args, **kw_args)
  @field_options << name
  option name, *args, **kw_args
end

Instance Method Details

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



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pcli/services/commands/users/change.rb', line 36

def call(user: nil, all:, **args)
  field_args = args.slice(*self.class.field_options)

  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 change?', 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

  fields = nil

  if all == false && field_args.empty?
    fields = prompt
             .multi_select('Which fields do you want to change?', self.class.field_options)
             .map { |n| [n, true] }
             .to_h
  else
    begin
      fields = Util::Cli.analyze_fields_flags(all, self.class.field_options, field_args)
    rescue Util::Cli::FieldsFlagsError => e
      output.puts Pl.yellow(e.message)
      return CommandOutput.continue
    end
  end

  payload = {}

  if fields[:name]
    name = prompt.ask('Name', default: user['name'])
    payload['name'] = name if name != user['name']
  end

  if fields[:username]
    username = prompt.ask('Username', default: user['username'])
    payload['username'] = username if username != user['username']
  end

  if payload.empty?
    output.puts Pl.yellow('Nothing to change!')
    return CommandOutput.continue
  end

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

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

    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