Class: Mortar::DescribeCommand

Inherits:
Command
  • Object
show all
Includes:
ClientHelper, ResourceHelper, TTYHelper
Defined in:
lib/mortar/describe_command.rb

Constant Summary

Constants inherited from Command

Command::CHECKSUM_ANNOTATION, Command::LABEL

Instance Method Summary collapse

Methods included from ResourceHelper

#from_file, #from_files, #load_resources, #resources_output, #same_resource?, #stringify_hash

Methods included from TTYHelper

#pastel

Methods included from ClientHelper

#build_kubeconfig_from_env, #client, #create_client

Instance Method Details

#executeObject



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
# File 'lib/mortar/describe_command.rb', line 17

def execute
  resources = client.list_resources(labelSelector: { LABEL => name }).select{ |r|
    r..labels&.dig(LABEL) == name
  }.uniq{ |r|
    # Kube api returns same object from many api versions...
    "#{r.kind}/#{r..name}/#{r..namespace}"
  }.sort{ |a, b| # Sort resources so that non namespaced objects are outputted firts
    if a..namespace == b..namespace
      1
    elsif a..namespace.nil? && !b..namespace.nil?
      -1
    else
      0
    end
  }

  case output
  when 'table'
    table(resources)
  when 'yaml'
    puts resources_output(resources)
  when 'json'
    puts json_output(resources)
  else
    signal_usage_error "Unknown output format: #{output}"
  end
end

#json_output(resources) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/mortar/describe_command.rb', line 53

def json_output(resources)
  json = JSON.pretty_generate(resources.map(&:to_hash))
  return json unless $stdout.tty?

  lexer = Rouge::Lexers::JSON.new
  rouge = Rouge::Formatters::Terminal256.new(Rouge::Themes::Github.new)
  rouge.format(lexer.lex(json))
end

#table(resources) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/mortar/describe_command.rb', line 45

def table(resources)
  table = TTY::Table.new %w(NAMESPACE KIND NAME), []
  resources.each do |r|
    table << [r..namespace || '', r.kind, r..name]
  end
  puts table.render(:basic)
end