addPeer() helper. TODO: Switch back to `noun verb` CLI args because I dont like verb noun

This commit is contained in:
Netscape Navigator 2020-09-15 07:50:48 -05:00
parent a91803a2bd
commit f2709a193b
8 changed files with 79 additions and 44 deletions

View File

@ -18,30 +18,30 @@ You can override this value by specifying a `PIGEON_PATH` ENV var.
- [ ] Get a good CI system going? Run tests at PR time, provide prebuilt binaries, prevent coverage slips, etc..
- [ ] Finish all the things below:
|Done?|Verb |Noun | Flag / arg 1 | Flag 2 |
|-----|------------|--------|---------------|-----------|
| |follow |peer | user mhash | |
| |show |peers | | |
| |show |peers | --blocked | |
| |block |peer | user mhash | |
| |unblock |peer | user mhash | |
| |unfollow |peer | | |
| |create |blob | file path | |
| |create |blob | pipe | |
| |create |bundle | | |
| |create |draft | | |
| |find |blob | | |
| |find |message | --all | |
| |find |message | --last | |
| |ingest |bundle | | |
| |publish |draft | | |
| |show |draft | | |
| |show |message | message mhash | |
| |update |draft | --key=? | --value=? |
| X |show |identity| | |
| X |create |identity| | |
| X |help | | | |
| X |version | | | |
|Done?|Verb |Noun | Flag / arg 1 | Flag 2 |
|-----|------------|-----------|---------------|-----------|
| |peer |block | peer mhash | |
| |peer |follow | peer mhash | |
| |peer |show | | |
| |peer |show | --blocked | |
| |peer |unblock | peer mhash | |
| |peer |unfollow | | |
| |blob |create | file path | |
| |blob |create | pipe | |
| |bundle |create | | |
| |draft |create | | |
| |blob |find | | |
| |message |find | --all | |
| |message |find | --last | |
| |bundle |ingest | | |
| |draft |publish | | |
| |draft |show | | |
| |message |show | message mhash | |
| |draft |update | --key=? | --value=? |
| X |identity |show | | |
| X |identity |create | | |
| X |help | | | |
| X |version | | | |
# Run Tests

View File

@ -34,6 +34,13 @@ var showCmd = &cobra.Command{
Long: `Shows resources such as blobs, drafts, identities, messages, peers, etc..`,
}
// CLI: `pigeon follow [resource]`
var followCmd = &cobra.Command{
Use: "follow [resource]",
Short: "follow various resources",
Long: `Follows resources such as blobs, drafts, identities, messages, peers, etc..`,
}
// CLI: `pigeon create [resource]`
var createCmd = &cobra.Command{
Use: "create [resource]",
@ -64,11 +71,13 @@ var showIdentityCmd = &cobra.Command{
// BootstrapCLI wires up all the relevant commands.
func BootstrapCLI() {
showCmd.AddCommand(showIdentityCmd)
createCmd.AddCommand(createIdentityCmd)
rootCmd.AddCommand(versionCmd)
showCmd.AddCommand(showIdentityCmd)
followCmd.AddCommand()
rootCmd.AddCommand(createCmd)
rootCmd.AddCommand(followCmd)
rootCmd.AddCommand(showCmd)
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)

View File

@ -11,9 +11,9 @@ const BlobSigil = "FILE."
// hash (SHA256) representing arbitrary data.
const MessageSigil = "TEXT."
// UserSigil is a string identifier that precedes a base32
// representation of a particular user's ED25519 public key.
const UserSigil = "USER."
// PeerSigil is a string identifier that precedes a base32
// representation of a particular peer's ED25519 public key.
const PeerSigil = "USER."
// StringSigil is a character used to identify strings as
// defined by the pigeon protocol spec.

View File

@ -12,6 +12,6 @@ func B32Encode(data []byte) string {
return encoder.EncodeToString(data)
}
func encodeUserMhash(pubKey []byte) string {
return UserSigil + B32Encode(pubKey)
func encodePeerMhash(pubKey []byte) string {
return PeerSigil + B32Encode(pubKey)
}

View File

@ -21,13 +21,13 @@ var migrations = []migration{
down: `DROP TABLE IF EXISTS configs`,
},
migration{
up: `CREATE TABLE IF NOT EXISTS users (
up: `CREATE TABLE IF NOT EXISTS peers (
mhash string NOT NULL,
status string NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS unique_users_mhash ON users (mhash);
CREATE UNIQUE INDEX IF NOT EXISTS unique_peers_mhash ON peers (mhash);
`,
down: `DROP TABLE IF EXISTS users`,
down: `DROP TABLE IF EXISTS peers`,
},
}

View File

@ -5,18 +5,22 @@ import (
"log"
)
type peerStatus string
// PeerStatus represents a known state of a peer, such as
// blocked, following, etc..
type PeerStatus string
const (
following peerStatus = "following"
following PeerStatus = "following"
blocked = "blocked"
unknown = "unknown"
)
func getPeerStatus(mHash string) peerStatus {
sqlStatement := `SELECT status FROM users WHERE mhash=$1;`
var status peerStatus
row := Database.QueryRow(sqlStatement, mHash)
const createPeer = "INSERT INTO peers(mhash, status) VALUES(?1, ?2)"
const findPeerByStatus = "SELECT status FROM peers WHERE mhash=$1;"
func getPeerStatus(mHash string) PeerStatus {
var status PeerStatus
row := Database.QueryRow(findPeerByStatus, mHash)
switch err := row.Scan(&status); err {
case sql.ErrNoRows:
return "unknown"
@ -28,7 +32,21 @@ func getPeerStatus(mHash string) peerStatus {
}
}
// func followPeer() {}
func addPeer(mHash string, status PeerStatus) {
tx, err := Database.Begin()
if err != nil {
log.Fatalf("Failed to begin addPeer trx (0): %s", err)
}
_, err2 := tx.Exec(createPeer, mHash, status)
if err2 != nil {
log.Fatalf("Failed to addPeer (1): %s", err2)
}
err1 := tx.Commit()
if err1 != nil {
log.Fatalf("Failed to commit peer (2): %s", err)
}
}
// func showPeers() {}
// func showBlockedPeers() {}
// func blockPeer() {}

View File

@ -9,8 +9,16 @@ import (
func TestGetPeerStatus(t *testing.T) {
resetDB()
mHash := "USER.RJFSFK8YZ8XGTGKQDMSQCQPQXKH8GPRCDY86YZCFQ1QRKYEF48MG"
status := getPeerStatus(mHash)
if status != peerStatus("unknown") {
if status != "unknown" {
t.Fatalf("Expected `unknown`, got %s", status)
}
addPeer(mHash, following)
status2 := getPeerStatus(mHash)
if status2 != "following" {
t.Fatalf("Expected `following`, got %s", status)
}
}

View File

@ -10,7 +10,7 @@ func showIdentity() string {
if len(existingKey) == 0 {
return "NONE"
}
return encodeUserMhash(existingKey)
return encodePeerMhash(existingKey)
}
func createOrShowIdentity() string {
@ -22,7 +22,7 @@ func createOrShowIdentity() string {
} else {
pubKey = oldKey
}
return encodeUserMhash(pubKey)
return encodePeerMhash(pubKey)
}
// CreateIdentity is used by the CLI to create an ED25519