Class: AppCommand::List

Inherits:
Convoy::ActionCommand::Base
  • Object
show all
Defined in:
lib/routes/list.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/routes/list.rb', line 5

def execute

    begin

        @opts = command_options
        @args = arguments

        @resource       = nil
        @export_id      = nil
        @silent         = @opts[:json] || @opts[:json_prompt] || @opts[:yaml] || @opts[:yaml_colored]
        @regions        = App::AWSCli::get_regions
        @terminal_width = Blufin::Terminal::get_terminal_width
        @data           = nil
        @table_widths   = {}
        @export_map     = {}
        @columns        = {}

        @columns, @data, @export_map, @table_widths = App::AWSReports::(@regions)

        opts_validate
        opts_routing

    rescue => e

        Blufin::Terminal::print_exception(e)

    end

end

#opts_routingObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/routes/list.rb', line 96

def opts_routing

    system('clear') unless @silent

    # Display metadata and exit (if -m flag is set)
    if @opts[:metadata]
        App::AWSReports::get_lookups(@data).each do |title, data|
            output = []
            output << "\x1B[38;5;240m    AWS-CLI Command:\x1B[38;5;106m #{data[:resource][App::AWSReports::KEY_CLI]['command']}" if data[:resource].has_key?(App::AWSReports::KEY_CLI) && data[:resource][App::AWSReports::KEY_CLI].has_key?('command')
            output << "\x1B[38;5;240m          Region(s):\x1B[38;5;94m #{data[:resource][App::AWSReports::KEY_REGIONS].join(', ')}" if data[:resource].has_key?(App::AWSReports::KEY_REGIONS)
            output << "\x1B[38;5;240mPreferred Region(s):\x1B[38;5;94m #{data[:resource][App::AWSReports::KEY_REGIONS_PREFERRED].join(', ')}" if data[:resource].has_key?(App::AWSReports::KEY_REGIONS_PREFERRED)
            Blufin::Terminal::custom(data[:resource_title], 55, "Exports: #{Blufin::Terminal::format_highlight(title)}", output)
        end
        puts
        exit
    end

    # Select Key (if not already specified using the --resource flag)
    if @resource.nil?
        resource_title = Blufin::Terminal::prompt_select('Select AWS Resource(s) to list:', @data.keys)
        @resource      = @data[resource_title]
    else
        resource_title = @opts[:resource]
    end

    # TODO - REMOVE CACHING
    cache_file = '/tmp/aws-uber-results-cache.txt'
    if !@opts[:resource].nil? && @opts[:resource].downcase == App::AWSReports::CONST_ALL

        if false && Blufin::Files::file_exists(cache_file)

            # TODO - REMOVE CACHING
            uber_results = {}
            puts
            puts "  \x1B[38;5;196mGetting regional_response from cache: #{Blufin::Terminal::format_directory(cache_file)}\x1B[0m"
            eval("uber_results = #{Blufin::Files::read_file(cache_file)[0]}")

        else

            # Get and display all resources.
            uber_results = {}
            cmd_color    = 240
            threads      = []
            no_fit       = []
            puts
            @data.each do |resource_title, resource|
                # Skip tables that don't fit.
                fit_res = fits_in_terminal(resource_title)
                unless fit_res[0]
                    no_fit << "\x1B[38;5;196m#{resource_title}\x1B[38;5;240m (required: #{fit_res[1]} / actual: #{fit_res[2]})"
                    next
                end
                sleep(0.01)
                cmd_regions = @regions.join(',')
                cmd_regions = resource[App::AWSReports::KEY_REGIONS].join(',') if resource.has_key?(App::AWSReports::KEY_REGIONS)
                cmd_regions = "\x1B[38;5;#{cmd_color}m[\x1B[38;5;246m#{cmd_regions}\x1B[38;5;#{cmd_color}m]"
                profile_name = App::AWSProfile::get_profile_name
                profile_output = profile_name.nil? ? '' : "\x1B[38;5;240 --profile \x1B[38;5;246m#{profile_name}"
                App::AWSOutputter::output_cli_command("aws #{resource[App::AWSReports::KEY_CLI][App::AWSReports::KEY_COMMAND]} --region #{cmd_regions}#{profile_output}\x1B[0m")
                threads << Thread.new {
                    uber_results[resource_title] = App::AWSReports::get_aws_data(@regions, resource, resource_title, silent: true)
                }
            end
            # If some tables don't fit, output a warning.
            sleep(0.01)
            if no_fit.any?
                Blufin::Terminal::warning("The following resource(s) will be omitted because they won't fit in the terminal window.", no_fit)
            else
                puts
            end
            Blufin::Terminal::execute_proc('AWS - Fetching data...', Proc.new {
                threads.each { |thread| thread.join }
            })

            # TODO - REMOVE CACHING
            Blufin::Files::write_file(cache_file, [uber_results.inspect])

        end

        puts
        @data.each do |resource_title, resource|
            if uber_results.has_key?(resource_title)
                output_aws_data(uber_results[resource_title], resource, resource_title, silent: false)
            end
        end

    else

        # Bomb-out if the output does not fit in Terminal.
        if !@opts[:json] && !@opts[:json_prompt] && !@opts[:yaml] && !@opts[:yaml_colored] && !fits_in_terminal(resource_title)[0]
            Blufin::Terminal::error("Output for #{Blufin::Terminal::format_highlight(resource_title)} \x1B[38;5;240m(#{Blufin::Terminal::format_action(@table_widths[resource_title])}\x1B[38;5;240m-width)\x1B[0m does not fit in Terminal \x1B[38;5;240m(#{Blufin::Terminal::format_action(@terminal_width)}\x1B[38;5;240m-width)\x1B[0m", 'Please make your terminal wider and try again.', true)
        end

        # TODO - REMOVE CACHING
        cache_file = "/tmp/aws-results-#{resource_title}-cache.txt"
        if false && Blufin::Files::file_exists(cache_file)

            # TODO - REMOVE CACHING
            puts
            puts "  \x1B[38;5;196mGetting regional_response from cache: #{Blufin::Terminal::format_directory(cache_file)}\x1B[0m"
            results = {}
            eval("results = #{Blufin::Files::read_file(cache_file)[0]}")

        else
            results = App::AWSReports::get_aws_data(@regions, @resource, resource_title, silent: @silent)

            # TODO - REMOVE CACHING
            Blufin::Files::write_file(cache_file, [results.inspect])
        end

        # Get and display a single resource.
        output_aws_data(results, @resource, resource_title, silent: @silent)
    end

