Sha256: 5e45d61a5d61ad1cf39856f54c7b782e12d714466d22067b0f2399bebf53414c

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require 'optparse'

module Pups
  class Cli
    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

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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pups-1.1.1 lib/pups/cli.rb
pups-1.1.0 lib/pups/cli.rb