class RCLoadEnv::CLI

Implementation of the rcloadenv command line.

Public Class Methods

new(args) click to toggle source

Create a command line handler.

@param [Array<String>] args Command-line arguments

# File lib/rcloadenv/cli.rb, line 30
def initialize args
  @project = nil
  @exclude = []
  @include = []
  @override = false
  @debug = false

  @parser = OptionParser.new do |opts|
    opts.banner = "Usage: rcloadenv [options] <config-name> [-- <command>]"
    opts.on "-p name", "--project=name", "--projectId=name",
            "Project to read runtime config from" do |p|
      @project = p
    end
    opts.on "-E key1,key2,key3", "--except=key1,key2,key3",
            "Runtime-config variables to exclude, comma delimited" do |v|
      @exclude += v.split ","
    end
    opts.on "-O key1,key2,key3", "--only=key1,key2,key3",
            "Runtime-config variables to include, comma delimited" do |v|
      @include += v.split ","
    end
    opts.on "-o", "--override",
            "Cause config to override existing environment variables" do
      @override = true
    end
    opts.on "-d", "--debug", "Enable debug output" do
      @debug = true
    end
    opts.on_tail "--version", "Show the version and exit" do
      puts RCLoadEnv::VERSION
      exit
    end
    opts.on_tail "-?", "--help", "Show the help text and exit" do
      puts @parser.help
      exit
    end
  end

  separator_index = args.index "--"
  @command_list = separator_index ? args[(separator_index+1)..-1] : nil

  args = args[0..separator_index] if separator_index
  begin
    remaining_args = @parser.parse args
  rescue OptionParser::ParseError => ex
    usage_error ex.message
  end
  @config_name = remaining_args.shift
  unless @config_name
    usage_error "You must provide a config name."
  end
  unless remaining_args.empty?
    usage_error "Extra arguments found: #{remaining_args.inspect}"
  end

  if @command_list && @command_list.empty?
    usage_error "Command cannot be empty. To output dotenv format, omit `--`."
  end
end

Public Instance Methods

run() click to toggle source

Run the command line handler. This method either never returns or throws an exception.

# File lib/rcloadenv/cli.rb, line 94
def run
  loader = RCLoadEnv::Loader.new @config_name,
      exclude: @exclude, include: @include, override: @override,
      project: @project, debug: @debug
  if @command_list
    loader.modify_env ENV
    exec(*@command_list)
  else
    loader.write_dotenv
  end
end
usage_error(msg) click to toggle source

@private

# File lib/rcloadenv/cli.rb, line 107
def usage_error msg
  STDERR.puts "rcloadenv: #{msg}"
  STDERR.puts @parser.help
  exit 1
end