77 "os"
88 "time"
99
10+ "github.com/google/uuid"
1011 "github.com/mattn/go-isatty"
1112 "github.com/pion/webrtc/v3"
1213 "github.com/spf13/cobra"
@@ -25,7 +26,7 @@ func ssh() *cobra.Command {
2526 stdio bool
2627 )
2728 cmd := & cobra.Command {
28- Use : "ssh <workspace> [resource ]" ,
29+ Use : "ssh <workspace> [agent ]" ,
2930 RunE : func (cmd * cobra.Command , args []string ) error {
3031 client , err := createClient (cmd )
3132 if err != nil {
@@ -57,50 +58,45 @@ func ssh() *cobra.Command {
5758 return err
5859 }
5960
60- resourceByAddress := make (map [ string ]codersdk.WorkspaceResource )
61+ agents := make ([ ]codersdk.WorkspaceAgent , 0 )
6162 for _ , resource := range resources {
62- if resource .Agent == nil {
63- continue
64- }
65- resourceByAddress [resource .Address ] = resource
63+ agents = append (agents , resource .Agents ... )
6664 }
67-
68- var resourceAddress string
65+ if len (agents ) == 0 {
66+ return xerrors .New ("workspace has no agents" )
67+ }
68+ var agent codersdk.WorkspaceAgent
6969 if len (args ) >= 2 {
70- resourceAddress = args [1 ]
71- } else {
72- // No resource name was provided!
73- if len (resourceByAddress ) > 1 {
74- // List available resources to connect into?
75- return xerrors .Errorf ("multiple agents" )
76- }
77- for _ , resource := range resourceByAddress {
78- resourceAddress = resource .Address
70+ for _ , otherAgent := range agents {
71+ if otherAgent .Name != args [1 ] {
72+ continue
73+ }
74+ agent = otherAgent
7975 break
8076 }
77+ if agent .ID == uuid .Nil {
78+ return xerrors .Errorf ("agent not found by name %q" , args [1 ])
79+ }
8180 }
82-
83- resource , exists := resourceByAddress [resourceAddress ]
84- if ! exists {
85- resourceKeys := make ([]string , 0 )
86- for resourceKey := range resourceByAddress {
87- resourceKeys = append (resourceKeys , resourceKey )
81+ if agent .ID == uuid .Nil {
82+ if len (agents ) > 1 {
83+ return xerrors .New ("you must specify the name of an agent" )
8884 }
89- return xerrors . Errorf ( "no sshable agent with address %q: %+v" , resourceAddress , resourceKeys )
85+ agent = agents [ 0 ]
9086 }
9187 // OpenSSH passes stderr directly to the calling TTY.
9288 // This is required in "stdio" mode so a connecting indicator can be displayed.
9389 err = cliui .Agent (cmd .Context (), cmd .ErrOrStderr (), cliui.AgentOptions {
9490 WorkspaceName : workspace .Name ,
95- Fetch : func (ctx context.Context ) (codersdk.WorkspaceResource , error ) {
96- return client .WorkspaceResource (ctx , resource .ID )
91+ Fetch : func (ctx context.Context ) (codersdk.WorkspaceAgent , error ) {
92+ return client .WorkspaceAgent (ctx , agent .ID )
9793 },
9894 })
9995 if err != nil {
10096 return xerrors .Errorf ("await agent: %w" , err )
10197 }
10298
103- conn , err := client .DialWorkspaceAgent (cmd .Context (), resource .ID , []webrtc.ICEServer {{
99+ conn , err := client .DialWorkspaceAgent (cmd .Context (), agent .ID , []webrtc.ICEServer {{
104100 URLs : []string {"stun:stun.l.google.com:19302" },
105101 }}, nil )
106102 if err != nil {
0 commit comments