From 40879455c6d706191dc5239f382e5cca3441c1c7 Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Mon, 7 Jul 2025 22:55:50 -0700 Subject: [PATCH 1/5] FEATURE: add a --params option Add a --params option that dynamically adds or modifies params of a pups run usage: `pups --params foo=bar,baz=other config.yml` This would dynamically set or override any params set in config.yml for the run. DEV: linting, and rubocop --- .rubocop.yml | 3 +++ README.md | 20 ++++++++++++++++++++ lib/pups/cli.rb | 11 +++++++++-- lib/pups/config.rb | 37 +++++++++++++++++++++++++------------ lib/pups/exec_command.rb | 1 - lib/pups/file_command.rb | 2 -- test/cli_test.rb | 24 ++++++++++++++++++++++++ test/config_test.rb | 10 ++++++++++ 8 files changed, 91 insertions(+), 17 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index d4dfe0d..3838a48 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,3 +1,6 @@ inherit_gem: rubocop-discourse: default.yml +AllCops: + Exclude: + - pups.gemspec 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..ad27b73 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,17 @@ def initialize( ) @params = @config["params"] + if extra_params + extra_params.each do |val| + puts 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 +60,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 +81,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 +124,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/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 From 22ac851c795c81e27293c467de7cfbbd7ff47af8 Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Mon, 7 Jul 2025 23:33:14 -0700 Subject: [PATCH 2/5] DEV: update ruby and checkout actions --- .github/workflows/ci.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 From d535cca8215ad5511ef2c8cc34b96a3bc09de8fa Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Mon, 7 Jul 2025 23:37:44 -0700 Subject: [PATCH 3/5] DEV: exclude pups.gemspec from linting on cli --- .github/workflows/ci.yml | 2 +- .rubocop.yml | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 02bec49..8be694e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - - run: bundle exec rubocop + - run: bundle exec rubocop --force-exclusion pups.gemspec test: name: "pups tests" runs-on: ${{ matrix.os }} diff --git a/.rubocop.yml b/.rubocop.yml index 3838a48..d46296c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,2 @@ inherit_gem: rubocop-discourse: default.yml - -AllCops: - Exclude: - - pups.gemspec From fc116b48074ac55fd3c2fcd5ac02d0016db0e19c Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Mon, 7 Jul 2025 23:42:10 -0700 Subject: [PATCH 4/5] DEV: pups.gemspec linting --- .github/workflows/ci.yml | 2 +- pups.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8be694e..02bec49 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - - run: bundle exec rubocop --force-exclusion pups.gemspec + - run: bundle exec rubocop test: name: "pups tests" runs-on: ${{ matrix.os }} 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| From 64ee19a87e72739a67c19997bd1501e382a60018 Mon Sep 17 00:00:00 2001 From: Jeff Wong Date: Tue, 8 Jul 2025 08:03:32 -0700 Subject: [PATCH 5/5] remove debug puts --- lib/pups/config.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pups/config.rb b/lib/pups/config.rb index ad27b73..9bfcdfd 100644 --- a/lib/pups/config.rb +++ b/lib/pups/config.rb @@ -44,7 +44,6 @@ def initialize( @params = @config["params"] if extra_params extra_params.each do |val| - puts val key_val = val.split("=", 2) if key_val.length == 2 @params[key_val[0]] = key_val[1]