Skip to content
Merged
Changes from all commits
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
56 changes: 48 additions & 8 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ import (
"github.com/coder/coder/coderd/autobuild/schedule"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/cryptorand"
)

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

func ssh() *cobra.Command {
var (
stdio bool
stdio bool
shuffle bool
)
cmd := &cobra.Command{
Annotations: workspaceCommand,
Use: "ssh <workspace>",
Short: "SSH into a workspace",
Args: cobra.MinimumNArgs(1),
Args: cobra.ArbitraryArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := createClient(cmd)
if err != nil {
Expand All @@ -48,10 +50,38 @@ func ssh() *cobra.Command {
return err
}

workspaceParts := strings.Split(args[0], ".")
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, 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.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
if err != nil {
return err
}
if len(workspaces) == 0 {
return xerrors.New("no workspaces to shuffle")
}

idx, err := cryptorand.Intn(len(workspaces))
if err != nil {
return err
}
workspace = workspaces[idx]
} else {
err := cobra.MinimumNArgs(1)(cmd, args)
if err != nil {
return err
}

workspaceParts = strings.Split(args[0], ".")
workspace, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceParts[0])
if err != nil {
return err
}
}

if workspace.LatestBuild.Transition != database.WorkspaceTransitionStart {
Expand Down Expand Up @@ -96,9 +126,17 @@ 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 {
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]
}
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 @@ -189,6 +227,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
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