From 1c4e9f5a68bef8ce335929a114afdfe7682aaf80 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Thu, 7 Dec 2023 15:19:35 +0200 Subject: [PATCH] Fix lint issues and small rewrite around ui/console --- cmd/state/state.go | 16 ++++++++-------- cmd/ui.go | 4 ++-- ui/console/writer.go | 9 ++++----- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/cmd/state/state.go b/cmd/state/state.go index a2a8695f28b..c9a183985af 100644 --- a/cmd/state/state.go +++ b/cmd/state/state.go @@ -69,16 +69,16 @@ func NewGlobalState(ctx context.Context) *GlobalState { stderrTTY := !isDumbTerm && (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) outMutex := &sync.Mutex{} stdout := &console.Writer{ - RawOut: os.Stdout, - Mutex: outMutex, - Writer: colorable.NewColorable(os.Stdout), - IsTTY: stdoutTTY, + RawOutFd: int(os.Stdout.Fd()), + Mutex: outMutex, + Writer: colorable.NewColorable(os.Stdout), + IsTTY: stdoutTTY, } stderr := &console.Writer{ - RawOut: os.Stderr, - Mutex: outMutex, - Writer: colorable.NewColorable(os.Stderr), - IsTTY: stderrTTY, + RawOutFd: int(os.Stderr.Fd()), + Mutex: outMutex, + Writer: colorable.NewColorable(os.Stderr), + IsTTY: stderrTTY, } env := BuildEnvMap(os.Environ()) diff --git a/cmd/ui.go b/cmd/ui.go index 3367223f1af..d857233999e 100644 --- a/cmd/ui.go +++ b/cmd/ui.go @@ -259,7 +259,7 @@ func showProgress(ctx context.Context, gs *state.GlobalState, pbs []*pb.Progress var errTermGetSize bool termWidth := defaultTermWidth if gs.Stdout.IsTTY { - tw, _, err := term.GetSize(int(gs.Stdout.RawOut.Fd())) + tw, _, err := term.GetSize(gs.Stdout.RawOutFd) if !(tw > 0) || err != nil { errTermGetSize = true logger.WithError(err).Warn("error getting terminal size") @@ -314,7 +314,7 @@ func showProgress(ctx context.Context, gs *state.GlobalState, pbs []*pb.Progress updateFreq := 1 * time.Second var stdoutFD int if gs.Stdout.IsTTY { - stdoutFD = int(gs.Stdout.RawOut.Fd()) + stdoutFD = gs.Stdout.RawOutFd updateFreq = 100 * time.Millisecond gs.OutMutex.Lock() gs.Stdout.PersistentText = printProgressBars diff --git a/ui/console/writer.go b/ui/console/writer.go index 9e42e08fe44..760b6f0cab6 100644 --- a/ui/console/writer.go +++ b/ui/console/writer.go @@ -3,17 +3,16 @@ package console import ( "bytes" "io" - "os" "sync" ) // Writer syncs writes with a mutex and, if the output is a TTY, clears before // newlines. type Writer struct { - RawOut *os.File - Mutex *sync.Mutex - Writer io.Writer - IsTTY bool + RawOutFd int + Mutex *sync.Mutex + Writer io.Writer + IsTTY bool // Used for flicker-free persistent objects like the progressbars PersistentText func()