From fcee853237824e832f1eecfc26e141f0fe09b0ef Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Wed, 9 Sep 2020 07:42:56 -0500 Subject: [PATCH] :clap: `pigeon create identity` works! --- README.md | 2 +- cli.go | 25 +++++++++++++++++++------ util.go | 29 ++++++++++++++++------------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 6a5870f..db914e6 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ Don't use the Go version yet. If you want something stable, there is a [Ruby ver |Done?|Verb |Noun | Flag / arg 1 | Flag 2 | |-----|------------|--------|---------------|-----------| - | |create |identity| | | | |show |identity| | | | |create |draft | | | | |show |blob | | | @@ -36,6 +35,7 @@ Don't use the Go version yet. If you want something stable, there is a [Ruby ver | |show |peers | | | | |show |peers | --blocked | | | |unfollow |peer | | | + | X |create |identity| | | | X |help | | | | | X |version | | | | diff --git a/cli.go b/cli.go index 1ec25f9..e5b8be8 100644 --- a/cli.go +++ b/cli.go @@ -9,27 +9,40 @@ import ( var rootCmd = &cobra.Command{ Use: "pigeon", - Short: "Pigeon is a peer-to-peer database for offline systems", + Short: "Pigeon is a peer-to-peer database for offline systems.", Long: `Pigeon is an off-grid, serverless, peer-to-peer database for building software that works on poor internet connections, or entirely offline.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("It works!") - }, } var versionCmd = &cobra.Command{ Use: "version", - Short: "This is the short description of version", - Long: `This one is longer.`, + Short: "Print the software version.", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("Pigeon CLI Client (Golang), version %s\n", Version) }, } +var createCmd = &cobra.Command{ + Use: "create [resource]", + Short: "Create various resources", + Long: `Creates resources, such as identities, drafts, messages, blobs, etc..`, +} + +var createIdentityCmd = &cobra.Command{ + Use: "identity", + Short: "Create a new identity.", + Long: `Creates a new identity.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(createOrShowIdentity()) + }, +} + // BootstrapCLI wires up all the relevant commands. func BootstrapCLI() { + createCmd.AddCommand(createIdentityCmd) rootCmd.AddCommand(versionCmd) + rootCmd.AddCommand(createCmd) if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) diff --git a/util.go b/util.go index f17eeaf..2790754 100644 --- a/util.go +++ b/util.go @@ -5,6 +5,18 @@ import ( "log" ) +func createOrShowIdentity() string { + var pubKey []byte + oldKey := GetConfig("private_key") + if len(oldKey) == 0 { + newKey, _ := CreateIdentity() + pubKey = newKey + } else { + pubKey = oldKey + } + return encodeUserMhash(pubKey) +} + // CreateIdentity is used by the CLI to create an ED25519 // keypair and store it to disk. It returns the private key // as a Base32 encoded string @@ -18,16 +30,7 @@ func CreateIdentity() (ed25519.PublicKey, ed25519.PrivateKey) { return pub, priv } -// EncodeUserMhash Takes a []byte and converts it to a B32 -// string in the format "USER.DATA.ED25519" -// func EncodeUserMhash(pubKey []byte) string { -// b32 := B32Encode(pubKey) -// b32Length := len(b32) - -// if b32Length != 52 { -// m := "Expected %s to be 52 bytes long. Got %d" -// log.Fatal(m, b32, b32Length) -// } - -// return fmt.Sprintf("%s%s", UserSigil, b32) -// } +func encodeUserMhash(pubKey []byte) string { + sigil := "USER." + return sigil + B32Encode(pubKey) +}