@@ -103,6 +103,15 @@ func configSSH() *cobra.Command {
103
103
// TODO(mafredri): Check coderConfigFile for previous options
104
104
// coderConfigFile.
105
105
106
+ out := cmd .OutOrStdout ()
107
+ if showDiff {
108
+ out = cmd .OutOrStderr ()
109
+ }
110
+ binaryFile , err := currentBinPath (out )
111
+ if err != nil {
112
+ return err
113
+ }
114
+
106
115
// Only allow not-exist errors to avoid trashing
107
116
// the users SSH config.
108
117
configRaw , err := os .ReadFile (sshConfigFile )
@@ -136,11 +145,6 @@ func configSSH() *cobra.Command {
136
145
configModified = append (configModified , []byte (sep + sshConfigIncludeStatement + "\n " )... )
137
146
}
138
147
139
- binaryFile , err := currentBinPath (cmd )
140
- if err != nil {
141
- return err
142
- }
143
-
144
148
root := createConfig (cmd )
145
149
var errGroup errgroup.Group
146
150
type workspaceConfig struct {
@@ -240,13 +244,13 @@ func configSSH() *cobra.Command {
240
244
if showDiff {
241
245
if len (changes ) > 0 {
242
246
// Write to stderr to avoid dirtying the diff output.
243
- _ , _ = fmt .Fprint (cmd . ErrOrStderr () , "Changes:\n \n " )
247
+ _ , _ = fmt .Fprint (out , "Changes:\n \n " )
244
248
for _ , change := range changes {
245
- _ , _ = fmt .Fprintf (cmd . ErrOrStderr () , "* %s\n " , change )
249
+ _ , _ = fmt .Fprintf (out , "* %s\n " , change )
246
250
}
247
251
}
248
252
249
- color := isTTY (cmd )
253
+ color := isTTYOut (cmd )
250
254
for _ , diffFn := range []func () ([]byte , error ){
251
255
func () ([]byte , error ) { return diffBytes (sshConfigFile , configRaw , configModified , color ) },
252
256
func () ([]byte , error ) { return diffBytes (coderConfigFile , coderConfigRaw , buf .Bytes (), color ) },
@@ -257,6 +261,7 @@ func configSSH() *cobra.Command {
257
261
}
258
262
if len (diff ) > 0 {
259
263
filesDiffer = true
264
+ // Always write to stdout.
260
265
_ , _ = fmt .Fprintf (cmd .OutOrStdout (), "\n %s" , diff )
261
266
}
262
267
}
@@ -270,9 +275,9 @@ func configSSH() *cobra.Command {
270
275
IsConfirm : true ,
271
276
})
272
277
if err != nil {
273
- return err
278
+ return nil
274
279
}
275
- _ , _ = fmt .Fprint (cmd . OutOrStdout () , "\n " )
280
+ _ , _ = fmt .Fprint (out , "\n " )
276
281
277
282
if ! bytes .Equal (configRaw , configModified ) {
278
283
err = writeWithTempFileAndMove (sshConfigFile , bytes .NewReader (configRaw ))
@@ -289,10 +294,10 @@ func configSSH() *cobra.Command {
289
294
}
290
295
291
296
if len (workspaces ) > 0 {
292
- _ , _ = fmt .Fprintln (cmd . OutOrStdout () , "You should now be able to ssh into your workspace" )
293
- _ , _ = fmt .Fprintf (cmd . OutOrStdout () , "For example, try running:\n \n \t $ ssh coder.%s\n \n " , workspaces [0 ].Name )
297
+ _ , _ = fmt .Fprintln (out , "You should now be able to ssh into your workspace. " )
298
+ _ , _ = fmt .Fprintf (out , "For example, try running:\n \n \t $ ssh coder.%s\n \n " , workspaces [0 ].Name )
294
299
} else {
295
- _ , _ = fmt .Fprint (cmd . OutOrStdout () , "You don't have any workspaces yet, try creating one with:\n \n \t $ coder create <workspace>\n \n " )
300
+ _ , _ = fmt .Fprint (out , "You don't have any workspaces yet, try creating one with:\n \n \t $ coder create <workspace>\n \n " )
296
301
}
297
302
return nil
298
303
},
@@ -349,7 +354,7 @@ func writeWithTempFileAndMove(path string, r io.Reader) (err error) {
349
354
350
355
// currentBinPath returns the path to the coder binary suitable for use in ssh
351
356
// ProxyCommand.
352
- func currentBinPath (cmd * cobra. Command ) (string , error ) {
357
+ func currentBinPath (w io. Writer ) (string , error ) {
353
358
exePath , err := os .Executable ()
354
359
if err != nil {
355
360
return "" , xerrors .Errorf ("get executable path: %w" , err )
@@ -365,24 +370,26 @@ func currentBinPath(cmd *cobra.Command) (string, error) {
365
370
// correctly. Check if the current executable is in $PATH, and warn the user
366
371
// if it isn't.
367
372
if err != nil && runtime .GOOS == "windows" {
368
- cliui .Warn (cmd . OutOrStdout () ,
373
+ cliui .Warn (w ,
369
374
"The current executable is not in $PATH." ,
370
375
"This may lead to problems connecting to your workspace via SSH." ,
371
376
fmt .Sprintf ("Please move %q to a location in your $PATH (such as System32) and run `%s config-ssh` again." , binName , binName ),
372
377
)
378
+ _ , _ = fmt .Fprint (w , "\n " )
373
379
// Return the exePath so SSH at least works outside of Msys2.
374
380
return exePath , nil
375
381
}
376
382
377
383
// Warn the user if the current executable is not the same as the one in
378
384
// $PATH.
379
385
if filepath .Clean (pathPath ) != filepath .Clean (exePath ) {
380
- cliui .Warn (cmd . OutOrStdout () ,
386
+ cliui .Warn (w ,
381
387
"The current executable path does not match the executable path found in $PATH." ,
382
388
"This may cause issues connecting to your workspace via SSH." ,
383
389
fmt .Sprintf ("\t Current executable path: %q" , exePath ),
384
390
fmt .Sprintf ("\t Executable path in $PATH: %q" , pathPath ),
385
391
)
392
+ _ , _ = fmt .Fprint (w , "\n " )
386
393
}
387
394
388
395
return binName , nil
0 commit comments