Skip to content

Commit

Permalink
goat: support input from stdin for repo mst command
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Jan 3, 2025
1 parent 86a3724 commit 76d8b41
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
32 changes: 20 additions & 12 deletions cmd/goat/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -18,12 +19,12 @@ import (
"github.com/bluesky-social/indigo/repo"
"github.com/bluesky-social/indigo/util"
"github.com/bluesky-social/indigo/xrpc"

"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
ipld "github.com/ipfs/go-ipld-format"
"github.com/urfave/cli/v2"
"github.com/xlab/treeprint"
"golang.org/x/term"
)

var cmdRepo = &cli.Command{
Expand Down Expand Up @@ -226,20 +227,27 @@ func runRepoMST(cctx *cli.Context) error {
fullCID: cctx.Bool("full-cid"),
root: cctx.String("root"),
}
if opts.carPath == "" {
return fmt.Errorf("need to provide path to CAR file as argument")
}
fi, err := os.Open(opts.carPath)
if err != nil {
return err
// determine whether to read from file argument or stdin
var inputCAR io.Reader
if opts.carPath != "" {
fi, err := os.Open(opts.carPath)
if err != nil {
return err
}
inputCAR = fi
} else {
if term.IsTerminal(int(os.Stdin.Fd())) {
return fmt.Errorf("need to provide path to CAR file as argument")
}
inputCAR = os.Stdin
}

// read repository tree in to memory
r, err := repo.ReadRepoFromCar(ctx, fi)
r, err := repo.ReadRepoFromCar(ctx, inputCAR)
if err != nil {
return err
}

cst := util.CborStore(r.Blockstore())
// determine which root cid to use, defaulting to repo data root
rootCID := r.DataCid()
if opts.root != "" {
optsRootCID, err := cid.Decode(opts.root)
Expand All @@ -248,7 +256,7 @@ func runRepoMST(cctx *cli.Context) error {
}
rootCID = optsRootCID
}
cst := util.CborStore(r.Blockstore())
// start walking mst
exists, err := nodeExists(ctx, cst, rootCID)
if err != nil {
return err
Expand All @@ -259,7 +267,7 @@ func runRepoMST(cctx *cli.Context) error {
return err
}
}

// print tree
fmt.Println(tree.String())
return nil
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ require (
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.21.0
golang.org/x/sync v0.7.0
golang.org/x/term v0.18.0
golang.org/x/text v0.14.0
golang.org/x/time v0.3.0
golang.org/x/tools v0.15.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,8 @@ golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit 76d8b41

Please sign in to comment.