Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 159137d

Browse files
authored
fix: Use stdin/out defined in command (#3199)
1 parent 9fe260d commit 159137d

8 files changed

+14
-21
lines changed

cli/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func server() *cobra.Command {
106106
Short: "Start a Coder server",
107107
RunE: func(cmd *cobra.Command, args []string) error {
108108
printLogo(cmd, spooky)
109-
logger := slog.Make(sloghuman.Sink(os.Stderr))
109+
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
110110
if verbose {
111111
logger = logger.Leveled(slog.LevelDebug)
112112
}
@@ -348,7 +348,7 @@ func server() *cobra.Command {
348348
}
349349

350350
// This prevents the pprof import from being accidentally deleted.
351-
var _ = pprof.Handler
351+
_ = pprof.Handler
352352
if pprofEnabled {
353353
//nolint:revive
354354
defer serveHandler(cmd.Context(), logger, nil, pprofAddress, "pprof")()
@@ -516,7 +516,7 @@ func server() *cobra.Command {
516516
Short: "Run the built-in PostgreSQL deployment.",
517517
RunE: func(cmd *cobra.Command, args []string) error {
518518
cfg := createConfig(cmd)
519-
logger := slog.Make(sloghuman.Sink(os.Stderr))
519+
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
520520
if verbose {
521521
logger = logger.Leveled(slog.LevelDebug)
522522
}

cli/ssh.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func ssh() *cobra.Command {
127127

128128
ipv6 := peerwg.UUIDToNetaddr(uuid.New())
129129
wgn, err := peerwg.New(
130-
slog.Make(sloghuman.Sink(os.Stderr)),
130+
slog.Make(sloghuman.Sink(cmd.ErrOrStderr())),
131131
[]netaddr.IPPrefix{netaddr.IPPrefixFrom(ipv6, 128)},
132132
)
133133
if err != nil {
@@ -192,14 +192,15 @@ func ssh() *cobra.Command {
192192
}
193193
}
194194

195-
stdoutFile, valid := cmd.OutOrStdout().(*os.File)
196-
if valid && isatty.IsTerminal(stdoutFile.Fd()) {
197-
state, err := term.MakeRaw(int(os.Stdin.Fd()))
195+
stdoutFile, validOut := cmd.OutOrStdout().(*os.File)
196+
stdinFile, validIn := cmd.InOrStdin().(*os.File)
197+
if validOut && validIn && isatty.IsTerminal(stdoutFile.Fd()) {
198+
state, err := term.MakeRaw(int(stdinFile.Fd()))
198199
if err != nil {
199200
return err
200201
}
201202
defer func() {
202-
_ = term.Restore(int(os.Stdin.Fd()), state)
203+
_ = term.Restore(int(stdinFile.Fd()), state)
203204
}()
204205

205206
windowChange := listenWindowSize(cmd.Context())

cli/state_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cli_test
22

33
import (
44
"bytes"
5-
"io"
65
"os"
76
"path/filepath"
87
"strings"
@@ -97,8 +96,6 @@ func TestStatePush(t *testing.T) {
9796
err = stateFile.Close()
9897
require.NoError(t, err)
9998
cmd, root := clitest.New(t, "state", "push", workspace.Name, stateFile.Name())
100-
cmd.SetErr(io.Discard)
101-
cmd.SetOut(io.Discard)
10299
clitest.SetupConfig(t, client, root)
103100
err = cmd.Execute()
104101
require.NoError(t, err)

cli/templatecreate_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cli_test
22

33
import (
4-
"io"
54
"os"
65
"testing"
76

@@ -219,8 +218,6 @@ func TestTemplateCreate(t *testing.T) {
219218
}
220219
cmd, root := clitest.New(t, args...)
221220
clitest.SetupConfig(t, client, root)
222-
cmd.SetOut(io.Discard)
223-
cmd.SetErr(io.Discard)
224221

225222
return cmd.Execute()
226223
}
@@ -233,8 +230,6 @@ func TestTemplateCreate(t *testing.T) {
233230
}
234231
cmd, root := clitest.New(t, args...)
235232
clitest.SetupConfig(t, client, root)
236-
cmd.SetOut(io.Discard)
237-
cmd.SetErr(io.Discard)
238233

239234
return cmd.Execute()
240235
}

cli/templateedit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func templateEdit() *cobra.Command {
4747
if err != nil {
4848
return xerrors.Errorf("update template metadata: %w", err)
4949
}
50-
_, _ = fmt.Printf("Updated template metadata at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
50+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Updated template metadata at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
5151
return nil
5252
},
5353
}

cli/templateupdate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func templateUpdate() *cobra.Command {
9898
return err
9999
}
100100

101-
_, _ = fmt.Printf("Updated version at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
101+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Updated version at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
102102
return nil
103103
},
104104
}

cli/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func update() *cobra.Command {
3030
return err
3131
}
3232
if !workspace.Outdated && !alwaysPrompt {
33-
_, _ = fmt.Printf("Workspace isn't outdated!\n")
33+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Workspace isn't outdated!\n")
3434
return nil
3535
}
3636
template, err := client.Template(cmd.Context(), workspace.TemplateID)
@@ -74,7 +74,7 @@ func update() *cobra.Command {
7474
if !ok {
7575
break
7676
}
77-
_, _ = fmt.Printf("Output: %s\n", log.Output)
77+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Output: %s\n", log.Output)
7878
}
7979
return nil
8080
},

cli/wireguardtunnel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func wireguardPortForward() *cobra.Command {
9595

9696
ipv6 := peerwg.UUIDToNetaddr(uuid.New())
9797
wgn, err := peerwg.New(
98-
slog.Make(sloghuman.Sink(os.Stderr)),
98+
slog.Make(sloghuman.Sink(cmd.ErrOrStderr())),
9999
[]netaddr.IPPrefix{netaddr.IPPrefixFrom(ipv6, 128)},
100100
)
101101
if err != nil {

0 commit comments

Comments
 (0)