Class: OsCtl::Image::Cli::Containers

Inherits:
Command
  • Object
show all
Defined in:
lib/osctl/image/cli/containers.rb

Constant Summary collapse

FIELDS =
%i(
  pool
  id
  type
  distribution
  version
)

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods inherited from Command

#initialize, #require_args!, run

Constructor Details

This class inherits a constructor from OsCtl::Image::Cli::Command

Instance Method Details

#deleteObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/osctl/image/cli/containers.rb', line 34

def delete
  client = OsCtldClient.new
  cts = get_cts(client)
  cts.select! { |ct| ct[:type] == opts[:type] } if opts[:type]
  cts.select! { |ct| args.include?(ct[:id]) } if args.any?

  unless opts[:force]
    puts 'The following containers will be deleted:'
    cts.each { |ct| puts "  #{ct[:pool]}:#{ct[:id]}" }
    STDOUT.write('Continue? [y/N]: ')
    STDOUT.flush
    return if STDIN.readline.strip != 'y'
  end

  cts.each do |ct|
    client.delete_container(ct[:id])
    client.delete_user(ct[:user])
  end
end

#get_cts(client) ⇒ Object (protected)



55
56
57
58
59
60
61
62
# File 'lib/osctl/image/cli/containers.rb', line 55

def get_cts(client)
  cts = client.list_containers.select do |ct|
    ct.has_key?(:'org.vpsadminos.osctl-image:type')
  end.map do |ct|
    ct[:type] = ct[:'org.vpsadminos.osctl-image:type']
    ct
  end
end

#listObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/osctl/image/cli/containers.rb', line 15

def list
  if opts[:list]
    puts FIELDS.join("\n")
    return
  end

  cts = get_cts(OsCtldClient.new)

  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && opts[:sort].split(',').map(&:to_sym),
    header: !opts['hide-header'],
  }

  cols = opts[:output] ? opts[:output].split(',').map(&:to_sym) : FIELDS

  OsCtl::Lib::Cli::OutputFormatter.print(cts, cols, fmt_opts)
end