moss

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

commit c008e548b1c547717db9fd949de9855885477bff
parent b12ac0ff186c4a8b3eb96770645f2b0f37108059
Author: Daniel Barlow <dan@telent.net>
Date:   Mon,  9 Aug 2021 22:57:19 +0100

add "moss init" and "moss git"

Diffstat:
Mfeatures/step_definitions/steps.rb | 22+++++++++++++++++++++-
Mmoss.rb | 26+++++++++++++++++++++-----
2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/features/step_definitions/steps.rb b/features/step_definitions/steps.rb @@ -143,4 +143,24 @@ Then("the store directory is under XDG_DATA_HOME") do store = result["store"] expect(store).to start_with(ENV["XDG_DATA_HOME"]) end - + +When("I create a moss instance with identity {string}") do |keyfile| + shell "#{MOSS} init fixtures/keys/#{keyfile}" +end + +Then("the instance store exists") do + expect(Pathname.new(ENV['MOSS_STORE'])).to be_directory +end + +Then("the instance identity is {string}") do |keyfile| + expect(store_path("../identity").read).to eq File.read("fixtures/keys/#{keyfile}") +end + +Then("the store root contains .recipients for the identity {string}") do |keyfile| + expected = `age-keygen -y fixtures/keys/#{keyfile.inspect}` + expect(store_path(".recipients").read).to eq expected +end + +Then("I can run git {string}") do |command| + @i_see = shell "#{MOSS} git #{command}" +end diff --git a/moss.rb b/moss.rb @@ -8,10 +8,13 @@ def xdg_data_home end STORE = Pathname.new(ENV['MOSS_STORE'] || "#{xdg_data_home}/moss/store") -IDENTITY_FILE = ENV.fetch('MOSS_IDENTITY_FILE', "#{xdg_data_home}/moss/identity") -File.exist?(IDENTITY_FILE) or - raise "missing identity file (private key) at #{IDENTITY_FILE}, use age-keygen to create" +def identity_file + value = ENV.fetch('MOSS_IDENTITY_FILE', "#{xdg_data_home}/moss/identity") + File.exist?(value) or + raise "missing identity file (private key) at #{IDENTITY_FILE}, use age-keygen to create" + value +end def git_managed? STORE.join(".git").exist? @@ -66,7 +69,7 @@ end def read_secret(name) pathname = STORE.join("#{name}.age") File.exist?(pathname) or raise "Can't open #{pathname}: $!" - `age -i #{IDENTITY_FILE} -d #{pathname}` + `age -i #{identity_file} -d #{pathname}` end action, *parameters = ARGV @@ -110,10 +113,23 @@ when 'search','list' when 'config' config = { store: STORE.to_s, - identity_file: IDENTITY_FILE.to_s, + identity_file: identity_file.to_s, git: git_managed? } puts JSON.generate(config) +when 'init' + keyfile = Pathname.new(parameters.first) + keyfile.exist? or raise "Cannot read identity at #{keyfile}" + keyfile.read.match(/AGE-SECRET-KEY-1/) or + raise "#{keyfile} does not appear to be an age identity" + STORE.mkpath + FileUtils.cp(keyfile, STORE.parent.join("identity")) + File.open(STORE.join(".recipients"), "w") do |f| + f.write `age-keygen -y #{keyfile.to_s.inspect}` + end +when 'git' + Kernel.system("/usr/bin/env", "git", *parameters, {chdir: STORE}) + else raise "command #{action} unrecognized" end