ops-cli
A small framework for repo-local operational CLIs. Drop the gem into a
project, write task classes under ops/tasks/, and bin/ops becomes the
single entry-point for build/deploy/console/whatever-else operational work
lives in that repo. Plain Ruby with no runtime dependencies: option parsing is
the standard library's OptionParser.
What's in the box
-
Ops::Task— base class with a small dispatch protocol (desc,task_options,call, optionalalready_done?). -
Ops::Runner+Ops::Registry— discovery + dispatch. Runner reads argv viaOps::Arguments, looks up a class from the Registry, callstask.perform, and renders the resultingOps::TaskResult. Registry walksOps::Tasks::and registers any class that descends fromOps::Task; the constant path becomes the colon-namespaced, kebab-cased command name (Ops::Tasks::Secrets::RotateMaster→secrets:rotate-master). -
Ops::TaskResult— boundary value object with three terminal states (successful,skipped,failed). -
Ops::PodExec—kubectl-based helpers (find_pod,exec_in_pod,open_in_pod) for tasks that need to land in a running pod. -
Ops::Shell—Shell.run(*cmd, display: false, stdin: nil)for side-effect commands;Shell.capture(*cmd, stdin: nil)for data-flow commands.cmdis argv (program, then arguments), optionally after a Hash of environment variables; the program is always run directly, never through a shell, so a lone string is a program name, not a command line. Both raiseOps::Shell::Erroron non-zero exit, carryingcommand,stdout,stderrandstatus; its message names the program and exit status with the tool's stderr, never the arguments or environment, which may hold secrets. -
Ops::SecretSealer— produces sealed-secret encrypted-data blobs withkubeseal. One sealer per namespace;#seal(secret_name:, values:, type: :generic)per target. It builds the Secret manifest itself and hands it to kubeseal on stdin, so plaintext never appears on a command line. Types::generic(Opaque) and:basic_auth; TLS / docker-registry would be separate sealer classes. -
exe/ops— the bin-stubbable executable.
Installation
# Gemfile
gem "ops-cli", "~> 1.0.0.pre"Until 1.0.0 is out, releases are prereleases (1.0.0.pre.N), which Bundler
only picks up when the requirement names one, as above; it also takes 1.0.0
and later 1.0.x when they come.
Then:
bundle install
bundle binstubs ops-cli # creates ./bin/ops
The gem was first published as janus-ops; require "janus_ops" still
works, as an alias for require "ops/cli".
Writing tasks
Tasks live under ops/tasks/ in the consuming repo. Each file defines a class
under Ops::Tasks:: that inherits from Ops::Task. The constant path is the
command name: each segment kebab-cased, joined with colons
(Ops::Tasks::Secrets::RotateMaster → secrets:rotate-master).
# ops/tasks/build.rb
module Ops
module Tasks
class Build < Ops::Task
desc "build [TAG]", "Build and push the application image"
def task_options
opts.option :force_rebuild, type: :boolean, default: false
end
def call
tag = opts.positional.first || "latest"
# ...use opts.force_rebuild
end
end
end
endInvoke it: bin/ops build v1.2.3 --force-rebuild.
desc records the task's usage and summary (class-level metadata; used by
bin/ops for the listing). task_options is the instance method where you
declare options on opts. call is the work method (no args — read parsed
argv via opts); its return value is wrapped into a successful
Ops::TaskResult. If call raises, the runner reports it and exits 1.
Override already_done? (default false, no args) to short-circuit into a
skipped result when the work has already been applied.
Declared options are accessible as methods on opts: opts.force_rebuild
returns the parsed value for option :force_rebuild. Positional args live at
opts.positional. Typos raise NoMethodError rather than silently returning
nil. Reserved names (the methods that already exist on Ops::TaskOptions —
parsed, positional, usage, description, etc.) raise at declaration time.
Arguments are checked before the task runs: an unrecognized flag fails the
task (exit 1) before already_done? or call, even if the task never reads
its options.
Options
opts.option NAME, **settings (or its alias opts.declare) declares one
option. The flag is the name with underscores turned into dashes:
| Setting | Effect |
|---|---|
type: :boolean |
--[no-]name; the value is true or false
|
type: :numeric |
--name=VALUE, converted with Integer()
|
no type
|
--name=VALUE, a string |
default: |
the value when the flag isn't given (otherwise nil) |
desc: |
the flag's line in bin/ops <cmd> --help
|
aliases: |
extra flag spellings, e.g. ["-n"]
|
Nested namespaces become colon names automatically:
ops/tasks/console/rails.rb → Ops::Tasks::Console::Rails → bin/ops console:rails
Repo-specific base classes
Layer your own base class on top of Ops::Task in ops/lib/ to share options
or helpers across tasks. Subclasses inherit their parent's options by calling
super inside task_options; skip super to start fresh. Plain Ruby method
override.
# ops/lib/tenant_task.rb
class TenantTask < Ops::Task
def task_options
opts.option :namespace, aliases: ["-n"]
end
end
# ops/tasks/deploy.rb
class Ops::Tasks::Deploy < TenantTask
desc "deploy NAME", "deploy a tenant"
def task_options
super # inherits :namespace
opts.option :dry_run, type: :boolean, default: false
end
def call
# opts.namespace and opts.dry_run are both available here
end
endInteractive tasks
ops-cli doesn't prompt for anything itself, but a task that needs to ask (a confirmation before something destructive, a password that mustn't echo) can use cli-ui. Add it to the consuming repo's Gemfile:
gem "cli-ui", "~> 2.7"and in the task:
require "cli/ui"
def call
return unless CLI::UI::Prompt.confirm("Remove tenant #{name}?", default: false)
password = CLI::UI::Prompt.ask_password("New password:")
# ...
endIts Prompt (ask, ask_password, confirm, ask with options:) and
Spinner primitives fit tasks well. Its framing (CLI::UI::Frame.open) and
inline {{color:...}} formatting are a bigger visual layer, best left out so
task output stays plain and readable in logs.
Where tasks are discovered
The ops/ directory in the consuming repo — containing lib/ (eager-loaded
on startup so task files can reference shared base classes) and tasks/
(lazy-walked when commands are resolved). Resolution order:
-
ENV["OPS_ROOT"](escape hatch, mostly for tests) -
Bundler.root / "ops"(the normal case — consuming app has a Gemfile) -
Dir.pwd / "ops"(fallback if Bundler isn't loaded)
bin/ops runs the Ruby under that ops/ directory, the way rake runs a
Rakefile: treat it as running the repo's code.
Top-level flags
-
bin/ops/bin/ops --help/bin/ops -T— list all commands -
bin/ops <cmd> --help(or-h, orhelp) — per-command help: the task'sdescusage and description, then its options
Development
bundle install
bundle exec rspec
bundle exec rubocop