Skip to content

Commit

Permalink
cmd/gomote: switch to specifying v2 via env var instead of by prefix
Browse files Browse the repository at this point in the history
For golang/go#53956.

Change-Id: I26d2e47d4a73239630b244cdffc6747aae55ee64
Reviewed-on: https://go-review.googlesource.com/c/build/+/418786
Reviewed-by: Carlos Amedee <[email protected]>
Run-TryBot: Michael Knyszek <[email protected]>
Auto-Submit: Michael Knyszek <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
mknyszek authored and gopherbot committed Aug 16, 2022
1 parent 88ec5ea commit ac7bbc6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/gomote/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func create(args []string) error {
fs := flag.NewFlagSet("create", flag.ContinueOnError)

fs.Usage = func() {
fmt.Fprintln(os.Stderr, "create usage: gomote v2 create [create-opts] <type>")
fmt.Fprintln(os.Stderr, "create usage: gomote create [create-opts] <type>")
fs.PrintDefaults()
fmt.Fprintln(os.Stderr, "\nValid types:")
for _, bt := range builders() {
Expand Down
67 changes: 31 additions & 36 deletions cmd/gomote/gomote.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ To list the subcommands, run "gomote" without arguments:
rdp RDP (Remote Desktop Protocol) to a Windows buildlet
run run a command on a buildlet
ssh ssh to a buildlet
v2 version 2 of the gomote API
To list all the builder types available, run "create" with no arguments:
Expand Down Expand Up @@ -90,6 +89,7 @@ import (
"fmt"
"os"
"sort"
"strconv"

"golang.org/x/build/buildenv"
"golang.org/x/build/buildlet"
Expand Down Expand Up @@ -127,7 +127,7 @@ Global flags:
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "Commands:\n\n")
for _, name := range sortedCommands() {
fmt.Fprintf(os.Stderr, " %-10s %s\n", name, commands[name].des)
fmt.Fprintf(os.Stderr, " %-13s %s\n", name, commands[name].des)
}
os.Exit(1)
}
Expand All @@ -143,7 +143,24 @@ func registerCommand(name, des string, run func([]string) error) {
}
}

func registerCommands() {
func registerCommands(version int) {
if version == 2 {
registerCommand("create", "create a buildlet; with no args, list types of buildlets", create)
registerCommand("destroy", "destroy a buildlet", destroy)
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)
registerCommand("ping", "test whether a buildlet is alive and reachable ", ping)
registerCommand("push", "sync your GOROOT directory to the buildlet", push)
registerCommand("put", "put files on a buildlet", put)
registerCommand("putbootstrap", "put bootstrap toolchain in place", putBootstrap)
registerCommand("puttar", "extract a tar.gz to a buildlet", putTar)
registerCommand("rdp", "RDP (Remote Desktop Protocol) to a Windows buildlet", rdp)
registerCommand("rm", "delete files or directories", rm)
registerCommand("run", "run a command on a buildlet", run)
registerCommand("ssh", "ssh to a buildlet", ssh)
return
}
registerCommand("create", "create a buildlet; with no args, list types of buildlets", legacyCreate)
registerCommand("destroy", "destroy a buildlet", legacyDestroy)
registerCommand("gettar", "extract a tar.gz from a buildlet", legacyGetTar)
Expand All @@ -158,7 +175,6 @@ func registerCommands() {
registerCommand("rm", "delete files or directories", legacyRm)
registerCommand("run", "run a command on a buildlet", legacyRun)
registerCommand("ssh", "ssh to a buildlet", legacySSH)
registerCommand("v2", "version 2 of the gomote commands", version2)
}

var (
Expand All @@ -167,7 +183,17 @@ var (

func main() {
buildlet.RegisterFlags()
registerCommands()
version := 1
if vs := os.Getenv("GOMOTE_VERSION"); vs != "" {
v, err := strconv.Atoi(vs)
if err == nil {
version = v
}
}
if version < 1 || version > 2 {
fmt.Fprintf(os.Stderr, "unsupported version %d", version)
}
registerCommands(version)
flag.Usage = usage
flag.Parse()
buildEnv = buildenv.FromFlags()
Expand Down Expand Up @@ -197,37 +223,6 @@ func gomoteServerClient(ctx context.Context) protos.GomoteServiceClient {
return protos.NewGomoteServiceClient(grpcClient)
}

type subCommand func([]string) error

// version2 manages how version 2 subcommands are called.
func version2(args []string) error {
cm := map[string]subCommand{

"create": create,
"destroy": destroy,
"list": list,
"ls": ls,
"run": run,
"ping": ping,
"ssh": ssh,
"rm": rm,
"gettar": getTar,
"put": put,
"puttar": putTar,
"putbootstrap": putBootstrap,
"push": push,
}
if len(args) == 0 {
usage()
}
subCmd := args[0]
sc, ok := cm[subCmd]
if !ok {
return fmt.Errorf("unknown sub-command %q\n", subCmd)
}
return sc(args[1:])
}

// logAndExitf is equivalent to Printf to Stderr followed by a call to os.Exit(1).
func logAndExitf(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gomote/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func legacyLs(args []string) error {
func ls(args []string) error {
fs := flag.NewFlagSet("ls", flag.ContinueOnError)
fs.Usage = func() {
fmt.Fprintln(os.Stderr, "ls usage: gomote v2 ls <instance> [-R] [dir]")
fmt.Fprintln(os.Stderr, "ls usage: gomote ls <instance> [-R] [dir]")
fs.PrintDefaults()
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gomote/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func legacyPing(args []string) error {
func ping(args []string) error {
fs := flag.NewFlagSet("ping", flag.ContinueOnError)
fs.Usage = func() {
fmt.Fprintln(os.Stderr, "ping usage: gomote v2 ping [--status] <instance>")
fmt.Fprintln(os.Stderr, "ping usage: gomote ping [--status] <instance>")
fs.PrintDefaults()
os.Exit(1)
}
Expand Down

0 comments on commit ac7bbc6

Please sign in to comment.