Skip to content

Commit 72c2bf8

Browse files
authored
feat: "coder ssh --shuffle" easter egg (#1084)
1 parent 3370186 commit 72c2bf8

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

cli/ssh.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,22 @@ import (
2424
"github.com/coder/coder/coderd/autobuild/schedule"
2525
"github.com/coder/coder/coderd/database"
2626
"github.com/coder/coder/codersdk"
27+
"github.com/coder/coder/cryptorand"
2728
)
2829

2930
var autostopPollInterval = 30 * time.Second
3031
var autostopNotifyCountdown = []time.Duration{30 * time.Minute}
3132

3233
func ssh() *cobra.Command {
3334
var (
34-
stdio bool
35+
stdio bool
36+
shuffle bool
3537
)
3638
cmd := &cobra.Command{
3739
Annotations: workspaceCommand,
3840
Use: "ssh <workspace>",
3941
Short: "SSH into a workspace",
40-
Args: cobra.MinimumNArgs(1),
42+
Args: cobra.ArbitraryArgs,
4143
RunE: func(cmd *cobra.Command, args []string) error {
4244
client, err := createClient(cmd)
4345
if err != nil {
@@ -48,10 +50,38 @@ func ssh() *cobra.Command {
4850
return err
4951
}
5052

51-
workspaceParts := strings.Split(args[0], ".")
52-
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceParts[0])
53-
if err != nil {
54-
return err
53+
var workspace codersdk.Workspace
54+
var workspaceParts []string
55+
if shuffle {
56+
err := cobra.ExactArgs(0)(cmd, args)
57+
if err != nil {
58+
return err
59+
}
60+
61+
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
62+
if err != nil {
63+
return err
64+
}
65+
if len(workspaces) == 0 {
66+
return xerrors.New("no workspaces to shuffle")
67+
}
68+
69+
idx, err := cryptorand.Intn(len(workspaces))
70+
if err != nil {
71+
return err
72+
}
73+
workspace = workspaces[idx]
74+
} else {
75+
err := cobra.MinimumNArgs(1)(cmd, args)
76+
if err != nil {
77+
return err
78+
}
79+
80+
workspaceParts = strings.Split(args[0], ".")
81+
workspace, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceParts[0])
82+
if err != nil {
83+
return err
84+
}
5585
}
5686

5787
if workspace.LatestBuild.Transition != database.WorkspaceTransitionStart {
@@ -96,9 +126,17 @@ func ssh() *cobra.Command {
96126
}
97127
if agent.ID == uuid.Nil {
98128
if len(agents) > 1 {
99-
return xerrors.New("you must specify the name of an agent")
129+
if !shuffle {
130+
return xerrors.New("you must specify the name of an agent")
131+
}
132+
idx, err := cryptorand.Intn(len(agents))
133+
if err != nil {
134+
return err
135+
}
136+
agent = agents[idx]
137+
} else {
138+
agent = agents[0]
100139
}
101-
agent = agents[0]
102140
}
103141
// OpenSSH passes stderr directly to the calling TTY.
104142
// This is required in "stdio" mode so a connecting indicator can be displayed.
@@ -189,6 +227,8 @@ func ssh() *cobra.Command {
189227
},
190228
}
191229
cliflag.BoolVarP(cmd.Flags(), &stdio, "stdio", "", "CODER_SSH_STDIO", false, "Specifies whether to emit SSH output over stdin/stdout.")
230+
cliflag.BoolVarP(cmd.Flags(), &shuffle, "shuffle", "", "CODER_SSH_SHUFFLE", false, "Specifies whether to choose a random workspace")
231+
_ = cmd.Flags().MarkHidden("shuffle")
192232

193233
return cmd
194234
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy