diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1d503a2..02bec49 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,10 +16,10 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - ruby: ["3.2"] + ruby: ["3.3"] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} @@ -34,10 +34,10 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - ruby: ["3.2"] + ruby: ["3.3"] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} @@ -52,7 +52,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Release Gem uses: discourse/publish-rubygems-action@v2 diff --git a/.rubocop.yml b/.rubocop.yml index d4dfe0d..d46296c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,2 @@ inherit_gem: rubocop-discourse: default.yml - diff --git a/README.md b/README.md index dab6b4f..b11aa93 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,26 @@ Running: `pups --tags="sometag,anothertag" somefile.yaml` will not run the echo Running: `pups --skip-tags="sometag,anothertag" somefile.yaml` will ONLY run the echo goodbye statement. +#### Parameter overriding + +The `--params` argument allows pups to dynamically override params set within a configuration for the single pups run. + +Note, it is expected to be of the form `key=value`. If it is malformed, a warning will be thrown. + +Example: + +``` +# somefile.yaml + +params: + param1: false_prophet + param2: also overridden +run: + - exec: + cmd: /bin/bash -c 'echo $param1 $param2 >> hello' +``` +Running `pups --params="param1=true_value,param2=other_true_value" somefile.yaml` will overwrite param1 and param2 with true_value and other_true_value respectively + #### Docker run argument generation The `--gen-docker-run-args` argument is used to make pups output arguments be in the format of `docker run `. Specifically, pups diff --git a/lib/pups/cli.rb b/lib/pups/cli.rb index a4ea32e..47a106d 100644 --- a/lib/pups/cli.rb +++ b/lib/pups/cli.rb @@ -24,6 +24,11 @@ def self.opts Array, "Run all but listed tagged commands." ) + opts.on( + "--params ", + Array, + "Replace params in the config with params listed." + ) opts.on("-h", "--help") do puts opts exit @@ -69,7 +74,8 @@ def self.run(args) conf, options[:ignore], tags: options[:tags], - skip_tags: options[:"skip-tags"] + skip_tags: options[:"skip-tags"], + extra_params: options[:params] ) else config = @@ -77,7 +83,8 @@ def self.run(args) input_file, options[:ignore], tags: options[:tags], - skip_tags: options[:"skip-tags"] + skip_tags: options[:"skip-tags"], + extra_params: options[:params] ) end diff --git a/lib/pups/config.rb b/lib/pups/config.rb index f65b599..9bfcdfd 100644 --- a/lib/pups/config.rb +++ b/lib/pups/config.rb @@ -7,8 +7,9 @@ class Config def initialize( config, ignored = nil, - tags: tags = nil, - skip_tags: skip_tags = nil + tags: nil, + skip_tags: nil, + extra_params: nil ) @config = config @@ -41,6 +42,16 @@ def initialize( ) @params = @config["params"] + if extra_params + extra_params.each do |val| + key_val = val.split("=", 2) + if key_val.length == 2 + @params[key_val[0]] = key_val[1] + else + warn "Malformed param #{val}. Expected param to be of the form `key=value`" + end + end + end ENV.each { |k, v| @params["$ENV_#{k}"] = v } inject_hooks end @@ -48,14 +59,16 @@ def initialize( def self.load_file( config_file, ignored = nil, - tags: tags = nil, - skip_tags: skip_tags = nil + tags: nil, + skip_tags: nil, + extra_params: nil ) Config.new( YAML.load_file(config_file), ignored, tags: tags, - skip_tags: skip_tags + skip_tags: skip_tags, + extra_params: extra_params ) rescue Exception warn "Failed to parse #{config_file}" @@ -67,14 +80,16 @@ def self.load_file( def self.load_config( config, ignored = nil, - tags: tags = nil, - skip_tags: skip_tags = nil + tags: nil, + skip_tags: nil, + extra_params: nil ) Config.new( YAML.safe_load(config), ignored, tags: tags, - skip_tags: skip_tags + skip_tags: skip_tags, + extra_params: extra_params ) end @@ -108,10 +123,7 @@ def self.combine_template_and_process_env(config, env) # Filter run commands by tag: by default, keep all commands that contain tags. # If skip_tags argument is true, keep all commands that DO NOT contain tags. - def filter_tags( - include_tags: include_tags = nil, - exclude_tags: exclude_tags = nil - ) + def filter_tags(include_tags: nil, exclude_tags: nil) if include_tags @config["run"] = @config["run"].select do |row| keep = false diff --git a/lib/pups/exec_command.rb b/lib/pups/exec_command.rb index 66055e1..34b65cd 100644 --- a/lib/pups/exec_command.rb +++ b/lib/pups/exec_command.rb @@ -87,7 +87,6 @@ def run Pups.log.info("> #{command}") pid = spawn(command) Pups.log.info(@result.readlines.join("\n")) if @result - pid end rescue StandardError raise if @raise_on_fail diff --git a/lib/pups/file_command.rb b/lib/pups/file_command.rb index dc8f64d..b946406 100644 --- a/lib/pups/file_command.rb +++ b/lib/pups/file_command.rb @@ -20,8 +20,6 @@ def initialize @type = :bash end - attr_writer :params - def run path = interpolate_params(@path) diff --git a/pups.gemspec b/pups.gemspec index 70ee0cd..33d7d09 100644 --- a/pups.gemspec +++ b/pups.gemspec @@ -1,7 +1,7 @@ # frozen_string_literal: true lib = File.expand_path('lib', __dir__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +$LOAD_PATH.unshift(lib) if !$LOAD_PATH.include?(lib) require 'pups/version' Gem::Specification.new do |spec| diff --git a/test/cli_test.rb b/test/cli_test.rb index a7f6164..4d68977 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -140,6 +140,7 @@ def test_cli_tags Cli.run(["--tags", "1,3", cf.path]) assert_equal("1\n3", File.read(f.path).strip) end + def test_cli_skip_tags # for testing output f = Tempfile.new("test_output") @@ -164,5 +165,28 @@ def test_cli_skip_tags Cli.run(["--skip-tags", "1,3", cf.path]) assert_equal("2", File.read(f.path).strip) end + + def test_cli_params + # for testing output + f = Tempfile.new("test_output") + f.close + + # for testing input + cf = Tempfile.new("test_config") + cf.puts <<~YAML + params: + one: 0 + two: 0 + run: + - exec: + cmd: echo $one >> #{f.path} + - exec: + cmd: echo $two >> #{f.path} + YAML + cf.close + + Cli.run(["--params", "one=1,two=2", cf.path]) + assert_equal("1\n2", File.read(f.path).strip) + end end end diff --git a/test/config_test.rb b/test/config_test.rb index 770d5ae..b3c34c9 100644 --- a/test/config_test.rb +++ b/test/config_test.rb @@ -283,5 +283,15 @@ def test_tag_filtering config["run"][1] ) end + + def test_extra_params + config = <<~YAML + params: + one: 1 + YAML + config = Config.new(YAML.safe_load(config), extra_params: %w[one=2 two=2]) + assert_equal("2", config.params["one"]) + assert_equal("2", config.params["two"]) + end end end