@@ -56,6 +56,7 @@ func (r *RootCmd) ssh() *serpent.Command {
56
56
noWait bool
57
57
logDirPath string
58
58
remoteForwards []string
59
+ env []string
59
60
disableAutostart bool
60
61
)
61
62
client := new (codersdk.Client )
@@ -145,16 +146,23 @@ func (r *RootCmd) ssh() *serpent.Command {
145
146
stack := newCloserStack (ctx , logger )
146
147
defer stack .close (nil )
147
148
148
- if len (remoteForwards ) > 0 {
149
- for _ , remoteForward := range remoteForwards {
150
- isValid := validateRemoteForward (remoteForward )
151
- if ! isValid {
152
- return xerrors .Errorf (`invalid format of remote-forward, expected: remote_port:local_address:local_port` )
153
- }
154
- if isValid && stdio {
155
- return xerrors .Errorf (`remote-forward can't be enabled in the stdio mode` )
156
- }
149
+ for _ , remoteForward := range remoteForwards {
150
+ isValid := validateRemoteForward (remoteForward )
151
+ if ! isValid {
152
+ return xerrors .Errorf (`invalid format of remote-forward, expected: remote_port:local_address:local_port` )
153
+ }
154
+ if isValid && stdio {
155
+ return xerrors .Errorf (`remote-forward can't be enabled in the stdio mode` )
156
+ }
157
+ }
158
+
159
+ var parsedEnv [][2 ]string
160
+ for _ , e := range env {
161
+ k , v , ok := strings .Cut (e , "=" )
162
+ if ! ok {
163
+ return xerrors .Errorf ("invalid environment variable setting %q" , e )
157
164
}
165
+ parsedEnv = append (parsedEnv , [2 ]string {k , v })
158
166
}
159
167
160
168
workspace , workspaceAgent , err := getWorkspaceAndAgent (ctx , inv , client , ! disableAutostart , inv .Args [0 ])
@@ -369,6 +377,12 @@ func (r *RootCmd) ssh() *serpent.Command {
369
377
}()
370
378
}
371
379
380
+ for _ , kv := range parsedEnv {
381
+ if err := sshSession .Setenv (kv [0 ], kv [1 ]); err != nil {
382
+ return xerrors .Errorf ("setenv: %w" , err )
383
+ }
384
+ }
385
+
372
386
err = sshSession .RequestPty ("xterm-256color" , 128 , 128 , gossh.TerminalModes {})
373
387
if err != nil {
374
388
return xerrors .Errorf ("request pty: %w" , err )
@@ -477,6 +491,13 @@ func (r *RootCmd) ssh() *serpent.Command {
477
491
FlagShorthand : "R" ,
478
492
Value : serpent .StringArrayOf (& remoteForwards ),
479
493
},
494
+ {
495
+ Flag : "env" ,
496
+ Description : "Set environment variable(s) for session (key1=value1,key2=value2,...)." ,
497
+ Env : "CODER_SSH_ENV" ,
498
+ FlagShorthand : "e" ,
499
+ Value : serpent .StringArrayOf (& env ),
500
+ },
480
501
sshDisableAutostartOption (serpent .BoolOf (& disableAutostart )),
481
502
}
482
503
return cmd
0 commit comments