moss

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit d9eb281f1ffed4178818b72bcb8cdcdcda5251b3
parent cbf2d07dc01cd7672bfe5373b203f6021567f442
Author: Daniel Barlow <dan@telent.net>
Date:   Thu, 12 Aug 2021 23:32:40 +0100

support command aliases

Diffstat:
Mfeatures/moss.feature | 2+-
Mfeatures/step_definitions/steps.rb | 2+-
Mmoss.rb | 33++++++++++++++++++++++-----------
Mspec/cli_spec.rb | 6++++++
4 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/features/moss.feature b/features/moss.feature @@ -117,5 +117,5 @@ Feature: command-line password management And I see the string "add a secret to the store" Scenario: argument checking - When I run "moss cat" + When I run "moss show" Then it complains that the wrong arguments were given diff --git a/features/step_definitions/steps.rb b/features/step_definitions/steps.rb @@ -199,5 +199,5 @@ end Then("it complains that the wrong arguments were given") do expect(@exit_status).to be > 0 - expect(@i_see).to match /usage: moss cat \<file\>/ + expect(@i_see).to match /usage: moss show \<file\>/ end diff --git a/moss.rb b/moss.rb @@ -171,7 +171,11 @@ class CLI @@commands = {} def command(command_name, docstring, &blk) define_method command_name, &blk - @@commands[command_name] = { doc: docstring } + @@commands[command_name] = { name: command_name, doc: docstring } + end + def link(ends) + dest, src = ends.first + @@commands[dest] = { alias: src } end end @@ -179,7 +183,11 @@ class CLI name = argv.first.to_sym args = argv.drop(1) raise NoMethodError unless @@commands.key?(name) - arg_signature = method(name).parameters + command = @@commands[name] + if dst = command[:alias] + command = @@commands[dst] + end + arg_signature = method(command[:name]).parameters return [ name, [] ] if arg_signature.empty? @@ -200,7 +208,7 @@ class CLI m end } - [ name, + [ command[:name], payload.respond_to?(:merge) ? flags.merge(payload) : payload ] end @@ -239,10 +247,13 @@ class CLI def usage command_texts = @@commands.map { |name, command| - # unless name == command[:name] # skip aliases - sprintf " %-40s - %s", - describe_usage(name), - command[:doc] + if command[:name] + sprintf " %-40s - %s", + describe_usage(name), + command[:doc] + else + "" + end } usage_header + command_texts.join("\n") @@ -267,12 +278,12 @@ def cli secret = STDIN.read MOSS.write_secret(filename, secret) end -# aka :add, :insert + link :insert => :add command :show, "display a secret" do |file:| STDOUT.write(MOSS.read_secret(file)) end -# aka :show, :cat + link :cat => :show command :search, "search secrets with names matching term" do |*term| files = MOSS.secrets.filter {|f| @@ -282,7 +293,7 @@ def cli puts n end end -# aka :search, :list + link :list => :search command :edit, "edit a secret" do |file:| content = MOSS.read_secret(file) @@ -308,7 +319,7 @@ def cli end command :git, "perform git operation in store" do |* git_command| - MOSS.git_operation(parameters) + MOSS.git_operation(git_command) end command :help, "display this help text" do diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb @@ -16,6 +16,7 @@ class <<cli command :encrypt, "scramble a file" do |file:, signed: false| # end + link :scramble => :encrypt command :mail, "send email" do |rcpt:, subject: "(no subject)"| "/usr/lib/sendmail #{rcpt} -s #{subject.inspect}" end @@ -62,6 +63,11 @@ RSpec.describe CLI do expect(cli.parse_arguments %w(help)). to match([:help, []]) end + + it "processes aliases" do + expect(cli.parse_arguments ["scramble", "foo.txt"]). + to match([:encrypt, { file: "foo.txt" }]) + end end describe "dispatching" do