✔️ Update CLI

This commit is contained in:
Netscape Navigator 2020-04-18 10:51:08 -05:00
parent 3dd47bfe5e
commit 4970355834
4 changed files with 44 additions and 34 deletions

View File

@ -2,16 +2,20 @@
# Pigeon Ruby # Pigeon Ruby
A WIP pigeon protocol client. This is a WIP [Pigeon Protocol] client written in Ruby.
# How to Use # Installation
This is a pre-release skeleton project. There is no gem yet. The gem will be released after we are fully compliant with the spec and have high test coverage stats. This is a pre-release skeleton project. There is no gem yet. The gem will be released after we are fully compliant with the spec and have high test coverage stats.
# Usage: CLI
To get started, clone this repo and run `./pigeon-cli` in place of `pigeon`. To get started, clone this repo and run `./pigeon-cli` in place of `pigeon`.
Eg: `pigeon identity show` becomes `./pigeon-cli show`. Eg: `pigeon identity show` becomes `./pigeon-cli show`.
# Usage: Ruby Lib
# Current Status # Current Status
- [X] pigeon identity new - [X] pigeon identity new
@ -56,8 +60,8 @@ Eg: `pigeon identity show` becomes `./pigeon-cli show`.
- [X] Make all methods private except those required for the CLI. - [X] Make all methods private except those required for the CLI.
- [X] Add Lipmaa links like the Bamboo folks do. - [X] Add Lipmaa links like the Bamboo folks do.
- [X] Set a max message size. - [X] Set a max message size.
- [ ] Clean up all singletons / .current hacks - [X] Clean up all singletons / .current hacks
- [ ] Reduce cross cutting where collaborating objects need access to `@db` - [X] Reduce cross cutting where collaborating objects need access to `@db`
- [ ] Update README.md. Needs user manual for new `Pigeon::Database` class. - [ ] Update README.md. Needs user manual for new `Pigeon::Database` class.
- [ ] Make the switch to LevelDB, RocksDB, [UNQLite](https://unqlite.org/features.html) or similar (currently using Ruby PStore). - [ ] Make the switch to LevelDB, RocksDB, [UNQLite](https://unqlite.org/features.html) or similar (currently using Ruby PStore).
- [ ] Make CLI names consistent with API names. Eg: find vs. read. - [ ] Make CLI names consistent with API names. Eg: find vs. read.
@ -84,6 +88,7 @@ Eg: `pigeon identity show` becomes `./pigeon-cli show`.
- [ ] Interest and Disinterest Signalling for document routing: Create a `$gossip` message to express `blob.have`, `blob.want` and to note last message received of a peer. This can steer bundle creation and an eventual `--for` flag at bundle creation time to customize a bundle to a particular user. - [ ] Interest and Disinterest Signalling for document routing: Create a `$gossip` message to express `blob.have`, `blob.want` and to note last message received of a peer. This can steer bundle creation and an eventual `--for` flag at bundle creation time to customize a bundle to a particular user.
# Idea Bin # Idea Bin
- [ ] Map/reduce plugin support for custom indices? - [ ] Map/reduce plugin support for custom indices?
- [ ] Ability to add a blob in one swoop using File objects and `Message#[]=`, maybe? - [ ] Ability to add a blob in one swoop using File objects and `Message#[]=`, maybe?

View File

@ -4,6 +4,7 @@ module Pigeon
def initialize(path: PIGEON_DB_PATH) def initialize(path: PIGEON_DB_PATH)
@store = Pigeon::Storage.new(path: path) @store = Pigeon::Storage.new(path: path)
init_ident
end end
def add_peer(p); store.add_peer(p); end def add_peer(p); store.add_peer(p); end

View File

@ -19,6 +19,7 @@ module Pigeon
self.body[key] self.body[key]
end end
# TODO: This is a wonky API
def put(db, key, value) def put(db, key, value)
raise STRING_KEYS_ONLY unless key.is_a?(String) raise STRING_KEYS_ONLY unless key.is_a?(String)

View File

@ -4,6 +4,14 @@ require_relative File.join("dist", "pigeon")
require "thor" require "thor"
def db
if File.file?(Pigeon::PIGEON_DB_PATH)
$db ||= Pigeon::Database.new
else
raise "ERROR: You need to create a database file first."
end
end
def bail(msg) def bail(msg)
$stderr.puts msg $stderr.puts msg
exit 1 exit 1
@ -25,20 +33,21 @@ module Pigeon
def new def new
if File.file?(Pigeon::PIGEON_DB_PATH) if File.file?(Pigeon::PIGEON_DB_PATH)
puts "Pigeon has detected a `pigeon.db` file. puts <<-END
Pigeon has detected a `pigeon.db` file.
Refusing to overwrite existing Pigeon config. Refusing to overwrite existing Pigeon config.
Remove config dir or switch to a different directory." Remove config dir or switch to a different directory."
END
raise ConfigAlreadyExists raise ConfigAlreadyExists
end end
kp = Pigeon::LocalIdentity.new() $db = Pigeon::Database.new
kp.save! puts db.local_identity.multihash
puts kp.multihash
end end
desc "show", "Prints your identiy string to STDOUT" desc "show", "Prints your identiy string to STDOUT"
def show def show
puts Pigeon::LocalIdentity.current.multihash puts db.local_identity.multihash
end end
end end
@ -47,13 +56,13 @@ module Pigeon
def set(data = "") def set(data = "")
blob = (data != "") ? data : STDIN.read blob = (data != "") ? data : STDIN.read
puts Pigeon::Storage.current.put_blob(blob) puts db.put_blob(blob)
end end
desc "get", "Read arbitrary data from the database" desc "get", "Read arbitrary data from the database"
def get(hex_digest) def get(hex_digest)
puts Pigeon::Storage.current.get_blob(hex_digest) puts db.get_blob(hex_digest)
end end
end end
@ -61,25 +70,25 @@ module Pigeon
desc "add", "Begin following a Pigeon peer" desc "add", "Begin following a Pigeon peer"
def add(identity) def add(identity)
puts Pigeon::Storage.current.add_peer(identity) puts db.add_peer(identity)
end end
desc "remove", "Stop following a Pigeon peer" desc "remove", "Stop following a Pigeon peer"
def remove(identity) def remove(identity)
puts Pigeon::Storage.current.remove_peer(identity) puts db.remove_peer(identity)
end end
desc "block", "Stop following a Pigeon peer AND refuse to replicate" desc "block", "Stop following a Pigeon peer AND refuse to replicate"
def block(identity) def block(identity)
puts Pigeon::Storage.current.block_peer(identity) puts db.block_peer(identity)
end end
desc "all", "List all Pigeon peers" desc "all", "List all Pigeon peers"
def all def all
puts Pigeon::Storage.current.all_peers puts db.all_peers
end end
end end
@ -96,7 +105,7 @@ module Pigeon
v = (raw_value != "") ? raw_value : STDIN.read v = (raw_value != "") ? raw_value : STDIN.read
draft = db.current_draft draft = db.current_draft
if draft if draft
puts draft[key] = v puts draft.put(db, key, v)
else else
bail("You must create a draft first") bail("You must create a draft first")
end end
@ -115,7 +124,7 @@ module Pigeon
desc "sign", "Commit current DRAFT to local feed." desc "sign", "Commit current DRAFT to local feed."
def sign def sign
puts db.current_draft.publish.render puts db.publish_draft(db.current_draft).render
end end
end end
@ -123,7 +132,7 @@ module Pigeon
desc "create", "Create a pigeon bundle file" desc "create", "Create a pigeon bundle file"
def create(file_path = Pigeon::DEFAULT_BUNDLE_PATH) def create(file_path = Pigeon::DEFAULT_BUNDLE_PATH)
Pigeon::Bundle.create(file_path) db.create_bundle(file_path)
end end
end end
@ -131,26 +140,21 @@ module Pigeon
desc "find", "Find a pigeon message in the local DB" desc "find", "Find a pigeon message in the local DB"
def find(multihash) def find(multihash)
puts Pigeon::Storage.current.read_message(multihash).render puts db.read_message(multihash).render
end end
desc "find-all", "Find a pigeon message in the local DB" desc "find-all", "Find a pigeon message in the local DB"
def find_all(author = Pigeon::LocalIdentity.current.multihash) def find_all(author = db.local_identity.multihash)
# TODO: Ability to find-all messages by author ID puts db.find_all(author).join(Pigeon::CR) + Pigeon::CR
puts Pigeon::Storage
.current
.find_all(author)
.join(Pigeon::CR) + Pigeon::CR
end end
desc "last", "Grab your last message. INTERNAL USE ONLY" desc "last", "Grab your last message. INTERNAL USE ONLY"
def last def last
me = Pigeon::LocalIdentity.current me = db.local_identity
store = Pigeon::Storage.current mcount = db.get_message_count_for(me.multihash)
mcount = store.get_message_count_for(me.multihash) multihash = db.get_message_by_depth(me.multihash, mcount - 1)
multihash = store.get_message_by_depth(me.multihash, mcount - 1)
puts multihash puts multihash
end end
end end
@ -159,14 +163,13 @@ module Pigeon
desc "status", "Show various information about the `.pigeon` directory" desc "status", "Show various information about the `.pigeon` directory"
def status def status
store = Pigeon::Storage.current me = db.local_identity.multihash
me = Pigeon::LocalIdentity.current.multihash mcount = db.get_message_count_for(me)
mcount = store.get_message_count_for(me)
puts " puts "
-`. Pigeon Protocol Ruby Client -`. Pigeon Protocol Ruby Client
'( @ > Version: #{Pigeon::VERSION} '( @ > Version: #{Pigeon::VERSION}
_) ( Peers: #{store.all_peers.count} _) ( Peers: #{db.all_peers.count}
/ ) Blocked: #{store.all_blocks.count} / ) Blocked: #{db.all_blocks.count}
/_,' / Published: #{mcount} /_,' / Published: #{mcount}
\\ / Ident: #{me} \\ / Ident: #{me}
===m" "m=== ===m" "m===