From 176cc9d93db859149c7dd19ae230db3f902ec89a Mon Sep 17 00:00:00 2001 From: David Wahler Date: Mon, 9 May 2022 16:24:17 +0000 Subject: [PATCH 1/2] feat: "coder ssh --shuffle" easter egg --- cli/ssh.go | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/cli/ssh.go b/cli/ssh.go index 627a0a041ee4d..86ec5d47403c0 100644 --- a/cli/ssh.go +++ b/cli/ssh.go @@ -3,6 +3,7 @@ package cli import ( "context" "io" + "math/rand" "net" "os" "strings" @@ -23,21 +24,46 @@ import ( func ssh() *cobra.Command { var ( - stdio bool + stdio bool + shuffle bool ) cmd := &cobra.Command{ Use: "ssh ", - Args: cobra.MinimumNArgs(1), + Args: cobra.ArbitraryArgs, RunE: func(cmd *cobra.Command, args []string) error { client, err := createClient(cmd) if err != nil { return err } - workspaceParts := strings.Split(args[0], ".") - workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, workspaceParts[0]) - if err != nil { - return err + var workspace codersdk.Workspace + var workspaceParts []string + if shuffle { + err := cobra.ExactArgs(0)(cmd, args) + if err != nil { + return err + } + + workspaces, err := client.WorkspacesByUser(cmd.Context(), codersdk.Me) + if err != nil { + return err + } + if len(workspaces) == 0 { + return xerrors.New("no workspaces to shuffle") + } + + workspace = workspaces[rand.Intn(len(workspaces))] + } else { + err := cobra.MinimumNArgs(1)(cmd, args) + if err != nil { + return err + } + + workspaceParts = strings.Split(args[0], ".") + workspace, err = client.WorkspaceByName(cmd.Context(), codersdk.Me, workspaceParts[0]) + if err != nil { + return err + } } if workspace.LatestBuild.Transition != database.WorkspaceTransitionStart { @@ -82,9 +108,14 @@ func ssh() *cobra.Command { } if agent.ID == uuid.Nil { if len(agents) > 1 { - return xerrors.New("you must specify the name of an agent") + if shuffle { + agent = agents[rand.Intn(len(agents))] + } else { + return xerrors.New("you must specify the name of an agent") + } + } else { + agent = agents[0] } - agent = agents[0] } // OpenSSH passes stderr directly to the calling TTY. // This is required in "stdio" mode so a connecting indicator can be displayed. @@ -159,6 +190,8 @@ func ssh() *cobra.Command { }, } cliflag.BoolVarP(cmd.Flags(), &stdio, "stdio", "", "CODER_SSH_STDIO", false, "Specifies whether to emit SSH output over stdin/stdout.") + cliflag.BoolVarP(cmd.Flags(), &shuffle, "shuffle", "", "CODER_SSH_SHUFFLE", false, "Specifies whether to choose a random workspace") + cmd.Flags().MarkHidden("shuffle") return cmd } From 781fab9ce13c26748e554b406affb4efa3bd43c5 Mon Sep 17 00:00:00 2001 From: David Wahler Date: Mon, 9 May 2022 16:24:17 +0000 Subject: [PATCH 2/2] fix lint errors --- cli/ssh.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/cli/ssh.go b/cli/ssh.go index 86ec5d47403c0..a7a4a6fb5b173 100644 --- a/cli/ssh.go +++ b/cli/ssh.go @@ -3,7 +3,6 @@ package cli import ( "context" "io" - "math/rand" "net" "os" "strings" @@ -20,6 +19,7 @@ import ( "github.com/coder/coder/cli/cliui" "github.com/coder/coder/coderd/database" "github.com/coder/coder/codersdk" + "github.com/coder/coder/cryptorand" ) func ssh() *cobra.Command { @@ -52,7 +52,11 @@ func ssh() *cobra.Command { return xerrors.New("no workspaces to shuffle") } - workspace = workspaces[rand.Intn(len(workspaces))] + idx, err := cryptorand.Intn(len(workspaces)) + if err != nil { + return err + } + workspace = workspaces[idx] } else { err := cobra.MinimumNArgs(1)(cmd, args) if err != nil { @@ -108,11 +112,14 @@ func ssh() *cobra.Command { } if agent.ID == uuid.Nil { if len(agents) > 1 { - if shuffle { - agent = agents[rand.Intn(len(agents))] - } else { + if !shuffle { return xerrors.New("you must specify the name of an agent") } + idx, err := cryptorand.Intn(len(agents)) + if err != nil { + return err + } + agent = agents[idx] } else { agent = agents[0] } @@ -191,7 +198,7 @@ func ssh() *cobra.Command { } cliflag.BoolVarP(cmd.Flags(), &stdio, "stdio", "", "CODER_SSH_STDIO", false, "Specifies whether to emit SSH output over stdin/stdout.") cliflag.BoolVarP(cmd.Flags(), &shuffle, "shuffle", "", "CODER_SSH_SHUFFLE", false, "Specifies whether to choose a random workspace") - cmd.Flags().MarkHidden("shuffle") + _ = cmd.Flags().MarkHidden("shuffle") return cmd }