Skip to content
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

Suppress color output when writing to something other than a terminal. #51

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
- name: Fetch Go ${{ matrix.go_version }}
uses: actions/setup-go@v5
with:
go-version: stable
go-version: 1.22

- name: Install m and mtools
run: |-
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ require (
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/term v0.26.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
20 changes: 16 additions & 4 deletions internal/verifier/logging_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"github.com/10gen/migration-verifier/internal/logger"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/term"
)

func getLogWriter(logPath string) io.Writer {
func getLogWriter(logPath string) (io.Writer, io.Writer) {
var writer io.Writer

switch logPath {
Expand All @@ -21,23 +22,34 @@ func getLogWriter(logPath string) io.Writer {

default:
if w, err := logger.NewRotatingWriter(logPath); err == nil {
return w
return w, w
}

log.Fatal().Msgf("Failed to open logPath: %s", logPath)
}

return zerolog.SyncWriter(writer)
return writer, zerolog.SyncWriter(writer)
}

func getLoggerAndWriter(logPath string) (*logger.Logger, io.Writer) {
writer := getLogWriter(logPath)
rawWriter, writer := getLogWriter(logPath)

consoleWriter := zerolog.ConsoleWriter{
Out: writer,
TimeFormat: timeFormat,
NoColor: !shouldColorize(rawWriter),
}

l := zerolog.New(consoleWriter).With().Timestamp().Logger()
return logger.NewLogger(&l, writer), writer
}

// Returns true only if the writer is a TTY.
func shouldColorize(writer io.Writer) bool {
osFile, isOsFile := writer.(*os.File)
if !isOsFile {
return true
}

return term.IsTerminal(int(osFile.Fd()))
}
17 changes: 17 additions & 0 deletions vendor/golang.org/x/sys/cpu/asm_darwin_x86_gc.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_darwin_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/golang.org/x/sys/cpu/cpu_gc_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_gc_x86.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions vendor/golang.org/x/sys/cpu/cpu_gccgo_x86.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion vendor/golang.org/x/sys/cpu/cpu_linux_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/golang.org/x/sys/cpu/cpu_linux_noinit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 137 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_linux_riscv64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading