Class: Petticoat::CLI

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

Overview

Optional command entry. Loading the core never loads an application's setup.

Constant Summary collapse

HELP =
<<~TEXT
  Usage: petticoat install [--dry-run]
         petticoat export|check [--output PATH] [--diagnostics PATH] [--seed N]
         petticoat test [application test arguments]
         petticoat --help|--version

  Run from the application root. Installation creates missing files only.
  Export, check and test use your runner in config/petticoat.rb.
  See the Petticoat README for the integration contract.
TEXT

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments, root, out) ⇒ CLI

Returns a new instance of CLI.



40
41
42
43
44
# File 'lib/petticoat/cli.rb', line 40

def initialize(arguments, root, out)
  @arguments = arguments
  @root = File.expand_path(root)
  @out = out
end

Class Attribute Details

.integration ⇒ Object

Returns the value of attribute integration.



21
22
23
# File 'lib/petticoat/cli.rb', line 21

def integration
  @integration
end

Class Method Details

.run(arguments, root: Dir.pwd, out: $stdout, err: $stderr) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/petticoat/cli.rb', line 23

def run(arguments, root: Dir.pwd, out: $stdout, err: $stderr)
  self.integration = nil
  new(arguments.dup, root, out).run
rescue OptionParser::ParseError
  err.puts 'Petticoat: invalid command options; run petticoat --help.'
  1
rescue StandardError => e
  # Only our own explicit messages may reach the terminal.
  message = e.is_a?(Petticoat::Error) ||
            (e.instance_of?(RuntimeError) && e.message.start_with?('Petticoat'))
  err.puts(message ? e.message : "Petticoat command failed (#{e.class}).")
  1
ensure
  self.integration = nil
end

Instance Method Details

#run ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/petticoat/cli.rb', line 46

def run
  mode = @arguments.shift
  case mode
  when nil, '--help', '-h' then display(HELP)
  when '--version', '-v' then display(Petticoat::VERSION)
  when 'install' then install
  when 'export', 'check', 'test' then operate(mode)
  else raise Error, 'Petticoat: unknown command; run petticoat --help.'
  end
  0
end