cmd/gomote: implements GRPC destroy command

This change adds the implementation for the the GRPC destroy command to
the gomote client.

Updates golang/go#48737
For golang/go#47521

Change-Id: I821dd68ee0c38f3a175db30b464e3e4247c04776
Reviewed-on: https://go-review.googlesource.com/c/build/+/398055
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Carlos Amedee <carlos@golang.org>
Auto-Submit: Carlos Amedee <carlos@golang.org>
Reviewed-by: Alex Rakoczy <alex@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/cmd/gomote/destroy.go b/cmd/gomote/destroy.go
index 315c811..2337b3e 100644
--- a/cmd/gomote/destroy.go
+++ b/cmd/gomote/destroy.go
@@ -5,15 +5,17 @@
 package main
 
 import (
+	"context"
 	"flag"
 	"fmt"
 	"log"
 	"os"
 
 	"golang.org/x/build/buildlet"
+	"golang.org/x/build/internal/gomote/protos"
 )
 
-func destroy(args []string) error {
+func legacyDestroy(args []string) error {
 	fs := flag.NewFlagSet("destroy", flag.ContinueOnError)
 	fs.Usage = func() {
 		fmt.Fprintln(os.Stderr, "destroy usage: gomote destroy <instance>")
@@ -49,3 +51,40 @@
 	}
 	return bc.Close()
 }
+
+func destroy(args []string) error {
+	fs := flag.NewFlagSet("destroy", flag.ContinueOnError)
+	fs.Usage = func() {
+		fmt.Fprintln(os.Stderr, "destroy usage: gomote destroy <instance>")
+		fs.PrintDefaults()
+		if fs.NArg() == 0 {
+			// List buildlets that you might want to destroy.
+			client := gomoteServerClient(context.Background())
+			resp, err := client.ListInstances(context.Background(), &protos.ListInstancesRequest{})
+			if err != nil {
+				log.Fatalf("unable to list possible instances to destroy: %s", statusFromError(err))
+			}
+			if len(resp.GetInstances()) > 0 {
+				fmt.Printf("possible instances:\n")
+				for _, inst := range resp.GetInstances() {
+					fmt.Printf("\t%s\n", inst.GetGomoteId())
+				}
+			}
+		}
+		os.Exit(1)
+	}
+
+	fs.Parse(args)
+	if fs.NArg() != 1 {
+		fs.Usage()
+	}
+	name := fs.Arg(0)
+	ctx := context.Background()
+	client := gomoteServerClient(ctx)
+	if _, err := client.DestroyInstance(ctx, &protos.DestroyInstanceRequest{
+		GomoteId: name,
+	}); err != nil {
+		return fmt.Errorf("unable to destroy instance: %s", statusFromError(err))
+	}
+	return nil
+}
diff --git a/cmd/gomote/gomote.go b/cmd/gomote/gomote.go
index d55bc6e..0a93d02 100644
--- a/cmd/gomote/gomote.go
+++ b/cmd/gomote/gomote.go
@@ -150,7 +150,7 @@
 
 func registerCommands() {
 	registerCommand("create", "create a buildlet; with no args, list types of buildlets", legacyCreate)
-	registerCommand("destroy", "destroy a buildlet", destroy)
+	registerCommand("destroy", "destroy a buildlet", legacyDestroy)
 	registerCommand("gettar", "extract a tar.gz from a buildlet", getTar)
 	registerCommand("ls", "list the contents of a directory on a buildlet", ls)
 	registerCommand("list", "list active buildlets", list)
@@ -216,7 +216,8 @@
 // version2 manages how version 2 subcommands are called.
 func version2(args []string) error {
 	cm := map[string]subCommand{
-		"create": create,
+		"create":  create,
+		"destroy": destroy,
 	}
 	if len(args) == 0 {
 		usage()