Class: Kumogata2::CLI::OptionParser

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

Constant Summary collapse

DEFAULT_OPTIONS =
{
  result_log: File.join(Dir.pwd, 'result.json'),
  command_result_log: File.join(Dir.pwd, 'command_result.json'),
  color: $stdout.tty?,
}
COMMANDS =
{
  describe: {
    description: 'Describe a specified stack',
    arguments: [:stack_name],
  },
  create: {
    description: 'Create resources as specified in the template',
    arguments: [:path_or_url, :stack_name?],
    output: false,
  },
  update: {
    description: 'Update a stack as specified in the template',
    arguments: [:path_or_url, :stack_name],
    output: false,
  },
  delete: {
    description: 'Delete a specified stack',
    arguments: [:stack_name],
    output: false,
  },
  validate: {
    description: 'Validate a specified template',
    arguments: [:path_or_url],
    output: false,
  },
  list: {
    description: 'List summary information for stacks',
    arguments: [:stack_name?],
  },
  export: {
    description: 'Export a template from a specified stack',
    arguments: [:stack_name],
  },
  convert: {
    description: 'Convert a template format',
    arguments: [:path_or_url],
  },
  diff: {
    description: 'Compare templates logically (file, http://..., stack://...)',
    arguments: [:path_or_url1, :path_or_url2],
  },
  dry_run: {
    description: 'Create a change set and show it',
    arguments: [:path_or_url, :stack_name],
  },
  show_events: {
    description: 'Show events for a specified stack',
    arguments: [:stack_name],
  },
  show_outputs: {
    description: 'Show outputs for a specified stack',
    arguments: [:stack_name],
  },
  show_resources: {
    description: 'Show resources for a specified stack',
    arguments: [:stack_name],
  },
  template_summary: {
    description: 'Show template information for a specified stack',
    arguments: [:path_or_url],
  },
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse!(argv) ⇒ Object



73
74
75
# File 'lib/kumogata2/cli/option_parser.rb', line 73

def parse!(argv)
  self.new.parse!(argv)
end

Instance Method Details

#parse!(argv) ⇒ Object

of class methods



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

def parse!(argv)
  command = nil
  arguments = nil
  options = {aws: {}}

  opt = ::OptionParser.new
  opt.summary_width = 65535
  set_usage!(opt)

  opt.on('-k', '--access-key ACCESS_KEY') {|v| options[:aws][:access_key_id]     = v }
  opt.on('-s', '--secret-key SECRET_KEY') {|v| options[:aws][:secret_access_key] = v }
  opt.on('-r', '--region REGION')         {|v| options[:aws][:region]            = v }

  opt.on('', '--profile PROFILE') do |v|
    options[:aws][:credentials] ||= {}
    options[:aws][:credentials][:profile_name] = v
  end

  opt.on('', '--credentials-path PATH') do |v|
    options[:aws][:credentials] ||= {}
    options[:aws][:credentials][:path] = v
  end

  plugin_exts = Kumogata2::Plugin.plugins.flat_map(&:ext).uniq
  opt.on(''  , '--output-format FORMAT', plugin_exts) do |v|
    options[:output_format] = v
  end

  opt.on('-p', '--parameters KEY_VALUES', Array) {|v| options[:parameters]              = v }
  opt.on('-j', '--json-parameters JSON')         {|v| options[:json_parameters]         = v }
  opt.on(''  , '--[no-]deletion-policy-retain')  {|v| options[:deletion_policy_retain]  = v }

  {
    disable_rollback: :boolean,
    timeout_in_minutes: Integer,
    notification_arns: Array,
    capabilities: Array,
    resource_types: Array,
    on_failure: nil,
    stack_policy_body: nil,
    stack_policy_url: nil,
    use_previous_template: :boolean,
    stack_policy_during_update_body: nil,
    stack_policy_during_update_url: nil,
  }.each do |key, type|
    opt_str = key.to_s.gsub('_', '-')
    opt_val = key.to_s.upcase

    case type
    when :boolean
      opt.on('', "--[no-]#{opt_str}") {|v| options[key] = v }
    when nil
      opt.on('', "--#{opt_str} #{opt_val}") {|v| options[key] = v }
    else
      opt.on('', "--#{opt_str} #{opt_val}", type) {|v| options[key] = v }
    end
  end

  opt.on(''  , '--result-log PATH')         {|v| options[:result_log]       = v }
  opt.on(''  , '--command-result-log PATH') {|v| options[:command]          = v }
  opt.on(''  , '--[no-]detach')             {|v| options[:detach]           = v }
  opt.on(''  , '--[no-]force')              {|v| options[:force]            = v }
  opt.on(''  , '--[no-]color')              {|v| options[:color]            = v }
  opt.on(''  , '--[no-]ignore-all-space')   {|v| options[:ignore_all_space] = v }
  opt.on(''  , '--[no-]debug')              {|v| options[:debug]            = v }

  opt.parse!

  unless (command = argv.shift)
    puts opt.help
    exit_parse!(1)
  end

  orig_command = command
  command = command.gsub('-', '_').to_sym
  command = find_command(command)

  unless command
    raise "Unknown command: #{orig_command}"
  end

  arguments = argv.dup
  validate_arguments(command, arguments)

  options = DEFAULT_OPTIONS.merge(options)
  options = Hashie::Mash.new(options)

  if options[:aws][:credentials]
    credentials = Aws::SharedCredentials.new(options[:aws][:credentials])
    options[:aws][:credentials] = credentials
  end

  Aws.config.update(options[:aws].dup)
  options = Hashie::Mash.new(options)

  String.colorize = options.color?
  Diffy::Diff.default_format = options.color? ? :color : :text

  if options.debug?
    Kumogata2::Logger.instance.set_debug(options.debug?)

    Aws.config.update(
      :http_wire_trace => true,
      :logger => Kumogata2::Logger.instance
    )
  end

  update_parameters(options)
  output = COMMANDS.fetch(command).fetch(:output, true)

  options.command = command
  options.arguments = arguments
  options.output_result = output

  [command, arguments, options, output]
end