Class: ElasticWhenever::CLI

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

Constant Summary collapse

SUCCESS_EXIT_CODE =
0
ERROR_EXIT_CODE =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
# File 'lib/elastic_whenever/cli.rb', line 8

def initialize(args)
  @args = args
  @option = Option.new(args)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



6
7
8
# File 'lib/elastic_whenever/cli.rb', line 6

def args
  @args
end

#optionObject (readonly)

Returns the value of attribute option.



6
7
8
# File 'lib/elastic_whenever/cli.rb', line 6

def option
  @option
end

Instance Method Details

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
# File 'lib/elastic_whenever/cli.rb', line 13

def run
  case option.mode
  when Option::DRYRUN_MODE
    option.validate!
    update_tasks(dry_run: true)
    Logger.instance.message("Above is your schedule file converted to scheduled tasks; your scheduled tasks was not updated.")
    Logger.instance.message("Run `elastic_whenever --help' for more options.")
  when Option::UPDATE_MODE
    option.validate!
    with_concurrent_modification_handling do
      update_tasks(dry_run: false)
    end
    Logger.instance.log("write", "scheduled tasks updated")
  when Option::CLEAR_MODE
    with_concurrent_modification_handling do
      clear_tasks
    end
    Logger.instance.log("write", "scheduled tasks cleared")
  when Option::LIST_MODE
    list_tasks
    Logger.instance.message("Above is your scheduled tasks.")
    Logger.instance.message("Run `elastic_whenever --help` for more options.")
  when Option::PRINT_VERSION_MODE
    print_version
  end

  SUCCESS_EXIT_CODE
rescue Aws::Errors::MissingRegionError
  Logger.instance.fail("missing region error occurred; please use `--region` option or export `AWS_REGION` environment variable.")
  ERROR_EXIT_CODE
rescue Aws::Errors::MissingCredentialsError => e
  Logger.instance.fail("missing credential error occurred; please specify it with arguments, use shared credentials, or export `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variable")
  ERROR_EXIT_CODE
rescue OptionParser::MissingArgument,
  Option::InvalidOptionException,
  Task::Target::InvalidContainerException => exn

  Logger.instance.fail(exn.message)
  ERROR_EXIT_CODE
end