Class: RCLoadEnv::CLI

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

Overview

Implementation of the rcloadenv command line.

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CLI

Create a command line handler.

Parameters:

  • args (Array<String>)

    Command-line arguments



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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

Instance Method Details

#runObject

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



94
95
96
97
98
99
100
101
102
103
104
# 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