Class: Lsaws::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/lsaws/cli.rb

Constant Summary collapse

SUPPORTED_FORMATS =
%w[table yaml json json-stream js text].sort
DEFAULT_FORMAT =
:table

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lsaws/cli.rb', line 10

def initialize(argv)
  @options = {
    format: DEFAULT_FORMAT,
    header: true,
    filters: {},
    show_cols: [],
    max_results: nil
  }
  @commands = option_parser.parse!(argv)
  if @options[:all]
    raise Error, "Unsupported arguments combination" if @commands.size != 1

    @commands << :all
  end
  @commands << :list if @options[:list]
end

Instance Method Details

#option_parserObject



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/lsaws/cli.rb', line 27

def option_parser
  @option_parser ||=
    OptionParser.new do |opt|
      opt.banner = "Usage: lsaws [options] <sdk> [entity_type]"

      opt.on("--endpoint ENDPOINT", "AWS endpoint") do |o|
        @options[:endpoint] = o
      end

      opt.on("--endpoint-url ENDPOINT", "AWS endpoint (aws cli compatible option)") do |o|
        @options[:endpoint] = o
      end

      opt.on("-p", "--profile PROFILE", "AWS profile") do |o|
        @options[:profile] = o
        ENV['AWS_PROFILE'] = o
      end

      opt.on("-o", "--output FMT", SUPPORTED_FORMATS, "Format: #{SUPPORTED_FORMATS.join("/")}") do |f|
        @options[:format] = f.to_sym
      end
      opt.on("--no-header", "Suppress header") { @options[:header] = false }
      opt.on("-x", 'Shortcut for "-o text --no-header"') do
        @options[:format] = :text
        @options[:header] = false
      end
      opt.on("--tags", "Show tags") { @options[:show_tags] = true }
      opt.on("-f", "--filter K=V", "Add filter") { |o| @options[:filters].merge!(Hash[*o.split("=", 2)]) }
      opt.on("-C", "--columns C", "Show only specified column(s)") do |o|
        if o[","]
          @options[:show_cols].append(*o.split(","))
        else
          @options[:show_cols] << o
        end
      end
      opt.on("--max-results N", Integer, "Fetch only specified number of results") do |o|
        @options[:max_results] = o
      end
      opt.on("--max-width X", Integer, "max text width for table/text mode, default: auto") do |o|
        @options[:max_width] = o
      end

      opt.separator ""
      opt.on("-v", "--verbose", "Verbose output") { @options[:verbose] = true }
      opt.on("--debug") { @options[:debug] = true }

      opt.separator ""
      opt.on("-L", "--list", "List SDKs or entity types") { @options[:list] = true }
      opt.on("-A", "--all", "List all entity types within SDK") { @options[:all] = true }
    end
end

#run!Object



79
80
81
82
83
84
85
86
87
# File 'lib/lsaws/cli.rb', line 79

def run!
  case @commands.size
  when 1, 2
    Lister.new(@options).process_command(*@commands)
  else
    puts option_parser.help
    nil
  end
end