From ccbc7096ff752c802894191868bc884663e8b285 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Thu, 1 Oct 2020 08:00:15 -0500 Subject: [PATCH] Blob stuff works. NEXT: Ingest a bundle. --- README.md | 5 +++-- project/blob.go | 19 ++++++++++++++++++- project/cli.go | 26 ++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 09008db..15995e8 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,11 @@ 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.. - [ ] Add a `transact()` helper to ensure all transactions are closed out. - [ ] Switch to [SQLX](https://github.com/jmoiron/sqlx) for extra sanity. + - [ ] Write docs for all CLI commands / args AFTER completion. - [ ] Finish all the things below: |Done?|Noun |Verb | Flag / arg 1 | Flag 2 | |-----|------------|-----------|---------------|-----------| - | |blob |find | | | | |draft |create | | | | |draft |publish | | | | |draft |show | | | @@ -32,8 +32,9 @@ You can override this value by specifying a `PIGEON_PATH` ENV var. | |message |show | message mhash | | | |bundle |create | | | | |bundle |ingest | | | - | |blob |add | pipe (later) | | + | X |blob |find | | | | X |blob |add | file path | | + | X |blob |add | STDIO pipe | | | X |peer |untrack | peer mhash | | | X |peers |list | | | | X |peer |block | peer mhash | | diff --git a/project/blob.go b/project/blob.go index 3a726bb..139d410 100644 --- a/project/blob.go +++ b/project/blob.go @@ -1,8 +1,10 @@ package main import ( + "bufio" "crypto/sha256" "fmt" + "io" "io/ioutil" "log" "os" @@ -48,7 +50,22 @@ func addBlob(data []byte) string { mhash := encodeBlobMhash(sha256.Sum256(data)) blobPath := createBlobDirectory(mhash) write(blobPath, data) - return blobPath + return mhash +} + +func addBlobFromPipe() string { + reader := bufio.NewReader(os.Stdin) + var output []byte + + for { + input, err := reader.ReadByte() + if err != nil && err == io.EOF { + break + } + output = append(output, input) + } + + return addBlob(output) } func addBlobFromPath(path string) string { diff --git a/project/cli.go b/project/cli.go index 96c1981..331d21c 100644 --- a/project/cli.go +++ b/project/cli.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "path" "github.com/spf13/cobra" ) @@ -117,10 +118,30 @@ var blobRootCmd = &cobra.Command{ var blobAddCommand = &cobra.Command{ Use: "add", - Short: "Begin tracking a file in the database", + Short: "Begin tracking a file in the database. Provide a pipe or file path.", Aliases: []string{"create"}, Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("%s\n", addBlobFromPath(args[0])) + tpl := "%s\n" + var output string + if len(args) == 0 { + output = addBlobFromPipe() + } else { + output = addBlobFromPath(args[0]) + } + fmt.Printf(tpl, output) + }, +} + +var blobFindCommand = &cobra.Command{ + Use: "find", + Short: "Print the file path of a blob (if any) to STDOUT", + Aliases: []string{"show"}, + Run: func(cmd *cobra.Command, args []string) { + p, f := pathAndFilename(args[0]) + fullPath := path.Join(p, f) + if _, err := os.Stat(fullPath); !os.IsNotExist(err) { + fmt.Printf("%s\n", fullPath) + } }, } @@ -140,6 +161,7 @@ func BootstrapCLI() { rootCmd.AddCommand(blobRootCmd) blobRootCmd.AddCommand(blobAddCommand) + blobRootCmd.AddCommand(blobFindCommand) if err := rootCmd.Execute(); err != nil { fmt.Println(err)