golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cmd/gomote/ping.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  
    13  	"golang.org/x/build/internal/gomote/protos"
    14  )
    15  
    16  func ping(args []string) error {
    17  	fs := flag.NewFlagSet("ping", flag.ContinueOnError)
    18  	fs.Usage = func() {
    19  		fmt.Fprintln(os.Stderr, "ping usage: gomote ping [instance]")
    20  		fmt.Fprintln(os.Stderr, "")
    21  		fmt.Fprintln(os.Stderr, "Instance name is optional if a group is specified.")
    22  		fs.PrintDefaults()
    23  		os.Exit(1)
    24  	}
    25  	fs.Parse(args)
    26  
    27  	var pingSet []string
    28  	if fs.NArg() == 1 {
    29  		pingSet = []string{fs.Arg(0)}
    30  	} else if fs.NArg() == 0 && activeGroup != nil {
    31  		for _, inst := range activeGroup.Instances {
    32  			pingSet = append(pingSet, inst)
    33  		}
    34  	} else {
    35  		fs.Usage()
    36  	}
    37  
    38  	ctx := context.Background()
    39  	for _, inst := range pingSet {
    40  		if err := doPing(ctx, inst); err != nil {
    41  			fmt.Fprintf(os.Stderr, "%s: %v\n", inst, err)
    42  		} else {
    43  			fmt.Fprintf(os.Stderr, "%s: alive\n", inst)
    44  		}
    45  	}
    46  	return nil
    47  }
    48  
    49  func doPing(ctx context.Context, name string) error {
    50  	client := gomoteServerClient(ctx)
    51  	_, err := client.InstanceAlive(ctx, &protos.InstanceAliveRequest{
    52  		GomoteId: name,
    53  	})
    54  	if err != nil {
    55  		return fmt.Errorf("unable to ping instance: %w", err)
    56  	}
    57  	return nil
    58  }