diff options
author | Martin Polden <mpolden@mpolden.no> | 2023-11-29 10:54:28 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2023-11-29 16:12:02 +0100 |
commit | 8a37c49b869d4bf5e4ee0239c512200dc9663ecd (patch) | |
tree | 92903817e54f96bf5df41f007b51d1238e0c0f4f | |
parent | 3e544830df072f5efdfebf68cb01a73e04a3956d (diff) |
Move spinner to cmd package
-rw-r--r-- | client/go/internal/cli/cmd/root.go | 28 | ||||
-rw-r--r-- | client/go/internal/util/spinner.go | 35 |
2 files changed, 26 insertions, 37 deletions
diff --git a/client/go/internal/cli/cmd/root.go b/client/go/internal/cli/cmd/root.go index 2a8c2fee22f..383ce7dd28d 100644 --- a/client/go/internal/cli/cmd/root.go +++ b/client/go/internal/cli/cmd/root.go @@ -12,6 +12,7 @@ import ( "strings" "time" + "github.com/briandowns/spinner" "github.com/fatih/color" "github.com/mattn/go-colorable" "github.com/mattn/go-isatty" @@ -21,7 +22,6 @@ import ( "github.com/vespa-engine/vespa/client/go/internal/cli/auth/auth0" "github.com/vespa-engine/vespa/client/go/internal/cli/auth/zts" "github.com/vespa-engine/vespa/client/go/internal/httputil" - "github.com/vespa-engine/vespa/client/go/internal/util" "github.com/vespa-engine/vespa/client/go/internal/version" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -107,6 +107,30 @@ type auth0Factory func(httpClient httputil.Client, options auth0.Options) (vespa type ztsFactory func(httpClient httputil.Client, domain, url string) (vespa.Authenticator, error) +// newSpinner writes message to writer w and executes function fn. While fn is running a spinning animation will be +// displayed after message. +func newSpinner(w io.Writer, message string, fn func() error) error { + s := spinner.New(spinner.CharSets[11], 100*time.Millisecond, spinner.WithWriter(w)) + // Cursor is hidden by default. Hiding cursor requires Stop() to be called to restore cursor (i.e. if the process is + // interrupted), however we don't want to bother with a signal handler just for this + s.HideCursor = false + if err := s.Color("blue", "bold"); err != nil { + return err + } + if !strings.HasSuffix(message, " ") { + message += " " + } + s.Prefix = message + s.FinalMSG = "\r" + message + "done\n" + s.Start() + err := fn() + if err != nil { + s.FinalMSG = "\r" + message + "failed\n" + } + s.Stop() + return err +} + // New creates the Vespa CLI, writing output to stdout and stderr, and reading environment variables from environment. func New(stdout, stderr io.Writer, environment []string) (*CLI, error) { cmd := &cobra.Command{ @@ -240,7 +264,7 @@ func (c *CLI) configureSpinner() { return fn() } } else { - c.spinner = util.Spinner + c.spinner = newSpinner } } diff --git a/client/go/internal/util/spinner.go b/client/go/internal/util/spinner.go deleted file mode 100644 index 323a5fffe12..00000000000 --- a/client/go/internal/util/spinner.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -package util - -import ( - "io" - "strings" - "time" - - "github.com/briandowns/spinner" -) - -// Spinner writes message to writer w and executes function fn. While fn is running a spinning animation will be -// displayed after message. -func Spinner(w io.Writer, message string, fn func() error) error { - s := spinner.New(spinner.CharSets[11], 100*time.Millisecond, spinner.WithWriter(w)) - // Cursor is hidden by default. Hiding cursor requires Stop() to be called to restore cursor (i.e. if the process is - // interrupted), however we don't want to bother with a signal handler just for this - s.HideCursor = false - if err := s.Color("blue", "bold"); err != nil { - return err - } - if !strings.HasSuffix(message, " ") { - message += " " - } - s.Prefix = message - s.FinalMSG = "\r" + message + "done\n" - s.Start() - err := fn() - if err != nil { - s.FinalMSG = "\r" + message + "failed\n" - } - s.Stop() - return err -} |