Class: Opsup::CLI

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/opsup/cli.rb,
lib/opsup/cli/option_builder.rb

Defined Under Namespace

Classes: OptionBuilder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app:, option_builder:, env_vars:) ⇒ CLI

Returns a new instance of CLI.



27
28
29
30
31
# File 'lib/opsup/cli.rb', line 27

def initialize(app:, option_builder:, env_vars:)
  @app = T.let(app, Opsup::App)
  @option_builder = T.let(option_builder, Opsup::CLI::OptionBuilder)
  @env_vars = T.let(env_vars, T::Hash[String, T.nilable(String)])
end

Class Method Details

.createObject



12
13
14
15
16
17
18
# File 'lib/opsup/cli.rb', line 12

def self.create
  new(
    app: Opsup::App.create,
    option_builder: Opsup::CLI::OptionBuilder.create,
    env_vars: ENV.to_h,
  )
end

Instance Method Details

#run(argv) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/opsup/cli.rb', line 34

def run(argv)
  parser = create_parser
  @option_builder.define_options(parser)

  options = @option_builder.options_from_env_vars(@env_vars)
  begin
    # It automatically exits with a help message if necessary.
    commands = parser.parse(argv, into: options)
  rescue OptionParser::MissingArgument => e
    puts e.message
    return false
  end

  begin
    config = @option_builder.generate_config(options)
    @app.run(commands, config)
  rescue Opsup::Error => e
    puts "Error: #{e.message}"
    return false
  end

  true
end