golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/gomote/list.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"os"
    12  	"strings"
    13  	"time"
    14  
    15  	"golang.org/x/build/internal/gomote/protos"
    16  )
    17  
    18  func list(args []string) error {
    19  	fs := flag.NewFlagSet("list", flag.ContinueOnError)
    20  	fs.Usage = func() {
    21  		fmt.Fprintln(os.Stderr, "list usage: gomote list")
    22  		fs.PrintDefaults()
    23  		os.Exit(1)
    24  	}
    25  	fs.Parse(args)
    26  	if fs.NArg() != 0 {
    27  		fs.Usage()
    28  	}
    29  	groups, err := loadAllGroups()
    30  	if err != nil {
    31  		return fmt.Errorf("loading groups: %w", err)
    32  	}
    33  	ctx := context.Background()
    34  	client := gomoteServerClient(ctx)
    35  	resp, err := client.ListInstances(ctx, &protos.ListInstancesRequest{})
    36  	if err != nil {
    37  		return fmt.Errorf("unable to list instance: %w", err)
    38  	}
    39  	for _, inst := range resp.GetInstances() {
    40  		var groupList strings.Builder
    41  		for _, g := range groups {
    42  			if !g.has(inst.GetGomoteId()) {
    43  				continue
    44  			}
    45  			if groupList.Len() == 0 {
    46  				groupList.WriteString(" (")
    47  			} else {
    48  				groupList.WriteString(", ")
    49  			}
    50  			groupList.WriteString(g.Name)
    51  		}
    52  		if groupList.Len() != 0 {
    53  			groupList.WriteString(")")
    54  		}
    55  		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()))
    56  	}
    57  	return nil
    58  }