Class: Pcli::Services::Commands::User::Change

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/pcli/services/commands/user/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/user/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/user/change.rb', line 13

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

Instance Method Details

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



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
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
# File 'lib/pcli/services/commands/user/change.rb', line 33

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

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

  if response.success?
    spinner.success
  else
    spinner.failure

    output.puts
    Output::ServerError.show(response, output, screen)
  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: response.json['name'])
    payload['name'] = name if name != response.json['name']
  end

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

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

  spinner = SimpleSpinnerBar.start('Changing user...', output)
  response = api_manager.ensure_authenticated { api.change_me(payload) }

  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