Module: Agent::Lock::CLI

Defined in:
lib/agent/lock/cli.rb,
lib/agent/lock/cli.rb,
lib/agent/lock/cli/commands/base.rb,
lib/agent/lock/cli/commands/list.rb,
lib/agent/lock/cli/commands/mine.rb,
lib/agent/lock/cli/commands/note.rb,
lib/agent/lock/cli/commands/show.rb,
lib/agent/lock/cli/commands/break.rb,
lib/agent/lock/cli/commands/check.rb,
lib/agent/lock/cli/commands/resume.rb,
lib/agent/lock/cli/commands/skills.rb,
lib/agent/lock/cli/commands/whoami.rb,
lib/agent/lock/cli/commands/acquire.rb,
lib/agent/lock/cli/commands/listing.rb,
lib/agent/lock/cli/commands/release.rb,
lib/agent/lock/cli/commands/version.rb,
lib/agent/lock/cli/commands/completion.rb,
lib/agent/lock/cli/commands/release_all.rb,
lib/agent/lock/cli/commands/tree_command.rb,
lib/agent/lock/cli/commands/switch_backend.rb

Overview

The command line, and nothing else. Every command here is a thin shell: parse flags, call one Manager method, print the result, set an exit code. No command decides anything a library object could decide instead.

Defined Under Namespace

Modules: Commands

Constant Summary collapse

PROGRAM_NAME =

What the gem is installed as. Taken from the gemspec's one executable rather than from $PROGRAM_NAME, which under a test runner is the runner. The completion script it emits names the program in every line, so guessing it wrong is not a cosmetic mistake.

"alock"
COLORS =

Colours the help text. Pastel honours NO_COLOR and a non-terminal stdout, so piped help and the specs read plain text.

Pastel.new
TITLE =

The name in the left cell of the help's title box.

"    Agent/Lock — Distributed Advisory Locker"
TITLE_CELLS =

Inner widths of the title box's two cells. The version is padded to the right one rather than spaced by hand, so 4.10.0 still closes the box.

[45, 33].freeze
HELP_WIDTH =

The column dry-cli-help wraps at, rather than the terminal's width, so help reads the same in a wide terminal, a narrow one, and a log.

85
DESCRIPTION =

What alock -h says before it lists the commands.

<<~TEXT.freeze
  Advisory locks for AI coding agents that share a checkout, or a branch
  across worktrees and optionally across machines. Claim the file, directory
  or glob you are about to write to with `acquire`; any other agent that claims
  or checks an overlapping scope is refused and told who holds it, since when,
  and why. Locks are advisory only: #{COLORS.blue("they protect agents from working on the same files as other agents that also check for locks")}.

  Sub-agents of one session share its process, so each must name itself on
  every call: #{COLORS.blue("AGENT_ID=<name> alock acquire ...")} Run `alock whoami` to see the
  name your locks would carry.

  Backends: Redis when one answers on env REDIS_URL (shared across machines),
  or 127.0.0.1:6379 (local only). Otherwise a file store inside .git (also
  local only). The first process in a checkout records the choice. To choose it yourself,
  pass #{COLORS.blue("--locking-strategy redis|filesystem")} (#{COLORS.blue("--redis-url URL")} names the one Redis
  to use), or set #{COLORS.blue("AGENT_LOCK_BACKEND=redis|file")}.

  Exit codes: 0 done, 1 refused or held by someone else, 2 could not run.

  If you are using the gem from a Ruby application, you should default to the Ruby
  API. Every CLI command is available via Ruby API, as described in the README:
  #{COLORS.cyan.bold.underline("https://github.com/kigster/agent-lock#ruby-api")}
TEXT
COMMANDS =

Every command but completion, which needs the finished registry. Each entry maps the name typed to its class and its aliases.

Returns:

  • (Hash{String => Array(Class, Array<String>)})
{
  "acquire" => [Commands::Acquire, []],
  "release" => [Commands::Release, []],
  "check" => [Commands::Check, []],
  "list" => [Commands::List, ["ls"]],
  "show repos" => [Commands::ShowRepos, []],
  "show repo" => [Commands::ShowRepo, []],
  "mine" => [Commands::Mine, []],
  "release-all" => [Commands::ReleaseAll, []],
  "break" => [Commands::Break, []],
  "note" => [Commands::Note, []],
  "resume" => [Commands::Resume, []],
  "switch-backend" => [Commands::SwitchBackend, []],
  "whoami" => [Commands::Whoami, []],
  "skills path" => [Commands::SkillsPath, []],
  "skills install" => [Commands::SkillsInstall, []],
  "version" => [Commands::Version, %w[-v --version]]
}.freeze

Class Method Summary collapse

Class Method Details

.registry_for(launcher) ⇒ Dry::CLI::Registry

A registry whose commands are already bound to this launcher, so a command writes to the streams it was given rather than to the process's.

Parameters:

Returns:

  • (Dry::CLI::Registry)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/agent/lock/cli.rb', line 85

def self.registry_for(launcher)
  # `extend` and `register` have to be sent to the new class explicitly.
  # Inside a `tap` block `self` is still this module, so writing them
  # bare turned Agent::Lock::CLI itself into the registry, handed back a
  # class that knew no commands, and left the completion command bound
  # to that empty class.
  registry = Class.new { extend Dry::CLI::Registry }

  COMMANDS.each do |name, (klass, aliases)|
    registry.register(name, klass.new(launcher), aliases: aliases)
  end

  registry.register("completion", Commands::Completion[registry, program_name: PROGRAM_NAME])
  registry
end

.title_box(version = Agent::Lock::VERSION) ⇒ String

The three lines alock -h opens with: the gem's name and its version, boxed.

Parameters:

  • version (String) (defaults to: Agent::Lock::VERSION)

Returns:

  • (String)


35
36
37
38
39
40
41
42
# File 'lib/agent/lock/cli.rb', line 35

def self.title_box(version = Agent::Lock::VERSION)
  left, right = TITLE_CELLS
  rule = ->(open, middle, close) { "#{open}#{"━" * left}#{middle}#{"━" * right}#{close}" }
  name = COLORS.yellow(TITLE.ljust(left))
  release = COLORS.green.bold(" Version #{version}".ljust(right))

  [rule.call("┏", "┳", "┓"), "┃#{name}┃#{release}┃", rule.call("┗", "┻", "┛")].join("\n")
end