Class: Kontena::Cli::Containers::ListCommand

Inherits:
Kontena::Command show all
Includes:
Kontena::Cli::Common, GridOptions, TableGenerator::Helper, Util
Defined in:
lib/kontena/cli/containers/list_command.rb

Constant Summary collapse

NON_STOP_STATES =
['paused', 'restarting', 'oom_killed', 'dead', 'running']

Instance Attribute Summary

Attributes inherited from Kontena::Command

#arguments, #exit_code, #result

Instance Method Summary collapse

Methods included from TableGenerator::Helper

#generate_table, included, #print_table, #table_generator

Methods included from GridOptions

included

Methods included from Kontena::Cli::Common

#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning

Methods included from Util

included, #longest_string_in_array, #safe_dig, #seconds_to_human, stringify_keys, stringify_keys!, symbolize_keys, symbolize_keys!, #time_ago, #time_until, which

Methods inherited from Kontena::Command

banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token

Instance Method Details

#container_id(row) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/kontena/cli/containers/list_command.rb', line 33

def container_id(row)
  if row['node']
    "#{row['node']['name']}/#{row['name']}"
  else
    "/#{row['name']}"
  end
end

#container_state(row) ⇒ Object



47
48
49
# File 'lib/kontena/cli/containers/list_command.rb', line 47

def container_state(row)
  NON_STOP_STATES.find { |state| row.fetch('state', {})[state] == true } || pastel.cyan('stopped')
end

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kontena/cli/containers/list_command.rb', line 20

def execute
  result = spin_if(!quiet?, "Retrieving container list") do
    Array(client.get("containers/#{current_grid}#{'?all=1' if all?}")['containers'])
  end

  print_table(result.reverse) do |row|
    row['id'] = container_id(row)
    row['created_at'] = time_ago(row['created_at'])
    row['cmd'] = truncate_cmd(row)
    row['state'] = container_state(row)
  end
end

#fieldsObject



15
16
17
18
# File 'lib/kontena/cli/containers/list_command.rb', line 15

def fields
  return ['id'] if quiet?
  { container_id: 'id', image: 'image', command: 'cmd', created: 'created_at', status: 'state' }
end

#truncate_cmd(row) ⇒ Object



41
42
43
44
45
# File 'lib/kontena/cli/containers/list_command.rb', line 41

def truncate_cmd(row)
  cmd = row['cmd'].nil? ? '' : row['cmd'].join(' ')
  cmd = "#{cmd[0..24]}#{pastel.cyan('..')}" if cmd.length > 26
  "\"#{cmd}\""
end