Skip to content

Set stderr and stdout explicitly #1932

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 6 additions & 1 deletion pkg/commands/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ import (
"github.com/golangci/golangci-lint/pkg/timeutils"
)

var (
stderr = io.Writer(os.Stderr)
stdout = io.Writer(os.Stdout)
)

type Executor struct {
rootCmd *cobra.Command
runCmd *cobra.Command
Expand Down Expand Up @@ -92,7 +97,7 @@ func NewExecutor(version, commit, date string) *Executor {

// init of commands must be done before config file reading because
// init sets config with the default values of flags
e.initRoot()
e.initRoot(stderr, stdout)
e.initRun()
e.initHelp()
e.initLinters()
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"io"
"os"
"runtime"
"runtime/pprof"
Expand Down Expand Up @@ -114,7 +115,7 @@ func getDefaultConcurrency() int {
return runtime.NumCPU()
}

func (e *Executor) initRoot() {
func (e *Executor) initRoot(stderr, stdout io.Writer) {
rootCmd := &cobra.Command{
Use: "golangci-lint",
Short: "golangci-lint is a smart linters runner.",
Expand All @@ -130,6 +131,8 @@ func (e *Executor) initRoot() {
PersistentPreRun: e.persistentPreRun,
PersistentPostRun: e.persistentPostRun,
}
rootCmd.SetErr(stderr)
rootCmd.SetOut(stdout)

initRootFlagSet(rootCmd.PersistentFlags(), e.cfg, e.needVersionOption())
e.rootCmd = rootCmd
Expand Down
7 changes: 3 additions & 4 deletions pkg/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"encoding/json"
"fmt"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -36,7 +35,7 @@ func (e *Executor) initVersion() {
RunE: func(cmd *cobra.Command, _ []string) error {
switch strings.ToLower(e.cfg.Version.Format) {
case "short":
fmt.Println(e.version)
cmd.Println(e.version)
case "json":
ver := jsonVersion{
Version: e.version,
Expand All @@ -47,9 +46,9 @@ func (e *Executor) initVersion() {
if err != nil {
return err
}
fmt.Println(string(data))
cmd.Println(string(data))
default:
fmt.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
cmd.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
}
return nil
},
Expand Down