Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: add --tags and --skip-tags options #28

Merged
merged 6 commits into from
Oct 23, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
ruby: ["2.7"]
ruby: ["3.2"]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
ruby: ["2.7"]
ruby: ["3.2"]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.2.0 - 10-22-2023

- Add --tags and --skip-tags options

1.0.3 - 09-04-2021

- Started changelog - release to rubygems
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Usage: pups [options] [FILE|--stdin]
--stdin Read input from stdin.
--quiet Don't print any logs.
--ignore <elements> Ignore specific configuration elements, multiple elements can be provided (comma-delimited).
--tags <elements> Only run tagged commands.
--skip-tags <elements> Run all but listed tagged commands.
Useful if you want to skip over config in a pups execution.
e.g. `--ignore env,params`.
--gen-docker-run-args Output arguments from the pups configuration for input into a docker run command. All other pups config is ignored.
Expand All @@ -48,6 +50,32 @@ Running: `pups somefile.yaml` will execute the shell script resulting in a file

### Features

#### Filtering run commands by tags

The `--tags` and `skip-tags` argument allows pups to target a subset of commands listed in the somefile.yaml. To use this, you may tag your commands in the rublock. `--tags` will only run commands when commands have a matching tag. `skip-tags` will skip when commands have a matching tag.

Note, hooks from tagged commands will be present or absent depending on if the tag is filtered out or not as well. A command filtered out by targeting tag will also filter out the command's `before_` and `after_` hooks.

Example:

```
# somefile.yaml

run:
- exec:
cmd: /bin/bash -c 'echo hello >> hello'
tag: sometag
- exec:
cmd: /bin/bash -c 'echo hi >> hello'
tag: anothertag
- exec:
cmd: /bin/bash -c 'echo goodbye >> hello'
tag: thirdtag
```
Running: `pups --tags="sometag,anothertag" somefile.yaml` will not run the echo goodbye statement.

Running: `pups --skip-tags="sometag,anothertag" somefile.yaml` will ONLY run the echo goodbye statement.

#### 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
26 changes: 12 additions & 14 deletions lib/pups.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# frozen_string_literal: true

require 'logger'
require 'yaml'
require "logger"
require "yaml"

require 'pups/version'
require 'pups/config'
require 'pups/command'
require 'pups/exec_command'
require 'pups/merge_command'
require 'pups/replace_command'
require 'pups/file_command'
require 'pups/docker'
require 'pups/runit'
require "pups/version"
require "pups/config"
require "pups/command"
require "pups/exec_command"
require "pups/merge_command"
require "pups/replace_command"
require "pups/file_command"
require "pups/docker"
require "pups/runit"

module Pups
class ExecError < RuntimeError
Expand All @@ -28,9 +28,7 @@ def self.log=(logger)
end

def self.silence
if @logger
@logger.close
end
@logger.close if @logger

@logger = Logger.new(File.open(File::NULL, "w"))
end
Expand Down
62 changes: 43 additions & 19 deletions lib/pups/cli.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
# frozen_string_literal: true

require 'optparse'
require "optparse"

module Pups
class Cli
def self.opts
OptionParser.new do |opts|
opts.banner = 'Usage: pups [FILE|--stdin]'
opts.on('--stdin', 'Read input from stdin.')
opts.on('--quiet', "Don't print any logs.")
opts.on('--ignore <element(s)>', Array, "Ignore these template configuration elements, multiple elements can be provided (comma-delimited).")
opts.on('--gen-docker-run-args', 'Output arguments from the pups configuration for input into a docker run command. All other pups config is ignored.')
opts.on('-h', '--help') do
opts.banner = "Usage: pups [FILE|--stdin]"
opts.on("--stdin", "Read input from stdin.")
opts.on("--quiet", "Don't print any logs.")
opts.on(
"--ignore <element(s)>",
Array,
"Ignore these template configuration elements, multiple elements can be provided (comma-delimited)."
)
opts.on(
"--gen-docker-run-args",
"Output arguments from the pups configuration for input into a docker run command. All other pups config is ignored."
)
opts.on("--tags <tag(s)>", Array, "Only run tagged commands.")
opts.on(
"--skip-tags <tag(s)>",
Array,
"Run all but listed tagged commands."
)
opts.on("-h", "--help") do
puts opts
exit
end
Expand All @@ -26,35 +39,46 @@ def self.parse_args(args)

def self.run(args)
options = parse_args(args)
input_file = options[:stdin] ? 'stdin' : args.last
input_file = options[:stdin] ? "stdin" : args.last
unless input_file
puts opts.parse!(%w[--help])
exit
end

if options[:quiet]
Pups.silence
end
Pups.silence if options[:quiet]

Pups.log.info("Reading from #{input_file}")

if options[:stdin]
conf = $stdin.readlines.join
split = conf.split('_FILE_SEPERATOR_')
split = conf.split("_FILE_SEPERATOR_")

conf = nil
split.each do |data|
current = YAML.safe_load(data.strip)
conf = if conf
Pups::MergeCommand.deep_merge(conf, current, :merge_arrays)
else
current
end
conf =
if conf
Pups::MergeCommand.deep_merge(conf, current, :merge_arrays)
else
current
end
end

config = Pups::Config.new(conf, options[:ignore])
config =
Pups::Config.new(
conf,
options[:ignore],
tags: options[:tags],
skip_tags: option[:"skip-tags"]
)
else
config = Pups::Config.load_file(input_file, options[:ignore])
config =
Pups::Config.load_file(
input_file,
options[:ignore],
tags: options[:tags],
skip_tags: options[:"skip-tags"]
)
end

if options[:"gen-docker-run-args"]
Expand Down
6 changes: 4 additions & 2 deletions lib/pups/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ module Pups
class Command
def self.run(command, params)
case command
when String then from_str(command, params).run
when Hash then from_hash(command, params).run
when String
from_str(command, params).run
when Hash
from_hash(command, params).run
end
end

Expand Down
Loading
Loading