Skip to content

Commit

Permalink
feat(cli): bucket entry commands
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw committed Jan 28, 2025
1 parent 09e4940 commit f577bf1
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"os"

"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
fbucket "github.com/storacha/fam/bucket"
"github.com/storacha/fam/cmd/bucket"
"github.com/storacha/fam/cmd/remote"
Expand Down Expand Up @@ -48,6 +50,114 @@ func main() {
},
},
bucket.Command,
{
Name: "del",
Aliases: []string{"delete"},
Usage: "Delete an entry from a bucket",
Args: true,
ArgsUsage: "<key>",
Action: func(cCtx *cli.Context) error {
datadir := util.EnsureDataDir(cCtx.String("datadir"))
userdata := util.UserDataStore(context.Background(), datadir)
curr := util.GetCurrent(datadir)
if curr == did.Undef {
return fmt.Errorf("no bucket selected, use `fam bucket use <did>`")
}
bk, err := userdata.Bucket(context.Background(), curr)
if err != nil {
log.Fatal(err)
}
key := cCtx.Args().Get(0)
if key == "" {
return fmt.Errorf("missing key")
}
err = bk.Del(context.Background(), key)
if err != nil {
log.Fatal(err)
}
root, err := bk.Root(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println(root.String())
return nil
},
},
{
Name: "ls",
Aliases: []string{"list"},
Usage: "List bucket entries",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pfx",
Aliases: []string{"p"},
Usage: "filter entries by key prefix",
},
&cli.StringFlag{
Name: "gt",
Usage: "filter entries by key greater than",
},
&cli.StringFlag{
Name: "gte",
Usage: "filter entries by key greater than or equal",
},
&cli.StringFlag{
Name: "lt",
Usage: "filter entries by key greater than",
},
&cli.StringFlag{
Name: "lte",
Usage: "filter entries by key less than or equal",
},
&cli.IntFlag{
Name: "limit",
Aliases: []string{"l"},
Usage: "limit the number of entries printed",
},
},
Action: func(cCtx *cli.Context) error {
datadir := util.EnsureDataDir(cCtx.String("datadir"))
userdata := util.UserDataStore(context.Background(), datadir)
curr := util.GetCurrent(datadir)
if curr == did.Undef {
return fmt.Errorf("no bucket selected, use `fam bucket use <did>`")
}
bk, err := userdata.Bucket(context.Background(), curr)
if err != nil {
log.Fatal(err)
}
opts := []fbucket.EntriesOption{}
if cCtx.String("pfx") != "" {
opts = append(opts, fbucket.WithKeyPrefix(cCtx.String("pfx")))
}
if cCtx.String("gt") != "" {
opts = append(opts, fbucket.WithKeyGreaterThan(cCtx.String("gt")))
}
if cCtx.String("gte") != "" {
opts = append(opts, fbucket.WithKeyGreaterThanOrEqual(cCtx.String("gte")))
}
if cCtx.String("lt") != "" {
opts = append(opts, fbucket.WithKeyLessThan(cCtx.String("lt")))
}
if cCtx.String("lte") != "" {
opts = append(opts, fbucket.WithKeyLessThanOrEqual(cCtx.String("lte")))
}
count := 0
limit := cCtx.Int("limit")
for entry, err := range bk.Entries(context.Background(), opts...) {
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\t%s\n", entry.Key, entry.Value)
count++
if limit > 0 && count >= limit {
break
}
}
fmt.Printf("%d total\n", count)
return nil
},
},
{
Name: "pull",
Usage: "Pull changes from a remote",
Expand Down Expand Up @@ -134,6 +244,42 @@ func main() {
return nil
},
},
{
Name: "put",
Usage: "Put a value to the bucket",
Args: true,
ArgsUsage: "<key> <value>",
Action: func(cCtx *cli.Context) error {
datadir := util.EnsureDataDir(cCtx.String("datadir"))
userdata := util.UserDataStore(context.Background(), datadir)
curr := util.GetCurrent(datadir)
if curr == did.Undef {
return fmt.Errorf("no bucket selected, use `fam bucket use <did>`")
}
bk, err := userdata.Bucket(context.Background(), curr)
if err != nil {
log.Fatal(err)
}
key := cCtx.Args().Get(0)
if key == "" {
return fmt.Errorf("missing key")
}
value, err := cid.Parse(cCtx.Args().Get(1))
if err != nil {
return fmt.Errorf("invalid value: %w", err)
}
err = bk.Put(context.Background(), key, cidlink.Link{Cid: value})
if err != nil {
log.Fatal(err)
}
root, err := bk.Root(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println(root.String())
return nil
},
},
remote.Command,
},
}
Expand Down

0 comments on commit f577bf1

Please sign in to comment.