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

@ -49,9 +49,13 @@ func runCreate(cmd *cobra.Command, args []string) error {
return fmt.Errorf("creating logger: %w", err)
}
defer log.Sync()
fileHandler := file.NewHandler(afero.NewOsFs())
spinner := newSpinner(cmd.ErrOrStderr())
spinner, err := newSpinnerOrStdout(cmd)
if err != nil {
return fmt.Errorf("creating spinner: %w", err)
}
defer spinner.Stop()
fileHandler := file.NewHandler(afero.NewOsFs())
creator := cloudcmd.NewCreator(spinner)
c := &createCmd{log: log}
return c.create(cmd, creator, fileHandler, spinner)

View File

@ -213,7 +213,7 @@ func TestCreate(t *testing.T) {
fileHandler := file.NewHandler(tc.setupFs(require, tc.provider))
c := &createCmd{log: logger.NewTest(t)}
err := c.create(cmd, tc.creator, fileHandler, nopSpinner{})
err := c.create(cmd, tc.creator, fileHandler, &nopSpinner{})
if tc.wantErr {
assert.Error(err)

View File

@ -100,7 +100,7 @@ func TestIAMCreateAWS(t *testing.T) {
require.NoError(cmd.Flags().Set("yes", "true"))
}
err := iamCreateAWS(cmd, nopSpinner{}, tc.creator)
err := iamCreateAWS(cmd, &nopSpinner{}, tc.creator)
if tc.wantErr {
assert.Error(err)

View File

@ -101,7 +101,7 @@ func TestIAMCreateAzure(t *testing.T) {
require.NoError(cmd.Flags().Set("yes", "true"))
}
err := iamCreateAzure(cmd, nopSpinner{}, tc.creator)
err := iamCreateAzure(cmd, &nopSpinner{}, tc.creator)
if tc.wantErr {
assert.Error(err)

View File

@ -123,7 +123,7 @@ func TestIAMCreateGCP(t *testing.T) {
fileHandler := file.NewHandler(tc.setupFs(require, tc.provider))
err := iamCreateGCP(cmd, nopSpinner{}, fileHandler, tc.creator)
err := iamCreateGCP(cmd, &nopSpinner{}, fileHandler, tc.creator)
if tc.wantErr {
assert.Error(err)

View File

@ -165,7 +165,7 @@ func TestInitialize(t *testing.T) {
defer cancel()
cmd.SetContext(ctx)
i := &initCmd{log: logger.NewTest(t)}
err := i.initialize(cmd, newDialer, fileHandler, &stubLicenseClient{}, nopSpinner{})
err := i.initialize(cmd, newDialer, fileHandler, &stubLicenseClient{}, &nopSpinner{})
if tc.wantErr {
assert.Error(err)
@ -415,7 +415,7 @@ func TestAttestation(t *testing.T) {
cmd.SetContext(ctx)
i := &initCmd{log: logger.NewTest(t)}
err := i.initialize(cmd, newDialer, fileHandler, &stubLicenseClient{}, nopSpinner{})
err := i.initialize(cmd, newDialer, fileHandler, &stubLicenseClient{}, &nopSpinner{})
assert.Error(err)
// make sure the error is actually a TLS handshake error
assert.Contains(err.Error(), "transport: authentication handshake failed")

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)
}

View File

@ -142,7 +142,7 @@ func TestTerminate(t *testing.T) {
require.NoError(cmd.Flags().Set("yes", "true"))
}
err := terminate(cmd, tc.terminator, fileHandler, nopSpinner{})
err := terminate(cmd, tc.terminator, fileHandler, &nopSpinner{})
if tc.wantErr {
assert.Error(err)