Ability to append to messages

This commit is contained in:
Netscape Navigator 2019-10-19 15:29:29 -05:00
parent 3f1f2d6223
commit 6c04082b85
3 changed files with 55 additions and 7 deletions

View File

@ -24,17 +24,35 @@ module Pigeon
def self.create(kind:, prev: nil, body: {})
# instantiate
msg = self.new(author: KeyPair.current.public_key,
kind: kind,
prev: prev,
body: body)
# Save to disk as HEAD
Pigeon::Storage.current.set_config(NAME_OF_DRAFT, Marshal.dump(msg))
msg
self.new(author: KeyPair.current.public_key,
kind: kind,
prev: prev,
body: body).save
end
def self.current
@current ||= Marshal.load(Pigeon::Storage.current.get_config(NAME_OF_DRAFT))
end
def save
Pigeon::Storage.current.set_config(NAME_OF_DRAFT, Marshal.dump(self))
self
end
def serialize
Template.new(self).render
end
def append(key, value)
# TODO: Sanitize, validate inputs.
case value[0]
when "%", "@", "&", "\"" # TODO: Use constants, not literals.
self.body[key] = value
else
self.body[key] = value.inspect
end
self.save
return self.body[key]
end
end
end

View File

@ -78,6 +78,13 @@ module Pigeon
def create(kind)
puts Pigeon::Message.create(kind: kind).serialize
end
desc "append", "Add a key/value pair to the current DRAFT"
def append(key, raw_value = "")
v = (raw_value != "") ? raw_value : STDIN.read
puts Pigeon::Message.current.append(key, v)
end
end
class CLI < Thor

View File

@ -40,5 +40,28 @@ echo "listing all peers:"
echo "Making a new `scratch_pad` log entry"
./pigeon-cli message create scratch_pad
echo "Appending values..."
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 quotes"
./pigeon-cli message append key3 "my_value3"
echo "...message ID"
./pigeon-cli message append key4 \%jvKh9yoiEJaePzoWCF1nnqpIlPgTk9FHEtqczQbvzGM=.sha256
echo "...blob"
./pigeon-cli message append key5 \&29f3933302c49c60841d7620886ce54afc68630242aee6ff683926d2465e6ca3.sha256
echo "...identity"
./pigeon-cli message append key6 \@galdahnB3L2DE2cTU0Me54IpIUKVEgKmBwvZVtWJccg=.ed25519
echo "show current message"
./pigeon-cli message show
echo "getting status:"
./pigeon-cli status