Skip to content

Commit

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

Change-Id: Ie4f2d1e75a410c9b6acb0ec7a848b285789dddb0
Reviewed-on: https://go-review.googlesource.com/c/build/+/418295
Run-TryBot: Michael Knyszek <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Auto-Submit: Michael Knyszek <[email protected]>
Reviewed-by: Carlos Amedee <[email protected]>
  • Loading branch information
mknyszek authored and gopherbot committed Nov 18, 2022
1 parent 1b202d1 commit 0a72f40
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
41 changes: 32 additions & 9 deletions cmd/gomote/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,17 @@ func listGroups(args []string) error {
if len(args) != 0 {
usage()
}
dir, err := groupDir()
groups, err := loadAllGroups()
if err != nil {
return fmt.Errorf("acquiring group directory: %w", err)
return err
}
matches, _ := filepath.Glob(filepath.Join(dir, "*.json"))
// N.B. Glob ignores I/O errors, so no matches also means the directory
// does not exist.
emit := func(name, inst string) {
fmt.Printf("%s\t%s\t\n", name, inst)
}
emit("Name", "Instances")
for _, match := range matches {
g, err := loadGroupFromFile(match)
if err != nil {
return fmt.Errorf("reading group file for %q: %w", match, err)
}
for _, g := range groups {
sort.Strings(g.Instances)
emitted := false
for _, inst := range g.Instances {
Expand All @@ -187,7 +182,7 @@ func listGroups(args []string) error {
emit(g.Name, "(none)")
}
}
if len(matches) == 0 {
if len(groups) == 0 {
fmt.Println("(none)")
}
return nil
Expand All @@ -201,6 +196,34 @@ type groupData struct {
Instances []string `json:"instances"`
}

func (g *groupData) has(inst string) bool {
for _, i := range g.Instances {
if inst == i {
return true
}
}
return false
}

func loadAllGroups() ([]*groupData, error) {
dir, err := groupDir()
if err != nil {
return nil, fmt.Errorf("acquiring group directory: %w", err)
}
// N.B. Glob ignores I/O errors, so no matches also means the directory
// does not exist.
matches, _ := filepath.Glob(filepath.Join(dir, "*.json"))
var groups []*groupData
for _, match := range matches {
g, err := loadGroupFromFile(match)
if err != nil {
return nil, fmt.Errorf("reading group file for %q: %w", match, err)
}
groups = append(groups, g)
}
return groups, nil
}

func loadGroup(name string) (*groupData, error) {
fname, err := groupFilePath(name)
if err != nil {
Expand Down
22 changes: 20 additions & 2 deletions cmd/gomote/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,33 @@ func list(args []string) error {
if fs.NArg() != 0 {
fs.Usage()
}

groups, err := loadAllGroups()
if err != nil {
return fmt.Errorf("loading groups: %v", err)
}
ctx := context.Background()
client := gomoteServerClient(ctx)
resp, err := client.ListInstances(ctx, &protos.ListInstancesRequest{})
if err != nil {
return fmt.Errorf("unable to list instance: %s", statusFromError(err))
}
for _, inst := range resp.GetInstances() {
fmt.Printf("%s\t%s\t%s\texpires in %v\n", inst.GetGomoteId(), inst.GetBuilderType(), inst.GetHostType(), time.Unix(inst.GetExpires(), 0).Sub(time.Now()))
var groupList strings.Builder
for _, g := range groups {
if !g.has(inst.GetGomoteId()) {
continue
}
if groupList.Len() == 0 {
groupList.WriteString(" (")
} else {
groupList.WriteString(", ")
}
groupList.WriteString(g.Name)
}
if groupList.Len() != 0 {
groupList.WriteString(")")
}
fmt.Printf("%s%s\t%s\t%s\texpires in %v\n", inst.GetGomoteId(), groupList.String(), inst.GetBuilderType(), inst.GetHostType(), time.Unix(inst.GetExpires(), 0).Sub(time.Now()))
}
return nil
}

0 comments on commit 0a72f40

Please sign in to comment.