Class: EnvSpec::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/envspec/cli.rb,
lib/envspec/cli/init.rb,
lib/envspec/cli/lint.rb,
lib/envspec/cli/check.rb,
lib/envspec/cli/command.rb

Overview

Dispatcher for ‘envspec` subcommands.

Each subcommand is a class under EnvSpec::CLI inheriting from EnvSpec::CLI::Command. Register new commands by adding them to COMMANDS below and creating a file under lib/envspec/cli/.

Defined Under Namespace

Classes: Check, Command, Init, Lint

Constant Summary collapse

COMMANDS =
{
  "lint"  => Lint,
  "check" => Check,
  "init"  => Init,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(argv) ⇒ Object



19
20
21
# File 'lib/envspec/cli.rb', line 19

def self.run(argv)
  new.run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



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
# File 'lib/envspec/cli.rb', line 23

def run(argv)
  argv = argv.dup
  cmd = argv.shift

  case cmd
  when nil, "help", "-h", "--help"
    print_help(argv.first)
    return 0
  when "version", "-v", "--version"
    puts EnvSpec::VERSION
    return 0
  end

  klass = COMMANDS[cmd]
  unless klass
    warn "envspec: unknown command '#{cmd}'"
    warn ""
    print_help
    return 2
  end

  klass.new.run(argv)
rescue OptionParser::ParseError => e
  warn "envspec: #{e.message}"
  2
rescue EnvSpec::ParseError, EnvSpec::ValidationError => e
  warn "envspec: #{e.message}"
  1
end