Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
inherit_gem:
rubocop-discourse: default.yml

20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <arguments output>`. Specifically, pups
Expand Down
11 changes: 9 additions & 2 deletions lib/pups/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def self.opts
Array,
"Run all but listed tagged commands."
)
opts.on(
"--params <param(s)>",
Array,
"Replace params in the config with params listed."
)
opts.on("-h", "--help") do
puts opts
exit
Expand Down Expand Up @@ -69,15 +74,17 @@ 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 =
Pups::Config.load_file(
input_file,
options[:ignore],
tags: options[:tags],
skip_tags: options[:"skip-tags"]
skip_tags: options[:"skip-tags"],
extra_params: options[:params]
)
end

Expand Down
36 changes: 24 additions & 12 deletions lib/pups/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -41,21 +42,33 @@ 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

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}"
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/pups/exec_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/pups/file_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ def initialize
@type = :bash
end

attr_writer :params

def run
path = interpolate_params(@path)

Expand Down
2 changes: 1 addition & 1 deletion pups.gemspec
Original file line number Diff line number Diff line change
@@ -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|
Expand Down
24 changes: 24 additions & 0 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
10 changes: 10 additions & 0 deletions test/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading