Class: Pups::Cli

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

Class Method Summary collapse

Class Method Details

.optsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pups/cli.rb', line 7

def self.opts
  OptionParser.new do |opts|
    opts.banner = 'Usage: pups [FILE|--stdin]'
    opts.on('--stdin', 'Read input from stdin.')
    opts.on('--quiet', "Don't print any logs.")
    opts.on('--ignore <element(s)>', Array, "Ignore these template configuration elements, multiple elements can be provided (comma-delimited).")
    opts.on('--gen-docker-run-args', 'Output arguments from the pups configuration for input into a docker run command. All other pups config is ignored.')
    opts.on('-h', '--help') do
      puts opts
      exit
    end
  end
end

.parse_args(args) ⇒ Object



21
22
23
24
25
# File 'lib/pups/cli.rb', line 21

def self.parse_args(args)
  options = {}
  opts.parse!(args, into: options)
  options
end

.run(args) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pups/cli.rb', line 27

def self.run(args)
  options = parse_args(args)
  input_file = options[:stdin] ? 'stdin' : args.last
  unless input_file
    puts opts.parse!(%w[--help])
    exit
  end

  if options[:quiet]
    Pups.silence
  end

  Pups.log.info("Reading from #{input_file}")

  if options[:stdin]
    conf = $stdin.readlines.join
    split = conf.split('_FILE_SEPERATOR_')

    conf = nil
    split.each do |data|
      current = YAML.safe_load(data.strip)
      conf = if conf
        Pups::MergeCommand.deep_merge(conf, current, :merge_arrays)
      else
        current
      end
    end

    config = Pups::Config.new(conf, options[:ignore])
  else
    config = Pups::Config.load_file(input_file, options[:ignore])
  end

  if options[:"gen-docker-run-args"]
    print config.generate_docker_run_arguments
    return
  end

  config.run
ensure
  Pups::ExecCommand.terminate_async
end