diff --git a/dist/pigeon/draft.rb b/dist/pigeon/draft.rb index ee34f0c..a69da93 100644 --- a/dist/pigeon/draft.rb +++ b/dist/pigeon/draft.rb @@ -9,7 +9,7 @@ module Pigeon end def self.current - Pigeon::Storage.current.get_config(CURRENT_DRAFT) + Pigeon::Storage.current.get_config(CURRENT_DRAFT) or raise "NO DRAFT FOUND" end def self.reset_current diff --git a/dist/pigeon/storage.rb b/dist/pigeon/storage.rb index 4f122c9..92b9a10 100644 --- a/dist/pigeon/storage.rb +++ b/dist/pigeon/storage.rb @@ -27,6 +27,7 @@ module Pigeon def save_message(msg) store.transaction do insert_and_update_index(msg) + msg end end @@ -49,18 +50,20 @@ module Pigeon end def set_blob(data) - hex_digest = Digest::SHA256.hexdigest(data) + raw_digest = Digest::SHA256.hexdigest(data) + b64_digest = Base64.urlsafe_encode64(raw_digest) + multihash = [BLOB_SIGIL, b64_digest, BLOB_FOOTER].join("") + store.transaction do - store[BLOB_NS][hex_digest] = data + store[BLOB_NS][multihash] = data end - puts "TODO: Maybe this needs to be URLsafe base 64: " - [BLOB_SIGIL, hex_digest, BLOB_FOOTER].join("") + + multihash end - def get_blob(hex_digest) - hd = hex_digest.gsub(BLOB_SIGIL, "").gsub(BLOB_FOOTER, "") + def get_blob(blob_multihash) store.transaction(true) do - store[BLOB_NS][hd] + store[BLOB_NS][blob_multihash] end end @@ -101,25 +104,26 @@ module Pigeon end def reset_defaults - @store.transaction do - @store[DEPTH_INDEX_NS] = {} - @store[BLOB_NS] = {} - @store[CONF_NS] = {} - @store[MESG_NS] = {} - @store[BLCK_NS] = Set.new - @store[PEER_NS] = Set.new + store.transaction do + store[DEPTH_INDEX_NS] = {} + store[BLOB_NS] = {} + store[CONF_NS] = {} + store[MESG_NS] = {} + store[BLCK_NS] = Set.new + store[PEER_NS] = Set.new end - @store + store end private def store - unless @store + if @store + return @store + else @store = PStore.new(PIGEON_DB_PATH) reset_defaults end - return @store end def insert_and_update_index(message) diff --git a/pigeon-cli b/pigeon-cli index 953789c..d9464ee 100755 --- a/pigeon-cli +++ b/pigeon-cli @@ -4,6 +4,11 @@ require_relative File.join("dist", "pigeon") require "thor" +def bail(msg) + $stderr.puts msg + exit 1 +end + module Pigeon class Identity < Thor class ConfigAlreadyExists < StandardError; end @@ -81,7 +86,12 @@ module Pigeon def append(key, raw_value = "") v = (raw_value != "") ? raw_value : STDIN.read - puts Pigeon::Draft.current[key] = v + draft = Pigeon::Draft.current + if draft + puts draft[key] = v + else + bail("You must create a draft first") + end end desc "show", "Print a message to STDOUT. If message_id is missing, current draft will be displayed." @@ -90,7 +100,7 @@ module Pigeon if message_id == "" puts Pigeon::Draft.current.render else - raise "TODO: Find message by ID?" + bail("You must create a draft first") end end @@ -98,7 +108,7 @@ module Pigeon def sign draft = Pigeon::Draft.current - message = Pigeon::Message.from_draft(draft) + message = Pigeon::Message.publish(draft) puts message.render end end diff --git a/scratchpad.sh b/scratchpad.sh index 28bc77f..25bb6da 100755 --- a/scratchpad.sh +++ b/scratchpad.sh @@ -46,35 +46,35 @@ echo "...string via pipe" echo "my_value" | ./pigeon-cli message append key1 -echo "...string with no quotes" -./pigeon-cli message append key2 my_value2 +# echo "...string with no quotes" +# ./pigeon-cli message append key2 my_value2 -echo "...string with quotes" -./pigeon-cli message append key3 "my_value3" +# echo "...string with quotes" +# ./pigeon-cli message append key3 "my_value3" -echo "...message ID" -./pigeon-cli message append key4 \%jvKh9yoiEJaePzoWCF1nnqpIlPgTk9FHEtqczQbvzGM=.sha256 +# echo "...message ID" +# ./pigeon-cli message append key4 \%jvKh9yoiEJaePzoWCF1nnqpIlPgTk9FHEtqczQbvzGM=.sha256 -echo "...blob" -./pigeon-cli message append key5 \&29f3933302c49c60841d7620886ce54afc68630242aee6ff683926d2465e6ca3.sha256 +# echo "...blob" +# ./pigeon-cli message append key5 \&29f3933302c49c60841d7620886ce54afc68630242aee6ff683926d2465e6ca3.sha256 -echo "...identity" -./pigeon-cli message append key6 \@galdahnB3L2DE2cTU0Me54IpIUKVEgKmBwvZVtWJccg=.ed25519 +# echo "...identity" +# ./pigeon-cli message append key6 \@galdahnB3L2DE2cTU0Me54IpIUKVEgKmBwvZVtWJccg=.ed25519 -echo "== show draft message" -./pigeon-cli message show +# echo "== show draft message" +# ./pigeon-cli message show -echo "== sign (publish, save, commit, etc) draft message" -./pigeon-cli message sign +# echo "== sign (publish, save, commit, etc) draft message" +# ./pigeon-cli message sign -echo "=== add a second message to the db" -./pigeon-cli message create second_test +# echo "=== add a second message to the db" +# ./pigeon-cli message create second_test -echo "=== append hello:'world' to message:" -./pigeon-cli message append hello "world" +# echo "=== append hello:'world' to message:" +# ./pigeon-cli message append hello "world" -echo "=== Sign message #2" -./pigeon-cli message sign +# echo "=== Sign message #2" +# ./pigeon-cli message sign -echo "=== getting status:" -./pigeon-cli status +# echo "=== getting status:" +# ./pigeon-cli status diff --git a/spec/pigeon/draft_spec.rb b/spec/pigeon/draft_spec.rb index 0e66486..b51f0af 100644 --- a/spec/pigeon/draft_spec.rb +++ b/spec/pigeon/draft_spec.rb @@ -20,15 +20,14 @@ RSpec.describe Pigeon::Draft do "prev DRAFT", "depth DRAFT", "\na:\"bar\"", - "b:&6462a5f5174b53702fc25afe67a8f9a29f572610a65bafefff627531552f096f.sha256", - "\n" + "b:&NjQ2MmE1ZjUxNzRiNTM3MDJmYzI1YWZlNjdhOGY5YTI5ZjU3MjYxMGE2NWJhZmVmZmY2Mjc1MzE1NTJmMDk2Zg==.sha256", + "\n", ].join("\n") it "renders a message" do pk = Pigeon::KeyPair.current.public_key actual = message.render expected = MSG.gsub("___", pk) - expect(actual).to start_with(expected) end @@ -40,7 +39,7 @@ RSpec.describe Pigeon::Draft do body: { "a" => "bar".to_json, "b" => hash, - } + }, } message["a"] = "bar" message["b"] = hash diff --git a/spec/pigeon/message_spec.rb b/spec/pigeon/message_spec.rb index 71729c9..26262ab 100644 --- a/spec/pigeon/message_spec.rb +++ b/spec/pigeon/message_spec.rb @@ -30,7 +30,7 @@ RSpec.describe Pigeon::Message do it "discards a draft after signing" do expect(draft.internal_id).to eq(Pigeon::Draft.current.internal_id) Pigeon::Message.publish(draft) - expect(Pigeon::Draft.current).to be nil + expect { Pigeon::Draft.current }.to raise_error("NO DRAFT FOUND") end it "creates a single message" do @@ -50,7 +50,7 @@ RSpec.describe Pigeon::Message do "depth 0", "", "a:\"bar\"", - "b:&6462a5f5174b53702fc25afe67a8f9a29f572610a65bafefff627531552f096f.sha256", + "b:&NjQ2MmE1ZjUxNzRiNTM3MDJmYzI1YWZlNjdhOGY5YTI5ZjU3MjYxMGE2NWJhZmVmZmY2Mjc1MzE1NTJmMDk2Zg==.sha256", "", "signature __SIGNATURE__", ].join("\n")