2019-09-22 02:30:03 +00:00
|
|
|
#!/usr/bin/env ruby
|
2020-04-19 22:37:29 +00:00
|
|
|
require_relative "../lib/pigeon"
|
2019-09-22 02:30:03 +00:00
|
|
|
require "thor"
|
|
|
|
|
2020-04-18 15:51:08 +00:00
|
|
|
def db
|
|
|
|
if File.file?(Pigeon::PIGEON_DB_PATH)
|
|
|
|
$db ||= Pigeon::Database.new
|
|
|
|
else
|
2020-04-25 15:11:25 +00:00
|
|
|
warn("You must first run `pigeon-cli identity new`.")
|
2020-04-18 18:18:00 +00:00
|
|
|
exit 1
|
2020-04-18 15:51:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-14 02:59:13 +00:00
|
|
|
def bail(msg)
|
2020-04-25 15:11:25 +00:00
|
|
|
warn msg
|
2020-03-14 02:59:13 +00:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2019-09-22 02:30:03 +00:00
|
|
|
module Pigeon
|
2020-03-15 17:32:41 +00:00
|
|
|
class ThorBase < Thor
|
|
|
|
no_commands do
|
|
|
|
def exit_on_failure?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Identity < ThorBase
|
2019-09-23 01:07:14 +00:00
|
|
|
class ConfigAlreadyExists < StandardError; end
|
2019-09-22 02:30:03 +00:00
|
|
|
|
2020-04-22 13:44:12 +00:00
|
|
|
desc "new", "Creates a new identiy in `.pgn` directory if none exists"
|
2019-09-22 02:30:03 +00:00
|
|
|
|
|
|
|
def new
|
2019-12-03 03:18:44 +00:00
|
|
|
if File.file?(Pigeon::PIGEON_DB_PATH)
|
2020-04-18 15:51:08 +00:00
|
|
|
puts <<-END
|
|
|
|
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."
|
2020-04-18 15:51:08 +00:00
|
|
|
END
|
2019-09-23 01:07:14 +00:00
|
|
|
raise ConfigAlreadyExists
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
2020-04-18 15:51:08 +00:00
|
|
|
$db = Pigeon::Database.new
|
2020-04-23 12:21:00 +00:00
|
|
|
puts db.who_am_i.multihash
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
|
2020-04-06 00:38:37 +00:00
|
|
|
desc "show", "Prints your identiy string to STDOUT"
|
2019-09-22 02:30:03 +00:00
|
|
|
|
|
|
|
def show
|
2020-04-23 12:21:00 +00:00
|
|
|
puts db.who_am_i.multihash
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-15 17:32:41 +00:00
|
|
|
class Blob < ThorBase
|
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 = "")
|
2020-04-25 15:11:25 +00:00
|
|
|
blob = data != "" ? data : STDIN.read
|
2020-04-23 12:21:00 +00:00
|
|
|
puts db.add_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)
|
2020-04-18 15:51:08 +00:00
|
|
|
puts db.get_blob(hex_digest)
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-15 17:32:41 +00:00
|
|
|
class Peer < ThorBase
|
2019-09-23 01:07:14 +00:00
|
|
|
desc "add", "Begin following a Pigeon peer"
|
|
|
|
|
|
|
|
def add(identity)
|
2020-04-18 15:51:08 +00:00
|
|
|
puts db.add_peer(identity)
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "remove", "Stop following a Pigeon peer"
|
|
|
|
|
|
|
|
def remove(identity)
|
2020-04-18 15:51:08 +00:00
|
|
|
puts db.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)
|
2020-04-18 15:51:08 +00:00
|
|
|
puts db.block_peer(identity)
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "all", "List all Pigeon peers"
|
|
|
|
|
|
|
|
def all
|
2020-04-18 15:51:08 +00:00
|
|
|
puts db.all_peers
|
2019-09-23 01:07:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-15 17:32:41 +00:00
|
|
|
class PigeonDraft < ThorBase
|
2019-10-12 19:43:27 +00:00
|
|
|
desc "create", "Begin a new Pigeon message"
|
|
|
|
|
|
|
|
def create(kind)
|
2020-04-19 21:27:09 +00:00
|
|
|
puts db.new_draft(kind: kind).render_as_draft
|
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 = "")
|
2020-06-12 13:37:48 +00:00
|
|
|
v = raw_value != "" ? raw_value : STDIN.read.chomp
|
2020-04-23 12:21:00 +00:00
|
|
|
if db.get_draft
|
2020-04-19 22:37:29 +00:00
|
|
|
db.update_draft(key, v)
|
2020-04-23 12:21:00 +00:00
|
|
|
puts db.get_draft.render_as_draft
|
2020-03-14 02:59:13 +00:00
|
|
|
else
|
|
|
|
bail("You must create a draft first")
|
|
|
|
end
|
2019-10-19 20:29:29 +00:00
|
|
|
end
|
2019-10-19 20:48:17 +00:00
|
|
|
|
2020-04-19 22:37:29 +00:00
|
|
|
desc "show", "Print current message to STDOUT."
|
2019-10-19 20:48:17 +00:00
|
|
|
|
2020-04-19 22:37:29 +00:00
|
|
|
def show
|
2020-04-23 12:21:00 +00:00
|
|
|
puts db.get_draft.render_as_draft
|
2019-10-19 20:48:17 +00:00
|
|
|
end
|
2019-10-20 03:43:21 +00:00
|
|
|
|
|
|
|
desc "sign", "Commit current DRAFT to local feed."
|
|
|
|
|
|
|
|
def sign
|
2020-04-23 12:21:00 +00:00
|
|
|
puts db.publish_draft(db.get_draft).render
|
2019-10-20 03:43:21 +00:00
|
|
|
end
|
2019-10-12 19:43:27 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 17:32:41 +00:00
|
|
|
class PigeonBundle < ThorBase
|
|
|
|
desc "create", "Create a pigeon bundle file"
|
|
|
|
|
|
|
|
def create(file_path = Pigeon::DEFAULT_BUNDLE_PATH)
|
2020-04-24 11:20:02 +00:00
|
|
|
db.export_bundle(file_path)
|
2020-03-15 17:32:41 +00:00
|
|
|
end
|
2020-04-18 19:51:10 +00:00
|
|
|
|
|
|
|
desc "ingest", "Ingest a pigeon bundle file"
|
|
|
|
|
|
|
|
def ingest(file_path = Pigeon::DEFAULT_BUNDLE_PATH)
|
2020-04-24 11:20:02 +00:00
|
|
|
db.import_bundle(file_path)
|
2020-04-18 19:51:10 +00:00
|
|
|
end
|
2020-03-15 17:32:41 +00:00
|
|
|
end
|
|
|
|
|
2020-03-15 18:50:00 +00:00
|
|
|
class PigeonMessage < ThorBase
|
|
|
|
desc "find", "Find a pigeon message in the local DB"
|
|
|
|
|
|
|
|
def find(multihash)
|
2020-04-18 15:51:08 +00:00
|
|
|
puts db.read_message(multihash).render
|
2020-03-15 18:50:00 +00:00
|
|
|
end
|
|
|
|
|
2020-04-19 15:16:06 +00:00
|
|
|
desc "find-all", "Find all message IDs of a particular identity."
|
2020-03-15 18:50:00 +00:00
|
|
|
|
2020-04-19 15:16:06 +00:00
|
|
|
def find_all(author = nil)
|
2020-04-23 12:21:00 +00:00
|
|
|
puts db.all_messages(author).join(Pigeon::CR) + Pigeon::CR
|
2020-03-15 18:50:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "last", "Grab your last message. INTERNAL USE ONLY"
|
|
|
|
|
|
|
|
def last
|
2020-04-23 12:21:00 +00:00
|
|
|
me = db.who_am_i
|
2020-04-18 15:51:08 +00:00
|
|
|
mcount = db.get_message_count_for(me.multihash)
|
|
|
|
multihash = db.get_message_by_depth(me.multihash, mcount - 1)
|
2020-03-15 18:50:00 +00:00
|
|
|
puts multihash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-15 17:32:41 +00:00
|
|
|
class CLI < ThorBase
|
2020-04-22 13:44:12 +00:00
|
|
|
desc "status", "Show various information about the `.pgn` directory"
|
2019-09-22 02:30:03 +00:00
|
|
|
|
|
|
|
def status
|
2020-04-23 12:21:00 +00:00
|
|
|
me = db.who_am_i.multihash
|
2020-04-19 15:16:06 +00:00
|
|
|
mine = db.get_message_count_for(me)
|
2019-09-25 01:54:32 +00:00
|
|
|
puts "
|
2020-04-19 15:16:06 +00:00
|
|
|
-`. Pigeon Protocol Ruby Client
|
|
|
|
'( @ > Version: #{Pigeon::VERSION}
|
|
|
|
_) ( Peers: #{db.all_peers.count}
|
|
|
|
/ ) Blocked: #{db.all_blocks.count}
|
|
|
|
/_,' / Msgs Published: #{mine}
|
2020-04-23 12:21:00 +00:00
|
|
|
\\ / Msgs Total: #{db.all_messages.count}
|
2020-04-19 15:16:06 +00:00
|
|
|
===m\" \"m===
|
|
|
|
Your local identity hash:
|
|
|
|
#{me}
|
2019-09-25 01:54:32 +00:00
|
|
|
"
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc "blob SUBCOMMAND ...ARGS", "Manage blob storage"
|
|
|
|
subcommand "blob", Blob
|
2019-09-23 01:07:14 +00:00
|
|
|
|
2020-03-15 18:12:26 +00:00
|
|
|
desc "bundle SUBCOMMAND ...ARGS", "Consume and create bundle files"
|
|
|
|
subcommand "bundle", PigeonBundle
|
2019-10-12 19:43:27 +00:00
|
|
|
|
2020-03-15 18:12:26 +00:00
|
|
|
desc "draft SUBCOMMAND ...ARGS", "Manage drafts"
|
2020-03-14 03:17:56 +00:00
|
|
|
subcommand "draft", PigeonDraft
|
2020-03-15 17:32:41 +00:00
|
|
|
|
2020-04-22 13:44:12 +00:00
|
|
|
desc "identity SUBCOMMAND ...ARGS", "Manage `.pgn` identity"
|
2020-03-15 18:12:26 +00:00
|
|
|
subcommand "identity", Identity
|
|
|
|
|
2020-06-12 13:37:48 +00:00
|
|
|
desc "message SUBCOMMAND ...ARGS", "Manage text-based messages"
|
2020-03-15 18:50:00 +00:00
|
|
|
subcommand "message", PigeonMessage
|
|
|
|
|
2020-06-12 13:37:48 +00:00
|
|
|
desc "peer SUBCOMMAND ...ARGS", "Manage blob (file) storage"
|
2020-03-15 18:12:26 +00:00
|
|
|
subcommand "peer", Peer
|
2019-09-22 02:30:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Pigeon::CLI.start(ARGV)
|