8000 feat: "coder ssh --shuffle" easter egg · coder/coder@176cc9d · GitHub
[go: up one dir, main page]

Skip to content

Commit 176cc9d

Browse files
committed
feat: "coder ssh --shuffle" easter egg
1 parent 73b8a5a commit 176cc9d

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

cli/ssh.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"context"
55
"io"
6+
"math/rand"
67
"net"
78
"os"
89
"strings"
@@ -23,21 +24,46 @@ import (
2324

2425
func ssh() *cobra.Command {
2526
var (
26-
stdio bool
27+
stdio bool
28+
shuffle bool
2729
)
2830
cmd := &cobra.Command{
2931
Use: "ssh <workspace>",
30-
Args: cobra.MinimumNArgs(1),
32+
Args: cobra.ArbitraryArgs,
3133
RunE: func(cmd *cobra.Command, args []string) error {
3234
client, err := createClient(cmd)
3335
if err != nil {
3436
return err
3537
}
3638

37-
workspaceParts := strings.Split(args[0], ".")
38-
workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, workspaceParts[0])
39-
if err != nil {
40-
return err
39+
var workspace codersdk.Workspace
40+
var workspaceParts []string
41+
if shuffle {
42+
err := cobra.ExactArgs(0)(cmd, args)
43+
if err != nil {
44+
return err
45+
}
46+
47+
workspaces, err := client.WorkspacesByUser(cmd.Context(), codersdk.Me)
48+
if err != nil {
49+
return err
50+
}
51+
if len(workspaces) == 0 {
52+
return xerrors.New("no workspaces to shuffle")
53+
}
54+
55+
workspace = workspaces[rand.Intn(len(workspaces))]
56+
} else {
57+
err := cobra.MinimumNArgs(1)(cmd, args)
58+
if err != nil {
59+
return err
60+
}
61+
62+
workspaceParts = strings.Split(args[0], ".")
63+
workspace, err = client.WorkspaceByName(cmd.Context(), codersdk.Me, workspaceParts[0])
64+
if err != nil {
65+
return err
66+
}
4167
}
4268

4369
if workspace.LatestBuild.Transition != database.WorkspaceTransitionStart {
@@ -82,9 +108,14 @@ func ssh() *cobra.Command {
82108
}
83109
if agent.ID == uuid.Nil {
84110
if len(agents) > 1 {
85-
return xerrors.New("you must specify the name of an agent")
111+
if shuffle {
112+
agent = agents[rand.Intn(len(agents))]
113+
} else {
114+
return xerrors.New("you must specify the name of an agent")
115+
}
116+
} else {
117+
agent = agents[0]
86118
}
87-
agent = agents[0]
88119
}
89120
// OpenSSH passes stderr directly to the calling TTY.
90121
// This is required in "stdio" mode so a connecting indicator can be displayed.
@@ -159,6 +190,8 @@ func ssh() *cobra.Command {
159190
},
160191
}
161192
cliflag.BoolVarP(cmd.Flags(), &stdio, "stdio", "", "CODER_SSH_STDIO", false, "Specifies whether to emit SSH output over stdin/stdout.")
193+
cliflag.BoolVarP(cmd.Flags(), &shuffle, "shuffle", "", "CODER_SSH_SHUFFLE", false, "Specifies whether to choose a random workspace")
194+
cmd.Flags().MarkHidden("shuffle")
162195

163196
return cmd
164197
}

0 commit comments

Comments
 (0)
0