Skip to content

Commit

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

Change-Id: Ie1bef16bab6f52e28f18cd84551b58de4118c28b
Reviewed-on: https://go-review.googlesource.com/c/build/+/418785
Reviewed-by: Carlos Amedee <carlos@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
  • Loading branch information
mknyszek authored and gopherbot committed Nov 18, 2022
1 parent ef00ab5 commit 365723c
Showing 1 changed file with 57 additions and 23 deletions.
80 changes: 57 additions & 23 deletions cmd/gomote/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ func legacyLs(args []string) error {
}

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

fs := flag.NewFlagSet("ls", flag.ContinueOnError)
fs.Usage = func() {
fmt.Fprintln(os.Stderr, "ls usage: gomote ls <instance> [-R] [dir]")
fmt.Fprintln(os.Stderr, "ls usage: gomote ls [ls-opts] [instance] [dir]")
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Instance name is optional if a group is specified.")
fs.PrintDefaults()
os.Exit(1)
}
Expand All @@ -74,27 +72,63 @@ func ls(args []string) error {
fs.StringVar(&skip, "skip", "", "comma-separated list of relative directories to skip (use forward slashes)")
fs.Parse(args)

ctx := context.Background()
dir := "."
if n := fs.NArg(); n < 1 || n > 2 {
fs.Usage()
} else if n == 2 {
var lsSet []string
switch fs.NArg() {
case 0:
// With no arguments, we need an active group to do anything useful.
if activeGroup == nil {
fmt.Fprintln(os.Stderr, "error: no group specified")
fs.Usage()
}
for _, inst := range activeGroup.Instances {
lsSet = append(lsSet, inst)
}
case 1:
// Ambiguous case. Check if it's a real instance, if not, treat it
// as a directory.
if err := doPing(ctx, fs.Arg(0)); instanceDoesNotExist(err) {
// Not an instance.
for _, inst := range activeGroup.Instances {
lsSet = append(lsSet, inst)
}
dir = fs.Arg(0)
} else if err == nil {
// It's an instance.
lsSet = []string{fs.Arg(0)}
} else {
return fmt.Errorf("failed to ping %q: %v", fs.Arg(0), err)
}
case 2:
// Instance and directory is specified.
lsSet = []string{fs.Arg(0)}
dir = fs.Arg(1)
default:
fmt.Fprintln(os.Stderr, "error: too many arguments")
fs.Usage()
}
name := fs.Arg(0)
ctx := context.Background()
client := gomoteServerClient(ctx)
resp, err := client.ListDirectory(ctx, &protos.ListDirectoryRequest{
GomoteId: name,
Directory: dir,
Recursive: recursive,
SkipFiles: strings.Split(skip, ","),
Digest: digest,
})
if err != nil {
return fmt.Errorf("unable to ls: %s", statusFromError(err))
}
for _, entry := range resp.GetEntries() {
fmt.Fprintf(os.Stdout, "%s\n", entry)
for _, inst := range lsSet {
client := gomoteServerClient(ctx)
resp, err := client.ListDirectory(ctx, &protos.ListDirectoryRequest{
GomoteId: inst,
Directory: dir,
Recursive: recursive,
SkipFiles: strings.Split(skip, ","),
Digest: digest,
})
if err != nil {
return fmt.Errorf("unable to ls: %s", statusFromError(err))
}
if len(lsSet) > 1 {
fmt.Fprintf(os.Stdout, "# %s\n", inst)
}
for _, entry := range resp.GetEntries() {
fmt.Fprintf(os.Stdout, "%s\n", entry)
}
if len(lsSet) > 1 {
fmt.Fprintln(os.Stdout)
}
}
return nil
}

0 comments on commit 365723c

Please sign in to comment.