Skip to content

Commit

Permalink
cmd/gomote: add support for groups to the rm command
Browse files Browse the repository at this point in the history
For golang/go#53956.

Change-Id: I1224aabae4e80cb0380baaa0d5d7c434c59af61a
Reviewed-on: https://go-review.googlesource.com/c/build/+/418937
Reviewed-by: Carlos Amedee <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Michael Knyszek <[email protected]>
Auto-Submit: Michael Knyszek <[email protected]>
  • Loading branch information
mknyszek authored and gopherbot committed Nov 18, 2022
1 parent 31fba47 commit 002ba28
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions cmd/gomote/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"

"golang.org/x/build/internal/gomote/protos"
"golang.org/x/sync/errgroup"
)

func legacyRm(args []string) error {
Expand Down Expand Up @@ -41,29 +42,61 @@ func legacyRm(args []string) error {
}

func rm(args []string) error {
if activeGroup != nil {
return fmt.Errorf("command does not yet support groups")
}

fs := flag.NewFlagSet("rm", flag.ContinueOnError)
fs.Usage = func() {
fmt.Fprintln(os.Stderr, "rm usage: gomote rm <instance> <file-or-dir>+")
fmt.Fprintln(os.Stderr, " gomote rm <instance> . (to delete everything)")
fmt.Fprintln(os.Stderr, "rm usage: gomote rm [instance] <file-or-dir>+")
fmt.Fprintln(os.Stderr, " gomote rm [instance] . (to delete everything)")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Instance name is optional if a group is specified.")
fs.PrintDefaults()
os.Exit(1)
}
fs.Parse(args)

if fs.NArg() < 2 {
fs.Usage()
}
name := fs.Arg(0)
args = fs.Args()[1:]
ctx := context.Background()
var rmSet []string
var paths []string
if err := doPing(ctx, fs.Arg(0)); instanceDoesNotExist(err) {
// When there's no active group, this is just an error.
if activeGroup == nil {
return fmt.Errorf("instance %q: %s", fs.Arg(0), statusFromError(err))
}
// When there is an active group, this just means that we're going
// to use the group instead and assume the rest is a command.
for _, inst := range activeGroup.Instances {
rmSet = append(rmSet, inst)
}
if fs.NArg() == 0 {
fmt.Fprintln(os.Stderr, "error: not enough arguments")
fs.Usage()
}
paths = fs.Args()
} else if err == nil {
rmSet = append(rmSet, fs.Arg(0))
if fs.NArg() == 1 {
fmt.Fprintln(os.Stderr, "error: not enough arguments")
fs.Usage()
}
paths = fs.Args()[1:]
} else {
return fmt.Errorf("checking instance %q: %v", fs.Arg(0), err)
}

eg, ctx := errgroup.WithContext(context.Background())
for _, inst := range rmSet {
inst := inst
eg.Go(func() error {
return doRm(ctx, inst, paths)
})
}
return eg.Wait()
}

func doRm(ctx context.Context, inst string, paths []string) error {
client := gomoteServerClient(ctx)
if _, err := client.RemoveFiles(ctx, &protos.RemoveFilesRequest{
GomoteId: name,
Paths: args,
GomoteId: inst,
Paths: paths,
}); err != nil {
return fmt.Errorf("unable to remove files: %s", statusFromError(err))
}
Expand Down

0 comments on commit 002ba28

Please sign in to comment.