Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit72c2bf8

Browse files
authored
feat: "coder ssh --shuffle" easter egg (#1084)
1 parent3370186 commit72c2bf8

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
varautostopPollInterval=30*time.Second
3031
varautostopNotifyCountdown= []time.Duration{30*time.Minute}
3132

3233
funcssh()*cobra.Command {
3334
var (
34-
stdiobool
35+
stdiobool
36+
shufflebool
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
iferr!=nil {
@@ -48,10 +50,38 @@ func ssh() *cobra.Command {
4850
returnerr
4951
}
5052

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

5787
ifworkspace.LatestBuild.Transition!=database.WorkspaceTransitionStart {
@@ -96,9 +126,17 @@ func ssh() *cobra.Command {
96126
}
97127
ifagent.ID==uuid.Nil {
98128
iflen(agents)>1 {
99-
returnxerrors.New("you must specify the name of an agent")
129+
if!shuffle {
130+
returnxerrors.New("you must specify the name of an agent")
131+
}
132+
idx,err:=cryptorand.Intn(len(agents))
133+
iferr!=nil {
134+
returnerr
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
returncmd
194234
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp