Skip to content

Commit 9462fac

Browse files
committed
Merge branch 'main' into ipapikey
2 parents 771cae8 + 437066c commit 9462fac

File tree

6 files changed

+63
-73
lines changed

6 files changed

+63
-73
lines changed

cli/configssh.go

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ func (o sshConfigOptions) equal(other sshConfigOptions) bool {
5757
return slices.Equal(opt1, opt2)
5858
}
5959

60-
func (o sshConfigOptions) asArgs() (args []string) {
61-
for _, opt := range o.sshOptions {
62-
args = append(args, "--ssh-option", fmt.Sprintf("%q", opt))
63-
}
64-
return args
65-
}
66-
6760
func (o sshConfigOptions) asList() (list []string) {
6861
for _, opt := range o.sshOptions {
6962
list = append(list, fmt.Sprintf("ssh-option: %s", opt))
@@ -140,11 +133,8 @@ func configSSH() *cobra.Command {
140133
sshConfigOpts sshConfigOptions
141134
usePreviousOpts bool
142135
coderConfigFile string
143-
showDiff bool
136+
dryRun bool
144137
skipProxyCommand bool
145-
146-
// Diff should exit with status 1 when files differ.
147-
filesDiffer bool
148138
)
149139
cmd := &cobra.Command{
150140
Annotations: workspaceCommand,
@@ -156,14 +146,9 @@ func configSSH() *cobra.Command {
156146
157147
` + cliui.Styles.Code.Render("$ coder config-ssh -o ForwardAgent=yes") + `
158148
159-
- You can use -D (or --diff) to display the changes that will be made.
149+
- You can use --dry-run (or -n) to see the changes that would be made.
160150
161-
` + cliui.Styles.Code.Render("$ coder config-ssh --diff"),
162-
PostRun: func(cmd *cobra.Command, args []string) {
163-
if showDiff && filesDiffer {
164-
os.Exit(1) //nolint: revive
165-
}
166-
},
151+
` + cliui.Styles.Code.Render("$ coder config-ssh --dry-run"),
167152
RunE: func(cmd *cobra.Command, args []string) error {
168153
client, err := createClient(cmd)
169154
if err != nil {
@@ -173,7 +158,9 @@ func configSSH() *cobra.Command {
173158
recvWorkspaceConfigs := sshPrepareWorkspaceConfigs(cmd.Context(), client)
174159

175160
out := cmd.OutOrStdout()
176-
if showDiff {
161+
if dryRun {
162+
// Print everything except diff to stderr so
163+
// that it's possible to capture the diff.
177164
out = cmd.OutOrStderr()
178165
}
179166
binaryFile, err := currentBinPath(out)
@@ -186,7 +173,6 @@ func configSSH() *cobra.Command {
186173
return xerrors.Errorf("user home dir failed: %w", err)
187174
}
188175

189-
sshConfigFileOrig := sshConfigFile
190176
if strings.HasPrefix(sshConfigFile, "~/") {
191177
sshConfigFile = filepath.Join(homedir, sshConfigFile[2:])
192178
}
@@ -221,7 +207,7 @@ func configSSH() *cobra.Command {
221207
// or when a previous config does not exist.
222208
if usePreviousOpts && lastConfig != nil {
223209
sshConfigOpts = *lastConfig
224-
} else if !showDiff && lastConfig != nil && !sshConfigOpts.equal(*lastConfig) {
210+
} else if lastConfig != nil && !sshConfigOpts.equal(*lastConfig) {
225211
newOpts := sshConfigOpts.asList()
226212
newOptsMsg := "\n\n New options: none"
227213
if len(newOpts) > 0 {
@@ -244,7 +230,10 @@ func configSSH() *cobra.Command {
244230
// Selecting "no" will use the last config.
245231
sshConfigOpts = *lastConfig
246232
}
247-
_, _ = fmt.Fprint(out, "\n")
233+
// Only print when prompts are shown.
234+
if yes, _ := cmd.Flags().GetBool("yes"); !yes {
235+
_, _ = fmt.Fprint(out, "\n")
236+
}
248237
}
249238

250239
configModified := configRaw
@@ -316,15 +305,25 @@ func configSSH() *cobra.Command {
316305
configModified = buf.Bytes()
317306
}
318307

319-
if showDiff {
320-
if len(changes) > 0 {
321-
// Write to stderr to avoid dirtying the diff output.
322-
_, _ = fmt.Fprint(out, "The following changes will be made to your SSH configuration:\n\n")
323-
for _, change := range changes {
324-
_, _ = fmt.Fprintf(out, " * %s\n", change)
325-
}
308+
if len(changes) > 0 {
309+
dryRunDisclaimer := ""
310+
if dryRun {
311+
dryRunDisclaimer = " (dry-run, no changes will be made)"
312+
}
313+
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
314+
Text: fmt.Sprintf("The following changes will be made to your SSH configuration:\n\n * %s\n\n Continue?%s", strings.Join(changes, "\n * "), dryRunDisclaimer),
315+
IsConfirm: true,
316+
})
317+
if err != nil {
318+
return nil
326319
}
320+
// Only print when prompts are shown.
321+
if yes, _ := cmd.Flags().GetBool("yes"); !yes {
322+
_, _ = fmt.Fprint(out, "\n")
323+
}
324+
}
327325

326+
if dryRun {
328327
color := isTTYOut(cmd)
329328
diffFns := []func() ([]byte, error){
330329
func() ([]byte, error) { return diffBytes(sshConfigFile, configRaw, configModified, color) },
@@ -340,34 +339,11 @@ func configSSH() *cobra.Command {
340339
return xerrors.Errorf("diff failed: %w", err)
341340
}
342341
if len(diff) > 0 {
343-
filesDiffer = true
344-
// Always write to stdout.
342+
// Write diff to stdout.
345343
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\n%s", diff)
346344
}
347345
}
348-
349-
return nil
350-
}
351-
352-
if len(changes) > 0 {
353-
// In diff mode we don't prompt re-using the previous
354-
// configuration, so we output the entire command.
355-
var args []string
356-
if sshConfigFileOrig != sshDefaultConfigFileName {
357-
args = append(args, "--ssh-config-file", sshConfigFileOrig)
358-
}
359-
args = append(args, sshConfigOpts.asArgs()...)
360-
args = append(args, "--diff")
361-
diffCommand := fmt.Sprintf("$ %s %s", cmd.CommandPath(), strings.Join(args, " "))
362-
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
363-
Text: fmt.Sprintf("The following changes will be made to your SSH configuration:\n\n * %s\n\n To see changes, run diff:\n\n %s\n\n Continue?", strings.Join(changes, "\n * "), diffCommand),
364-
IsConfirm: true,
365-
})
366-
if err != nil {
367-
return nil
368-
}
369-
_, _ = fmt.Fprint(out, "\n")
370-
346+
} else {
371347
if !bytes.Equal(configRaw, configModified) {
372348
err = writeWithTempFileAndMove(sshConfigFile, bytes.NewReader(configModified))
373349
if err != nil {
@@ -394,7 +370,7 @@ func configSSH() *cobra.Command {
394370
}
395371
cliflag.StringVarP(cmd.Flags(), &sshConfigFile, "ssh-config-file", "", "CODER_SSH_CONFIG_FILE", sshDefaultConfigFileName, "Specifies the path to an SSH config.")
396372
cmd.Flags().StringArrayVarP(&sshConfigOpts.sshOptions, "ssh-option", "o", []string{}, "Specifies additional SSH options to embed in each host stanza.")
397-
cmd.Flags().BoolVarP(&showDiff, "diff", "D", false, "Show diff of changes that will be made.")
373+
cmd.Flags().BoolVarP(&dryRun, "dry-run", "n", false, "Perform a trial run with no changes made, showing a diff at the end.")
398374
cmd.Flags().BoolVarP(&skipProxyCommand, "skip-proxy-command", "", false, "Specifies whether the ProxyCommand option should be skipped. Useful for testing.")
399375
_ = cmd.Flags().MarkHidden("skip-proxy-command")
400376
cliflag.BoolVarP(cmd.Flags(), &usePreviousOpts, "use-previous-options", "", "CODER_SSH_USE_PREVIOUS_OPTIONS", false, "Specifies whether or not to keep options from previous run of config-ssh.")
@@ -575,7 +551,7 @@ func diffBytes(name string, b1, b2 []byte, color bool) ([]byte, error) {
575551
if color {
576552
opts = append(opts, write.TerminalColor())
577553
}
578-
err := diff.Text(name, name+".new", b1, b2, &buf, opts...)
554+
err := diff.Text(name, name, b1, b2, &buf, opts...)
579555
if err != nil {
580556
return nil, err
581557
}
@@ -584,7 +560,7 @@ func diffBytes(name string, b1, b2 []byte, color bool) ([]byte, error) {
584560
//
585561
// Example:
586562
// --- /home/user/.ssh/config
587-
// +++ /home/user/.ssh/config.new
563+
// +++ /home/user/.ssh/config
588564
if bytes.Count(b, []byte{'\n'}) == 2 {
589565
b = nil
590566
}

cli/configssh_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,26 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
494494
"--yes",
495495
},
496496
},
497+
{
498+
name: "Do not overwrite config when using --dry-run",
499+
writeConfig: writeConfig{
500+
ssh: strings.Join([]string{
501+
baseHeader,
502+
"",
503+
}, "\n"),
504+
},
505+
wantConfig: wantConfig{
506+
ssh: strings.Join([]string{
507+
baseHeader,
508+
"",
509+
}, "\n"),
510+
},
511+
args: []string{
512+
"--ssh-option", "ForwardAgent=yes",
513+
"--dry-run",
514+
"--yes",
515+
},
516+
},
497517

498518
// Tests for deprecated split coder config.
499519
{

cli/server_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,19 @@ func TestServer(t *testing.T) {
202202
root, cfg := clitest.New(t, "server", "--in-memory", "--address", ":0", "--provisioner-daemons", "1")
203203
serverErr := make(chan error)
204204
go func() {
205-
err := root.ExecuteContext(ctx)
206-
serverErr <- err
205+
serverErr <- root.ExecuteContext(ctx)
207206
}()
208207
require.Eventually(t, func() bool {
209208
var err error
210209
_, err = cfg.URL().Read()
211210
return err == nil
212211
}, 15*time.Second, 25*time.Millisecond)
213-
214212
currentProcess, err := os.FindProcess(os.Getpid())
215213
require.NoError(t, err)
216214
err = currentProcess.Signal(os.Interrupt)
217215
require.NoError(t, err)
218-
// Send a two more signal, which should be ignored. Send 2 because the channel has a buffer
219-
// of 1 and we want to make sure that nothing strange happens if we exceed the buffer.
220-
err = currentProcess.Signal(os.Interrupt)
221-
require.NoError(t, err)
222-
err = currentProcess.Signal(os.Interrupt)
223-
require.NoError(t, err)
216+
// We cannot send more signals here, because it's possible Coder
217+
// has already exited, which could cause the test to fail due to interrupt.
224218
err = <-serverErr
225219
require.NoError(t, err)
226220
})

coderd/users_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ func TestPostLogin(t *testing.T) {
208208
require.NoError(t, err, "fetch login key")
209209

210210
err = api.Database.UpdateAPIKeyByID(ctx, database.UpdateAPIKeyByIDParams{
211-
ID: apiKey.ID,
212-
LastUsed: apiKey.LastUsed,
211+
ID: apiKey.ID,
212+
LastUsed: apiKey.LastUsed,
213+
IPAddress: apiKey.IPAddress,
213214
// This should cause a refresh
214215
ExpiresAt: apiKey.ExpiresAt.Add(time.Hour * -2),
215216
OAuthAccessToken: apiKey.OAuthAccessToken,

scripts/build_go_matrix.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ for spec in "${specs[@]}"; do
193193
--os "$spec_os" \
194194
--arch "$spec_arch" \
195195
--output "$spec_output_binary" \
196-
"${build_args[@]}" &
196+
"${build_args[@]}"
197197
log
198198
log
199199

@@ -227,5 +227,3 @@ for spec in "${specs[@]}"; do
227227
log
228228
fi
229229
done
230-
231-
wait

site/src/components/TerminalLink/TerminalLink.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import ComputerIcon from "@material-ui/icons/Computer"
44
import { FC } from "react"
55
import * as TypesGen from "../../api/typesGenerated"
66
import { combineClasses } from "../../util/combineClasses"
7+
import { generateRandomString } from "../../util/random"
78

89
export const Language = {
910
linkText: "Open terminal",
10-
terminalTitle: "Terminal",
11+
terminalTitle: (identifier: string): string => `Terminal - ${identifier}`,
1112
}
1213

1314
export interface TerminalLinkProps {
@@ -35,7 +36,7 @@ export const TerminalLink: FC<TerminalLinkProps> = ({ agentName, userName = "me"
3536
target="_blank"
3637
onClick={(event) => {
3738
event.preventDefault()
38-
window.open(href, Language.terminalTitle, "width=900,height=600")
39+
window.open(href, Language.terminalTitle(generateRandomString(12)), "width=900,height=600")
3940
}}
4041
>
4142
<ComputerIcon className={styles.icon} />

0 commit comments

Comments
 (0)