Blob stuff works. NEXT: Ingest a bundle.

This commit is contained in:
Netscape Navigator 2020-10-01 08:00:15 -05:00
parent 0835250cc6
commit ccbc7096ff
3 changed files with 45 additions and 5 deletions

View File

@ -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 | |

View File

@ -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 {

View File

@ -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)