Finish most `peer` commands. NEXT: peer all
This commit is contained in:
parent
63d3d5463b
commit
70f8638ccb
14
README.md
14
README.md
|
@ -19,6 +19,11 @@ Eg: `pigeon identity show` becomes `./pigeon-cli show`.
|
|||
- [X] pigeon blob set
|
||||
- [X] pigeon blob get
|
||||
|
||||
- [X] pigeon peer add
|
||||
- [X] pigeon peer remove
|
||||
- [X] pigeon peer block
|
||||
- [ ] pigeon peer all
|
||||
|
||||
- [ ] pigeon message new
|
||||
- [ ] pigeon message current
|
||||
- [ ] pigeon message append
|
||||
|
@ -26,10 +31,9 @@ Eg: `pigeon identity show` becomes `./pigeon-cli show`.
|
|||
- [ ] pigeon message find
|
||||
- [ ] pigeon message find-all
|
||||
|
||||
- [ ] pigeon peer add
|
||||
- [ ] pigeon peer remove
|
||||
- [ ] pigeon peer block
|
||||
- [ ] pigeon peer all
|
||||
|
||||
- [ ] pigeon bundle create
|
||||
- [ ] pigeon bundle consume
|
||||
|
||||
- [ ] 100% documentation
|
||||
- [ ] 100% coverage
|
||||
- [ ] add parsers and validators for all CLI inputs
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
require_relative File.join("pigeon", "config.rb")
|
||||
require_relative File.join("pigeon", "storage.rb")
|
||||
require_relative File.join("pigeon", "key_pair.rb")
|
||||
require_relative File.join("pigeon", "storage.rb")
|
||||
require_relative File.join("pigeon", "message.rb")
|
||||
|
||||
module Pigeon
|
||||
end
|
||||
|
|
|
@ -32,6 +32,7 @@ module Pigeon
|
|||
end
|
||||
|
||||
def save!
|
||||
Storage.current.add_peer(public_key)
|
||||
{
|
||||
public_key: public_key,
|
||||
private_key: private_key,
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
module Pigeon
|
||||
class Message
|
||||
# WIP - will work on peer stuff first
|
||||
end
|
||||
end
|
|
@ -6,6 +6,8 @@ module Pigeon
|
|||
ROOT_DIR = ".pigeon"
|
||||
CONF_DIR = "conf"
|
||||
BLOB_DIR = "blobs"
|
||||
PEER_DIR = "peers"
|
||||
BLOCK_DIR = "__blocked__"
|
||||
|
||||
BLOB_HEADER = "&"
|
||||
BLOB_FOOTER = ".sha256"
|
||||
|
@ -19,6 +21,7 @@ module Pigeon
|
|||
create_root_dir
|
||||
create_conf_dir
|
||||
create_blob_dir
|
||||
create_peer_dir
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -48,21 +51,67 @@ module Pigeon
|
|||
File.file?(f) ? File.read(f) : nil
|
||||
end
|
||||
|
||||
def initialized?
|
||||
File.directory?(root_dir)
|
||||
# Base64 is not good for file/dir names.
|
||||
# Imagine: A "/" in a file name. Bad.
|
||||
# We will use base36 for files operations
|
||||
# instead.
|
||||
def to_base36(identity)
|
||||
identity
|
||||
.gsub("@", "")
|
||||
.gsub(".ed25519", "")
|
||||
.each_byte
|
||||
.map { |b| b.to_s(36) }
|
||||
.join
|
||||
end
|
||||
|
||||
def from_base36(identity)
|
||||
end
|
||||
|
||||
def add_peer(identity)
|
||||
path = to_base36(identity)
|
||||
FileUtils.mkdir_p(File.join(peer_dir, path))
|
||||
end
|
||||
|
||||
def remove_peer(identity)
|
||||
path = to_base36(identity)
|
||||
FileUtils.rm_rf(path)
|
||||
end
|
||||
|
||||
def block_peer(identity)
|
||||
remove_peer(identity)
|
||||
path = to_base36(identity)
|
||||
FileUtils.touch(File.join(block_dir, path))
|
||||
end
|
||||
|
||||
def all_peers
|
||||
all = Dir[File.join(peer_dir, "*")]
|
||||
.map { |x| File.split(x).last }
|
||||
puts "Converting base36 to base 64 is hard hmm"
|
||||
binding.pry
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def blob_dir
|
||||
@blob_dir ||= File.join(ROOT_DIR, BLOB_DIR, "sha256")
|
||||
def initialized?
|
||||
File.directory?(root_dir)
|
||||
end
|
||||
|
||||
def root_dir
|
||||
@root_dir ||= File.join(ROOT_DIR)
|
||||
end
|
||||
|
||||
# WARNING: Side effects. Im in a hurry. -RC
|
||||
def blob_dir
|
||||
@blob_dir ||= File.join(ROOT_DIR, BLOB_DIR, "sha256")
|
||||
end
|
||||
|
||||
def peer_dir
|
||||
@peer_dir ||= File.join(ROOT_DIR, PEER_DIR)
|
||||
end
|
||||
|
||||
def block_dir
|
||||
File.join(peer_dir, BLOCK_DIR)
|
||||
end
|
||||
|
||||
def blob_path_for(hex_hash_string)
|
||||
first_part = File.join(blob_dir, hex_hash_string[0, 2])
|
||||
FileUtils.mkdir_p(first_part)
|
||||
|
@ -88,5 +137,10 @@ module Pigeon
|
|||
def create_root_dir
|
||||
FileUtils.mkdir_p(root_dir)
|
||||
end
|
||||
|
||||
def create_peer_dir
|
||||
FileUtils.mkdir_p(peer_dir)
|
||||
FileUtils.mkdir_p(block_dir)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
39
pigeon-cli
39
pigeon-cli
|
@ -6,7 +6,7 @@ require "thor"
|
|||
|
||||
module Pigeon
|
||||
class Identity < Thor
|
||||
class RoostAlreadyExists < StandardError; end
|
||||
class ConfigAlreadyExists < StandardError; end
|
||||
|
||||
desc "new", "Creates a new identiy in `.pigeon` directory if none exists"
|
||||
|
||||
|
@ -16,8 +16,8 @@ module Pigeon
|
|||
if Dir.exist?(Pigeon::Storage::ROOT_DIR)
|
||||
puts "Pigeon has detected a `.pigeon` directory.
|
||||
Refusing to overwrite existing Pigeon config.
|
||||
Remove roost or switch to a different directory."
|
||||
raise RoostAlreadyExists
|
||||
Remove config dir or switch to a different directory."
|
||||
raise ConfigAlreadyExists
|
||||
end
|
||||
kp = Pigeon::KeyPair.new()
|
||||
kp.save!
|
||||
|
@ -32,20 +32,46 @@ module Pigeon
|
|||
end
|
||||
|
||||
class Blob < Thor
|
||||
desc "set", "Copy arbitrary binary data into the roost"
|
||||
desc "set", "Copy arbitrary binary data into the database"
|
||||
|
||||
def set(data = "")
|
||||
blob = (data != "") ? data : STDIN.read
|
||||
puts Pigeon::Storage.current.set_blob(blob)
|
||||
end
|
||||
|
||||
desc "get", "Read arbitrary data from the roost"
|
||||
desc "get", "Read arbitrary data from the database"
|
||||
|
||||
def get(hex_digest)
|
||||
puts Pigeon::Storage.current.get_blob(hex_digest)
|
||||
end
|
||||
end
|
||||
|
||||
class Peer < Thor
|
||||
desc "add", "Begin following a Pigeon peer"
|
||||
|
||||
def add(identity)
|
||||
Pigeon::Storage.current.add_peer(identity)
|
||||
end
|
||||
|
||||
desc "remove", "Stop following a Pigeon peer"
|
||||
|
||||
def remove(identity)
|
||||
Pigeon::Storage.current.remove_peer(identity)
|
||||
end
|
||||
|
||||
desc "block", "Stop following a Pigeon peer AND refuse to replicate"
|
||||
|
||||
def block(identity)
|
||||
Pigeon::Storage.current.block_peer(identity)
|
||||
end
|
||||
|
||||
desc "all", "List all Pigeon peers"
|
||||
|
||||
def all
|
||||
puts Pigeon::Storage.current.all_peers
|
||||
end
|
||||
end
|
||||
|
||||
class CLI < Thor
|
||||
desc "status", "Show various information about the `.pigeon` directory"
|
||||
|
||||
|
@ -58,6 +84,9 @@ module Pigeon
|
|||
|
||||
desc "blob SUBCOMMAND ...ARGS", "Manage blob storage"
|
||||
subcommand "blob", Blob
|
||||
|
||||
desc "peer SUBCOMMAND ...ARGS", "Manage blob storage"
|
||||
subcommand "peer", Peer
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue