cli: deactivate spinner for debug logging

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2023-01-04 11:00:07 +01:00
parent 3c24e3fa01
commit 35d720e657
8 changed files with 34 additions and 12 deletions

View file

@ -15,6 +15,7 @@ import (
"time"
tty "github.com/mattn/go-isatty"
"github.com/spf13/cobra"
)
const (
@ -31,6 +32,7 @@ var (
type spinnerInterf interface {
Start(text string, showDots bool)
Stop()
io.Writer
}
type spinner struct {
@ -41,6 +43,17 @@ type spinner struct {
spinFunc func(out io.Writer, wg *sync.WaitGroup, stop *atomic.Bool, delay time.Duration, text string, showDots bool)
}
func newSpinnerOrStdout(cmd *cobra.Command) (spinnerInterf, error) {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return nil, err
}
if debug {
return &nopSpinner{cmd.ErrOrStderr()}, nil
}
return newSpinner(cmd.ErrOrStderr()), nil
}
func newSpinner(writer io.Writer) *spinner {
s := &spinner{
out: writer,
@ -108,7 +121,12 @@ func spinNoTTY(out io.Writer, wg *sync.WaitGroup, _ *atomic.Bool, _ time.Duratio
fmt.Fprintln(out, text+"...")
}
type nopSpinner struct{}
type nopSpinner struct {
io.Writer
}
func (s nopSpinner) Start(string, bool) {}
func (s nopSpinner) Stop() {}
func (s *nopSpinner) Start(string, bool) {}
func (s *nopSpinner) Stop() {}
func (s *nopSpinner) Write(p []byte) (n int, err error) {
return s.Writer.Write(p)
}