Class: Central::Cli::Nodes::ListCommand

Inherits:
Clamp::Command
  • Object
show all
Includes:
Common, StackOptions
Defined in:
lib/central/cli/nodes/list_command.rb

Instance Method Summary collapse

Methods included from StackOptions

included

Methods included from Common

#access_token=, #add_master, #api_url, #api_url=, #clear_current_stack, #client, #current_master, #current_master=, #current_master_index, #current_stack, #current_stack=, #ensure_custom_ssl_ca, #require_api_url, #require_current_stack, #require_token, #reset_client, #save_settings, #settings, #settings_filename

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
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
# File 'lib/central/cli/nodes/list_command.rb', line 11

def execute
  require_api_url
  require_current_stack
  token = require_token
  rows = []

  if all?
    stacks = client(token).get('stacks')
    header = ['Name', 'Public IP', 'Status', 'Labels']

    stacks['stacks'].each do |stack|
      nodes = client(token).get("stacks/#{stack['name']}/nodes")
      nodes['nodes'].each do |node|
        status = if node['connected']
                   ''.colorize(:green)
                 else
                   ''.colorize(:red)
                 end
        rows << [
          "#{stack['name']}/#{node['name']}",
          node['public_ip'],
          status,
          (node['labels'] || ['-']).join(', ')
        ]
      end
    end
  else
    nodes = client(token).get("stacks/#{current_stack}/nodes")
    header = ['Name', 'Public IP', 'Status', 'Labels']
    nodes['nodes'].each do |node|
      status = if node['connected']
                 ''.colorize(:green)
               else
                 ''.colorize(:red)
               end
      rows << [
        node['name'],
        node['public_ip'],
        status,
        (node['labels'] || ['-']).join(', ')
      ]
    end

    ttable = TTY::Table.new header: header, rows: Array.new(rows)
    renderer = TTY::Table::Renderer::Unicode.new(ttable)
    renderer.border.style = :cyan
    renderer.padding = [0, 1, 0, 1]
    puts renderer.render
  end
end