Class: Kumogata::ArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/kumogata/argument_parser.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :delete_stack => true,
  :result_log => File.join(Dir.pwd, 'result.json'),
  :command_result_log => File.join(Dir.pwd, 'command_result.json'),
  :color => $stdout.tty?,
  :debug => false,
  :config_path => File.expand_path('~/.aws/config'),
}
COMMANDS =
{
  :create => {
    :description => 'Create resources as specified in the template',
    :arguments   => [:path_or_url, :stack_name?],
    :output      => false,
  },
  :validate => {
    :description => 'Validate a specified template',
    :arguments   => [:path_or_url],
    :output      => false,
  },
  :convert => {
    :description => 'Convert a template format',
    :arguments   => [:path_or_url],
  },
  :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,
  },
  :list => {
    :description => 'List summary information for stacks',
    :arguments   => [:stack_name?],
  },
  :export => {
    :description => 'Export a template from a specified stack',
    :arguments   => [: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],
  },
  :diff => {
    :description => 'Compare templates logically (file, http://..., stack://...)',
    :arguments   => [:path_or_url1, :path_or_url2],
  },
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse!(&block) ⇒ Object



66
67
68
# File 'lib/kumogata/argument_parser.rb', line 66

def parse!(&block)
  self.new.parse!(&block)
end

Instance Method Details

#parse!Object

of class methods



71
72
73
74
75
76
77
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
# File 'lib/kumogata/argument_parser.rb', line 71

def parse!
  command = nil
  arguments = nil
  options = {}

  if ENV['KUMOGATA_OPTIONS']
    ARGV.concat(scan_args(ENV['KUMOGATA_OPTIONS']))
  end

  ARGV.options do |opt|
    update_usage(opt)

    begin
      supported_formats = [:ruby, :json, :yaml, :js, :coffee, :json5]
      opt.on('-k', '--access-key ACCESS_KEY')                    {|v| options[:access_key_id]           = v     }
      opt.on('-s', '--secret-key SECRET_KEY')                    {|v| options[:secret_access_key]       = v     }
      opt.on('-r', '--region REGION')                            {|v| options[:region]                  = v     }
      opt.on(''  , '--profile CONFIG_PROFILE')                   {|v| options[:config_profile]          = v     }
      opt.on(''  , '--credentials-path PATH')                    {|v| options[:credentials_path]        = v     }
      opt.on(''  , '--config-path PATH')                         {|v| options[:config_path]             = v     }
      opt.on(''  , '--stack-policy-body PATH')                   {|v| options[:stack_policy_body]       = File.read(v) }
      opt.on(''  , '--stack-policy-url URL')                     {|v| options[:stack_policy_url]        = v     }

      opt.on(''  , '--format TMPLATE_FORMAT', supported_formats) {|v| options[:format]                  = v     }
      opt.on(''  , '--output-format FORMAT', supported_formats)  {|v| options[:output_format]           = v     }
      opt.on(''  , '--skip-replace-underscore')                  {    options[:skip_replace_underscore] = false }
      opt.on(''  , '--deletion-policy-retain')                   {    options[:deletion_policy_retain]  = true  }
      opt.on('-p', '--parameters KEY_VALUES', Array)             {|v| options[:parameters]              = v     }
      opt.on('-j', '--json-parameters JSON')                     {|v| options[:json_parameters]         = v     }
      opt.on('-e', '--encrypt-parameters KEYS', Array)           {|v| options[:encrypt_parameters]      = v     }
      opt.on('',   '--encryption-password PASS')                 {|v| options[:encryption_password]     = v     }
      opt.on('',   '--skip-send-password')                       {    options[:skip_send_password]      = true  }
      opt.on(''  , '--capabilities CAPABILITIES', Array)         {|v| options[:capabilities]            = v     }
      opt.on(''  , '--disable-rollback')                         {    options[:disable_rollback]        = true  }
      opt.on(''  , '--notify SNS_TOPICS', Array)                 {|v| options[:notify]                  = v     }
      opt.on(''  , '--timeout MINUTES', Integer)                 {|v| options[:timeout]                 = v     }
      opt.on(''  , '--result-log PATH')                          {|v| options[:result_log]              = v     }
      opt.on(''  , '--command-result-log PATH')                  {|v| options[:command]                 = v     }
      opt.on(''  , '--detach')                                   {    options[:detach]                  = true  }
      opt.on(''  , '--force')                                    {    options[:force]                   = true  }
      opt.on('-w', '--ignore-all-space')                         {    options[:ignore_all_space]        = true  }
      opt.on(''  , '--color')                                    {    options[:color]                   = true  }
      opt.on(''  , '--no-color')                                 {    options[:color]                   = false }
      opt.on(''  , '--debug')                                    {    options[:debug]                   = true  }
      opt.on('-v', '--verbose')                                  {    options[:verbose]                 = true  }
      opt.parse!

      unless (command = ARGV.shift)
        puts opt.help
        exit 1
      end

      command = command.to_sym

      unless COMMANDS.has_key?(command)
        raise "Unknown command: #{command}"
      end

      arguments = ARGV.dup
      validate_arguments(command, arguments)

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

      if block_given?
        yield(opt, command, arguments, options)
      end

      update_parameters(options)
    rescue => e
      $stderr.puts("#{e.message}")
      raise e if options[:debug]
      exit 1
    end
  end

  output = COMMANDS[command].fetch(:output, true)
  command = command.to_s.gsub('-', '_').to_sym

  $kumogata.command = command
  $kumogata.arguments = arguments
  $kumogata.options = options
  options = $kumogata.options # Copy of the reference

  [command, arguments, options, output]
end