Skip to content

Commit

Permalink
goat: use "-" convention for standard io on repo export and repo mst …
Browse files Browse the repository at this point in the history
…commands
  • Loading branch information
devinivy committed Jan 7, 2025
1 parent 76d8b41 commit 1ce44fa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
41 changes: 21 additions & 20 deletions cmd/goat/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -24,7 +23,6 @@ import (
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 @@ -125,16 +123,25 @@ func runRepoExport(cctx *cli.Context) error {
now := time.Now().Format("20060102150405")
carPath = fmt.Sprintf("%s.%s.car", username, now)
}
// NOTE: there is a race condition, but nice to give a friendly error earlier before downloading
if _, err := os.Stat(carPath); err == nil {
return fmt.Errorf("file already exists: %s", carPath)
output, err := getFileOrStdout(carPath)
if err != nil {
if errors.Is(err, os.ErrExist) {
return fmt.Errorf("file already exists: %s", carPath)
}
return err
}
defer output.Close()
if (carPath != stdIOPath) {
fmt.Printf("downloading from %s to: %s\n", xrpcc.Host, carPath)
}
fmt.Printf("downloading from %s to: %s\n", xrpcc.Host, carPath)
repoBytes, err := comatproto.SyncGetRepo(ctx, &xrpcc, ident.DID.String(), "")
if err != nil {
return err
}
return os.WriteFile(carPath, repoBytes, 0666)
if _, err := output.Write(repoBytes); err != nil {
return err
}
return nil
}

func runRepoImport(cctx *cli.Context) error {
Expand Down Expand Up @@ -227,19 +234,13 @@ func runRepoMST(cctx *cli.Context) error {
fullCID: cctx.Bool("full-cid"),
root: cctx.String("root"),
}
// 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 from file or stdin
if opts.carPath == "" {
return fmt.Errorf("need to provide path to CAR file as argument")
}
inputCAR, err := getFileOrStdin(opts.carPath)
if (err != nil) {
return err
}
// read repository tree in to memory
r, err := repo.ReadRepoFromCar(ctx, inputCAR)
Expand Down
27 changes: 27 additions & 0 deletions cmd/goat/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"io"
"os"

"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
Expand All @@ -16,3 +18,28 @@ func resolveIdent(ctx context.Context, arg string) (*identity.Identity, error) {
dir := identity.DefaultDirectory()
return dir.Lookup(ctx, *id)
}

const stdIOPath = "-"

func getFileOrStdin(path string) (io.Reader, error) {
if (path == stdIOPath) {
return os.Stdin, nil
}
file, err := os.Open(path)
if err != nil {
return nil, err
}
return file, nil
}

func getFileOrStdout(path string) (io.WriteCloser, error) {
if (path == stdIOPath) {
return os.Stdout, nil
}
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return nil, err
}
return file, nil
}

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ 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: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,6 @@ 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 1ce44fa

Please sign in to comment.