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

feat: add --env flag for "coder ssh"#12991

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mafredri merged 1 commit intocoder:mainfromaaronlehmann:ssh-setenv
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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
39 changes: 30 additions & 9 deletionscli/ssh.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -56,6 +56,7 @@ func (r *RootCmd) ssh() *serpent.Command {
noWaitbool
logDirPathstring
remoteForwards []string
env []string
disableAutostartbool
)
client:=new(codersdk.Client)
Expand DownExpand Up@@ -145,16 +146,23 @@ func (r *RootCmd) ssh() *serpent.Command {
stack:=newCloserStack(ctx,logger)
deferstack.close(nil)

iflen(remoteForwards)>0 {
for_,remoteForward:=rangeremoteForwards {
isValid:=validateRemoteForward(remoteForward)
if!isValid {
returnxerrors.Errorf(`invalid format of remote-forward, expected: remote_port:local_address:local_port`)
}
ifisValid&&stdio {
returnxerrors.Errorf(`remote-forward can't be enabled in the stdio mode`)
}
for_,remoteForward:=rangeremoteForwards {
isValid:=validateRemoteForward(remoteForward)
if!isValid {
returnxerrors.Errorf(`invalid format of remote-forward, expected: remote_port:local_address:local_port`)
}
ifisValid&&stdio {
returnxerrors.Errorf(`remote-forward can't be enabled in the stdio mode`)
}
}

varparsedEnv [][2]string
for_,e:=rangeenv {
k,v,ok:=strings.Cut(e,"=")
if!ok {
returnxerrors.Errorf("invalid environment variable setting %q",e)
}
parsedEnv=append(parsedEnv, [2]string{k,v})
}

workspace,workspaceAgent,err:=getWorkspaceAndAgent(ctx,inv,client,!disableAutostart,inv.Args[0])
Expand DownExpand Up@@ -369,6 +377,12 @@ func (r *RootCmd) ssh() *serpent.Command {
}()
}

for_,kv:=rangeparsedEnv {
iferr:=sshSession.Setenv(kv[0],kv[1]);err!=nil {
returnxerrors.Errorf("setenv: %w",err)
}
}

err=sshSession.RequestPty("xterm-256color",128,128, gossh.TerminalModes{})
iferr!=nil {
returnxerrors.Errorf("request pty: %w",err)
Expand DownExpand Up@@ -477,6 +491,13 @@ func (r *RootCmd) ssh() *serpent.Command {
FlagShorthand:"R",
Value:serpent.StringArrayOf(&remoteForwards),
},
{
Flag:"env",
Description:"Set environment variable(s) for session (key1=value1,key2=value2,...).",
Env:"CODER_SSH_ENV",
FlagShorthand:"e",
Value:serpent.StringArrayOf(&env),
},
sshDisableAutostartOption(serpent.BoolOf(&disableAutostart)),
}
returncmd
Expand Down
43 changes: 43 additions & 0 deletionscli/ssh_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -968,6 +968,49 @@ func TestSSH(t *testing.T) {
<-cmdDone
})

t.Run("Env",func(t*testing.T) {
ifruntime.GOOS=="windows" {
t.Skip("Test not supported on windows")
}

t.Parallel()

client,workspace,agentToken:=setupWorkspaceForAgent(t)
_=agenttest.New(t,client.URL,agentToken)
coderdtest.AwaitWorkspaceAgents(t,client,workspace.ID)

inv,root:=clitest.New(t,
"ssh",
workspace.Name,
"--env",
"foo=bar,baz=qux",
)
clitest.SetupConfig(t,client,root)

pty:=ptytest.New(t).Attach(inv)
inv.Stderr=pty.Output()

// Wait super long so this doesn't flake on -race test.
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitSuperLong)
defercancel()

w:=clitest.StartWithWaiter(t,inv.WithContext(ctx))
deferw.Wait()// We don't care about any exit error (exit code 255: SSH connection ended unexpectedly).

// Since something was output, it should be safe to write input.
// This could show a prompt or "running startup scripts", so it's
// not indicative of the SSH connection being ready.
_=pty.Peek(ctx,1)

// Ensure the SSH connection is ready by testing the shell
// input/output.
pty.WriteLine("echo $foo $baz")
pty.ExpectMatchContext(ctx,"bar qux")

// And we're done.
pty.WriteLine("exit")
})

t.Run("RemoteForwardUnixSocket",func(t*testing.T) {
ifruntime.GOOS=="windows" {
t.Skip("Test not supported on windows")
Expand Down
3 changes: 3 additions & 0 deletionscli/testdata/coder_ssh_--help.golden
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,9 @@ OPTIONS:
--disable-autostart bool, $CODER_SSH_DISABLE_AUTOSTART (default: false)
Disable starting the workspace automatically when connecting via SSH.

-e, --env string-array, $CODER_SSH_ENV
Set environment variable(s) for session (key1=value1,key2=value2,...).

-A, --forward-agent bool, $CODER_SSH_FORWARD_AGENT
Specifies whether to forward the SSH agent specified in
$SSH_AUTH_SOCK.
Expand Down
9 changes: 9 additions & 0 deletionsdocs/cli/ssh.md
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.


[8]ページ先頭

©2009-2025 Movatter.jp