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

Skip to content

fix: Use stdin/out defined in command #3199

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func server() *cobra.Command {
Short: "Start a Coder server",
RunE: func(cmd *cobra.Command, args []string) error {
printLogo(cmd, spooky)
logger := slog.Make(sloghuman.Sink(os.Stderr))
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
if verbose {
logger = logger.Leveled(slog.LevelDebug)
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func server() *cobra.Command {
}

// This prevents the pprof import from being accidentally deleted.
var _ = pprof.Handler
_ = pprof.Handler
if pprofEnabled {
//nolint:revive
defer serveHandler(cmd.Context(), logger, nil, pprofAddress, "pprof")()
Expand Down Expand Up @@ -516,7 +516,7 @@ func server() *cobra.Command {
Short: "Run the built-in PostgreSQL deployment.",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := createConfig(cmd)
logger := slog.Make(sloghuman.Sink(os.Stderr))
logger := slog.Make(sloghuman.Sink(cmd.ErrOrStderr()))
if verbose {
logger = logger.Leveled(slog.LevelDebug)
}
Expand Down
11 changes: 6 additions & 5 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func ssh() *cobra.Command {

ipv6 := peerwg.UUIDToNetaddr(uuid.New())
wgn, err := peerwg.New(
slog.Make(sloghuman.Sink(os.Stderr)),
slog.Make(sloghuman.Sink(cmd.ErrOrStderr())),
[]netaddr.IPPrefix{netaddr.IPPrefixFrom(ipv6, 128)},
)
if err != nil {
Expand Down Expand Up @@ -192,14 +192,15 @@ func ssh() *cobra.Command {
}
}

stdoutFile, valid := cmd.OutOrStdout().(*os.File)
if valid && isatty.IsTerminal(stdoutFile.Fd()) {
state, err := term.MakeRaw(int(os.Stdin.Fd()))
stdoutFile, validOut := cmd.OutOrStdout().(*os.File)
stdinFile, validIn := cmd.InOrStdin().(*os.File)
if validOut && validIn && isatty.IsTerminal(stdoutFile.Fd()) {
state, err := term.MakeRaw(int(stdinFile.Fd()))
if err != nil {
return err
}
defer func() {
_ = term.Restore(int(os.Stdin.Fd()), state)
_ = term.Restore(int(stdinFile.Fd()), state)
}()

windowChange := listenWindowSize(cmd.Context())
Expand Down
3 changes: 0 additions & 3 deletions cli/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli_test

import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -97,8 +96,6 @@ func TestStatePush(t *testing.T) {
err = stateFile.Close()
require.NoError(t, err)
cmd, root := clitest.New(t, "state", "push", workspace.Name, stateFile.Name())
cmd.SetErr(io.Discard)
cmd.SetOut(io.Discard)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are just no longer needed (cleanup).

clitest.SetupConfig(t, client, root)
err = cmd.Execute()
require.NoError(t, err)
Expand Down
5 changes: 0 additions & 5 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli_test

import (
"io"
"os"
"testing"

Expand Down Expand Up @@ -219,8 +218,6 @@ func TestTemplateCreate(t *testing.T) {
}
cmd, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

return cmd.Execute()
}
Expand All @@ -233,8 +230,6 @@ func TestTemplateCreate(t *testing.T) {
}
cmd, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

return cmd.Execute()
}
Expand Down
2 changes: 1 addition & 1 deletion cli/templateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func templateEdit() *cobra.Command {
if err != nil {
return xerrors.Errorf("update template metadata: %w", err)
}
_, _ = fmt.Printf("Updated template metadata at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Updated template metadata at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion cli/templateupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func templateUpdate() *cobra.Command {
return err
}

_, _ = fmt.Printf("Updated version at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Updated version at %s!\n", cliui.Styles.DateTimeStamp.Render(time.Now().Format(time.Stamp)))
return nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func update() *cobra.Command {
return err
}
if !workspace.Outdated && !alwaysPrompt {
_, _ = fmt.Printf("Workspace isn't outdated!\n")
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Workspace isn't outdated!\n")
return nil
}
template, err := client.Template(cmd.Context(), workspace.TemplateID)
Expand Down Expand Up @@ -74,7 +74,7 @@ func update() *cobra.Command {
if !ok {
break
}
_, _ = fmt.Printf("Output: %s\n", log.Output)
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Output: %s\n", log.Output)
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cli/wireguardtunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func wireguardPortForward() *cobra.Command {

ipv6 := peerwg.UUIDToNetaddr(uuid.New())
wgn, err := peerwg.New(
slog.Make(sloghuman.Sink(os.Stderr)),
slog.Make(sloghuman.Sink(cmd.ErrOrStderr())),
[]netaddr.IPPrefix{netaddr.IPPrefixFrom(ipv6, 128)},
)
if err != nil {
Expand Down