end

#opts_validateObject



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/routes/list.rb', line 35

def opts_validate

    Blufin::Routes::only_one_of(@opts, [:json, :json_prompt, :yaml, :yaml_colored])

    # TODO - Implement!
    if @opts[:region] || @opts[:environment] || @opts[:project]
        raise RuntimeError, 'Not yet implemented!'
    end

    # Throw error if -m is set with any other flags.
    Blufin::Terminal::error("When #{Blufin::Terminal::format_flag('m')} is set, no other flags can be set.", "Found: #{Blufin::Routes::flags_set(@opts).to_i - 1} other flag(s).", true) if @opts[:metadata] && Blufin::Routes::flags_set(@opts).to_i > 1

    if @opts[:json_prompt]
        # For resources with exports, this enables you to pass the -r flag as both AcmCertificates or AcmCertificateArn (and it will resolve to the same API call).
        if @export_map.has_key?(@opts[:resource])
            if @export_map[@opts[:resource]].is_a?(Array) && @export_map[@opts[:resource]].length > 1
                @export_id = Blufin::Terminal::prompt_select('Which resource would you like to export?', @export_map[@opts[:resource]])
            else
                @export_id = @export_map[@opts[:resource]][0]
            end
        else
            @export_id = @opts[:resource]
            @export_map.each do |k, v|
                v.each do |resource|
                    if resource == @opts[:resource]
                        @opts[:resource] = k
                        break
                    end
                end
            end
        end
    end

    # Validate that resource exists (if passed from command-line).
    unless @opts[:resource].nil?
        if @opts[:resource].downcase == App::AWSReports::CONST_ALL
            Blufin::Terminal::error("The #{Blufin::Terminal::format_flag('v')} cannot be set when listing all resources.") if @opts[:verbose]
            Blufin::Terminal::error("The #{Blufin::Terminal::format_flag('j')} cannot be set when listing all resources.") if @opts[:json]
            Blufin::Terminal::error("The #{Blufin::Terminal::format_flag('j')} cannot be set when listing all resources.") if @opts[:json_prompt]
            Blufin::Terminal::error("The #{Blufin::Terminal::format_flag('y')} cannot be set when listing all resources.") if @opts[:yaml]
            Blufin::Terminal::error("The #{Blufin::Terminal::format_flag('Y')} cannot be set when listing all resources.") if @opts[:yaml_colored]
            @resource = App::AWSReports::CONST_ALL
        else
            Blufin::Terminal::error("Invalid: #{Blufin::Terminal::format_invalid(@opts[:resource])} . Available resources are:", @data.keys, true) unless @data.has_key?(@opts[:resource])
            @resource = @data[@opts[:resource]]
        end
    end

    # If response is JSON/YAML, Resource must be explicitly specified.
    if @opts[:json] || @opts[:json_prompt] || @opts[:yaml] || @opts[:yaml_colored]
        flag = 'j' if @opts[:json]
        flag = 'J' if @opts[:json_prompt]
        flag = 'y' if @opts[:yaml]
        flag = 'Y' if @opts[:yaml_colored]
        Blufin::Terminal::error("Must specify #{Blufin::Terminal::format_highlight('resource')} explicitly (using #{Blufin::Terminal::format_flag('r')}) if #{Blufin::Terminal::format_flag(flag)} is set.") if @opts[:resource].nil?
        if @opts[:json_prompt]
            Blufin::Terminal::error("Resource must have #{Blufin::Terminal::format_highlight('export')} defined if #{Blufin::Terminal::format_flag(flag)} is set.") unless @resource.has_key?(App::AWSReports::KEY_EXPORT)
        end
    end
end