From 3850eea62b6ba5d5e7009621c03bbae32146049d Mon Sep 17 00:00:00 2001 From: Daniel Weinmann Date: Fri, 14 Jul 2017 16:25:59 -0300 Subject: [PATCH] Accepts --env option to export environment variables to the executable --- README.md | 7 +++++++ lib/subcontractor/cli.rb | 4 ++++ lib/subcontractor/command.rb | 2 +- spec/subcontractor/cli_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 488ae17..a2e2a2c 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ USAGE: subcontract [options] -- executable -c, --choose-env ENV run in either a specified RBENV, RVM or CHRUBY, whichever is present -d, --chdir PATH chdir to PATH before starting process -s, --signal SIGNAL signal to send to process to kill it, default TERM + -e, --env VAR=value additional environment variable to be set on executable context ``` An example Procfile tells the story @@ -67,6 +68,12 @@ If you have team members using both rvm and rbenv on a project then use --choose mixed_env_manager_app: bundle exec subcontract --choose-env . --chdir ~/mixed_env_manager_app -- bundle exec rails server -p 3001 ``` +You can pass multiple environment variables to be passed along to the executable. + +``` +rbenv_app: bundle exec subcontract --rbenv 'ree-1.8.7-2012.02' --chdir ~/rbenv_app --env FOO=bar --env BAR=foo -- bundle exec rails server -p 3001 +``` + ### Contributions * Fork the project diff --git a/lib/subcontractor/cli.rb b/lib/subcontractor/cli.rb index 5330354..e962349 100644 --- a/lib/subcontractor/cli.rb +++ b/lib/subcontractor/cli.rb @@ -76,6 +76,7 @@ def find_child_pids(pids) def parse_options(argv) options = {} + options[:env] = [] parser = OptionParser.new do |opt| opt.banner = "USAGE: subcontract [options] -- executable" opt.on("-r", "--rvm RVM", "run in a specific RVM") do |rvm| @@ -96,6 +97,9 @@ def parse_options(argv) opt.on("-s", "--signal SIGNAL", "signal to send to process to kill it, default TERM") do |signal| options[:signal] = signal end + opt.on("-e", "--env VAR=value", "additional environment variable to be set on executable context") do |env| + options[:env] << "env #{env}" + end end parser.parse! argv diff --git a/lib/subcontractor/command.rb b/lib/subcontractor/command.rb index f0aa652..d875a2d 100644 --- a/lib/subcontractor/command.rb +++ b/lib/subcontractor/command.rb @@ -18,7 +18,7 @@ def build @parts.unshift("chruby-exec #{_env_specifier(:chruby)} --") end - @parts.join(" ") + (@options[:env] + @parts).join(" ") end def _use_command?(command) diff --git a/spec/subcontractor/cli_spec.rb b/spec/subcontractor/cli_spec.rb index e6d9dab..a771b98 100644 --- a/spec/subcontractor/cli_spec.rb +++ b/spec/subcontractor/cli_spec.rb @@ -68,5 +68,27 @@ Subcontractor::CLI.new.run end end + + context "with --env" do + it "specifies custom env variable" do + ARGV = ["--env", "FOO=bar", "test"] + SafePty.should_receive(:spawn).with("env FOO=bar test") + Subcontractor::CLI.new.run + end + + it "specifies custom env variables if many where passed" do + ARGV = ["--env", "FOO=bar", "--env", "BAR=foo", "test"] + SafePty.should_receive(:spawn).with("env FOO=bar env BAR=foo test") + Subcontractor::CLI.new.run + end + end + + context "with --env and --rbenv" do + it "specifies custom env variables if many where passed" do + ARGV = ["--rbenv", "1.9.3", "--env", "FOO=bar", "--env", "BAR=foo", "test"] + SafePty.should_receive(:spawn).with("env FOO=bar env BAR=foo env RBENV_VERSION=1.9.3 rbenv exec test") + Subcontractor::CLI.new.run + end + end end end