Module: NexClient::Commands::Addons

Extended by:
Helpers
Defined in:
lib/nex_client/commands/addons.rb

Constant Summary collapse

ADDONS_TITLE =
"Addons".colorize(:red)
ADDONS_HEADERS =
['id','name','status','service','app'].map(&:upcase)

Constants included from Helpers

Helpers::LOG_COLORS

Class Method Summary collapse

Methods included from Helpers

display_logs, display_record_errors, error, success

Class Method Details

.create(args, opts) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nex_client/commands/addons.rb', line 48

def self.create(args,opts)
  svc_name,app_name = args
  app = NexClient::App.find(name: app_name).first

  # Display error
  unless app
    error("Error! Could not find app: #{app_name}")
    return false
  end

  addon = NexClient::Addon.new(service: svc_name)
  addon.relationships.attributes = { app: { data: { type: 'apps', id: app.id } } }
  addon.save

  # Display errors if any
  if addon.errors.any?
    display_record_errors(addon)
    return false
  end

  # Display app
  self.display_addons(NexClient::Addon.includes(:app).find(addon.id).first)
end

.destroy(args, opts) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/nex_client/commands/addons.rb', line 72

def self.destroy(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Ask confirmation
  answer = ask("Enter the name of this addon to confirm: ")
  unless answer == e.name
    error("Aborting deletion...")
    return false
  end

  e.destroy
  success("Successfully destroyed addon: #{name}")
end

.display_addons(list) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/nex_client/commands/addons.rb', line 93

def self.display_addons(list)
  table = Terminal::Table.new title: ADDONS_TITLE, headings: ADDONS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(self.format_record(e))
    end
  end
  puts table
  puts "\n"
end

.format_app(record) ⇒ Object



114
115
116
117
# File 'lib/nex_client/commands/addons.rb', line 114

def self.format_app(record)
  return '-' unless a = record.app
  a.name
end

.format_record(record) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/nex_client/commands/addons.rb', line 103

def self.format_record(record)
  app = self.format_app(record)
  [
    record.id,
    record.name,
    record.status,
    record.service,
    app
  ]
end

.list(args, opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nex_client/commands/addons.rb', line 10

def self.list(args,opts)
  filters = {}
  filters[:status] = opts.status || 'active'
  filters[:service] = opts.service if opts.service.present?

  # All option
  filters = {} if opts.all

  # Scope to specific app
  filters[:'app.name'] = args.first if args.first.present?

  # Create table
  list = NexClient::Addon.includes(:app).where(filters).order('status')
  self.display_addons(list)

  # Loop through results
  while (list.pages.links||{})['next']
    ask("Press enter for next page")
    list = list.pages.next
    self.display_addons(list)
  end
end

.logs(args, opts) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nex_client/commands/addons.rb', line 33

def self.logs(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Retrieve logs and display them
  logs = e.logs(tail: opts.tail).first
  self.display_logs(logs.log_ret)
end