Class: Pcli::Services::Commands::Templates::Change

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

Instance Method Summary collapse

Instance Method Details

#call(template: 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
81
82
83
84
85
86
87
88
89
90
# File 'lib/pcli/services/commands/templates/change.rb', line 21

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

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

  templates = response.json

  unless template
    choices = templates.map { |t| { name: "#{t['name']} (#{t['id']})", value: t['id'] } }
    template = prompt.select('Which template would you like to view/change?', choices)
  end

  template = templates.find { |t| t['id'] === template }
  unless template
    output.puts(Pl.yellow('That template does not exist.'))
    return CommandOutput.continue
  end

  file = Tempfile.new('pcli_open_template')
  file.write(template['templateXML'])
  file.close

  unless editor.open(file.path)
    output.puts('Aborted.')
    return CommandOutput.continue
  end

  file.open
  new_template = file.read

  if template['templateXML'] == new_template
    output.puts('Nothing to change.')
    return CommandOutput.continue
  end

  unless prompt.yes?('Save changes?')
    output.puts('Aborted.')
    return CommandOutput.continue
  end

  response = api_manager.ensure_authenticated do
    spinner = SimpleSpinnerBar.start('Changing template...', output)
    r = api.change_template(template['id'], { 'templateXML' => new_template })
    if r.failure?
      spinner.failure
    else
    spinner.success("Template #{Pl.green('changed')}")
    end
    r
  end

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

  CommandOutput.continue
end