Sha256: ef491b8f1664f3748e1511ffe56b398e43cf910c7a682f6afe2167654aa5ea6c

Contents?: true

Size: 1.37 KB

Versions: 55

Compression:

Stored size: 1.37 KB

Contents

module Kontena::Cli::Containers
  class ListCommand < Kontena::Command
    include Kontena::Util
    include Kontena::Cli::Common
    include Kontena::Cli::GridOptions
    include Kontena::Cli::TableGenerator::Helper

    option ['-a', '--all'], :flag, 'Show all containers'

    requires_current_master
    requires_current_master_token

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

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

    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

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

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

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

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
kontena-cli-1.5.4 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.4.rc1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.3 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.2 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.0 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.0.rc1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.0.pre5 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.0.pre4 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.0.pre3 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.0.pre2 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.5.0.pre1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.4.3 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.4.3.rc1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.4.2 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.4.2.rc1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.4.2.pre1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.4.1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.4.1.rc1 lib/kontena/cli/containers/list_command.rb
kontena-cli-1.4.1.pre1 lib/kontena/cli/containers/list_command.rb