Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: "coder ssh --shuffle" easter egg #1084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: "coder ssh --shuffle" easter egg
  • Loading branch information
dwahler committed May 9, 2022
commit 176cc9d93db859149c7dd19ae230db3f902ec89a
49 changes: 41 additions & 8 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"context"
"io"
"math/rand"
"net"
"os"
"strings"
Expand All @@ -23,21 +24,46 @@ import (

func ssh() *cobra.Command {
var (
stdio bool
stdio bool
shuffle bool
)
cmd := &cobra.Command{
Use: "ssh <workspace>",
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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down