2019-09-22 02:30:03 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require_relative File.join("dist", "pigeon")
|
|
|
|
|
|
|
|
require "thor"
|
|
|
|
|
|
|
|
module Pigeon
|
|
|
|
class Identity < Thor
|
2019-09-23 01:07:14 +00:00
|
|
|
class ConfigAlreadyExists < StandardError; end
|
2019-09-22 02:30:03 +00:00
|
|
|
|
|
|
|
desc "new", "Creates a new identiy in `.pigeon` directory if none exists"
|
|
|
|
|
|
|
|
def new
|
2019-12-03 03:18:44 +00:00
|
|
|
if File.file?(Pigeon::PIGEON_DB_PATH)
|
2019-12-02 02:45:28 +00:00
|
|
|
puts "Pigeon has detected a `pigeon.db` file.
|
2019-09-22 02:30:03 +00:00
|
|
|
Refusing to overwrite existing Pigeon config.
|
2019-09-23 01:07:14 +00:00
|
|
|
Remove config dir or switch to a different directory."
|
|
|
|
raise ConfigAlreadyExists
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
kp = Pigeon::KeyPair.new()
|
|
|
|
kp.save!
|
|
|
|
puts kp.public_key
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "show", "Prints a base64 identiy string to STDOUT"
|
|
|
|
|
|
|
|
def show
|
2019-10-12 20:25:17 +00:00
|
|
|
puts Pigeon::KeyPair.current.public_key
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Blob < Thor
|
2019-09-23 01:07:14 +00:00
|
|
|
desc "set", "Copy arbitrary binary data into the database"
|
2019-09-22 02:30:03 +00:00
|
|
|
|
2019-09-22 17:34:08 +00:00
|
|
|
def set(data = "")
|
|
|
|
blob = (data != "") ? data : STDIN.read
|
|
|
|
puts Pigeon::Storage.current.set_blob(blob)
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
|
2019-09-23 01:07:14 +00:00
|
|
|
desc "get", "Read arbitrary data from the database"
|
2019-09-22 02:30:03 +00:00
|
|
|
|
2019-09-22 11:00:19 +00:00
|
|
|
def get(hex_digest)
|
|
|
|
puts Pigeon::Storage.current.get_blob(hex_digest)
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-23 01:07:14 +00:00
|
|
|
class Peer < Thor
|
|
|
|
desc "add", "Begin following a Pigeon peer"
|
|
|
|
|
|
|
|
def add(identity)
|
2019-09-25 01:19:25 +00:00
|
|
|
puts Pigeon::Storage.current.add_peer(identity)
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "remove", "Stop following a Pigeon peer"
|
|
|
|
|
|
|
|
def remove(identity)
|
2019-09-25 01:19:25 +00:00
|
|
|
puts Pigeon::Storage.current.remove_peer(identity)
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "block", "Stop following a Pigeon peer AND refuse to replicate"
|
|
|
|
|
|
|
|
def block(identity)
|
2019-09-25 01:19:25 +00:00
|
|
|
puts Pigeon::Storage.current.block_peer(identity)
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "all", "List all Pigeon peers"
|
|
|
|
|
|
|
|
def all
|
|
|
|
puts Pigeon::Storage.current.all_peers
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 19:43:27 +00:00
|
|
|
class PigeonMessage < Thor
|
|
|
|
desc "create", "Begin a new Pigeon message"
|
|
|
|
|
|
|
|
def create(kind)
|
2020-03-05 13:59:11 +00:00
|
|
|
puts Pigeon::Draft.create(kind: kind).render
|
2019-10-12 19:43:27 +00:00
|
|
|
end
|
2019-10-19 20:29:29 +00:00
|
|
|
|
|
|
|
desc "append", "Add a key/value pair to the current DRAFT"
|
|
|
|
|
|
|
|
def append(key, raw_value = "")
|
|
|
|
v = (raw_value != "") ? raw_value : STDIN.read
|
2020-03-05 13:59:11 +00:00
|
|
|
puts Pigeon::Draft.current[key] = v
|
2019-10-19 20:29:29 +00:00
|
|
|
end
|
2019-10-19 20:48:17 +00:00
|
|
|
|
|
|
|
desc "show", "Print a message to STDOUT. If message_id is missing, current draft will be displayed."
|
|
|
|
|
|
|
|
def show(message_id = "")
|
|
|
|
if message_id == ""
|
2020-03-05 13:59:11 +00:00
|
|
|
puts Pigeon::Draft.current.render
|
2019-10-19 20:48:17 +00:00
|
|
|
else
|
|
|
|
raise "TODO: Find message by ID?"
|
|
|
|
end
|
|
|
|
end
|
2019-10-20 03:43:21 +00:00
|
|
|
|
|
|
|
desc "sign", "Commit current DRAFT to local feed."
|
|
|
|
|
|
|
|
def sign
|
2020-03-07 15:38:34 +00:00
|
|
|
draft = Pigeon::Draft.current
|
|
|
|
message = Pigeon::Message.from_draft(draft)
|
|
|
|
puts message.render
|
2019-10-20 03:43:21 +00:00
|
|
|
end
|
2019-10-12 19:43:27 +00:00
|
|
|
end
|
|
|
|
|
2019-09-22 02:30:03 +00:00
|
|
|
class CLI < Thor
|
|
|
|
desc "status", "Show various information about the `.pigeon` directory"
|
|
|
|
|
|
|
|
def status
|
2019-09-25 01:54:32 +00:00
|
|
|
puts "
|
2020-03-07 15:38:34 +00:00
|
|
|
-`. Pigeon Protocol Ruby Client
|
|
|
|
'( @ > Version: #{Pigeon::VERSION}
|
|
|
|
_) ( Peers: #{Pigeon::Storage.current.all_peers.count}
|
|
|
|
/ ) Blocked: #{Pigeon::Storage.current.all_blocks.count}
|
|
|
|
/_,' / Logs: #{Pigeon::Storage.current.message_count}
|
|
|
|
\ /
|
|
|
|
===m" "m===
|
2019-09-25 01:54:32 +00:00
|
|
|
"
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "identity SUBCOMMAND ...ARGS", "Manage `.pigeon` identity"
|
|
|
|
subcommand "identity", Identity
|
|
|
|
|
|
|
|
desc "blob SUBCOMMAND ...ARGS", "Manage blob storage"
|
|
|
|
subcommand "blob", Blob
|
2019-09-23 01:07:14 +00:00
|
|
|
|
|
|
|
desc "peer SUBCOMMAND ...ARGS", "Manage blob storage"
|
|
|
|
subcommand "peer", Peer
|
2019-10-12 19:43:27 +00:00
|
|
|
|
|
|
|
desc "message SUBCOMMAND ...ARGS", "Manage messages"
|
|
|
|
subcommand "message", PigeonMessage
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Pigeon::CLI.start(ARGV)
|