From c48420e49ebe26d6968e7f20ec9f9584d9a7b54a Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Tue, 10 Oct 2023 16:43:49 -0500 Subject: [PATCH] feat(flags): Add configurable spinner easter egg --- cmd/dump/dump.go | 3 +++ cmd/restore/restore.go | 3 +++ go.mod | 1 + go.sum | 2 ++ internal/actions/dump/dump.go | 2 +- internal/actions/restore/restore.go | 2 +- internal/config/dump.go | 1 + internal/config/flags/generic.go | 21 +++++++++++++++++++++ internal/config/restore.go | 1 + internal/progressbar/new.go | 13 +++++++++++-- 10 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 internal/config/flags/generic.go diff --git a/cmd/dump/dump.go b/cmd/dump/dump.go index da4595eb..ed88c0bb 100644 --- a/cmd/dump/dump.go +++ b/cmd/dump/dump.go @@ -54,6 +54,7 @@ the generated filename might look like "` + dump.HelpFilename() + `"`, flags.ExcludeTableData(cmd, &action.ExcludeTableData) flags.Quiet(cmd, &action.Quiet) flags.RemoteGzip(cmd) + flags.Spinner(cmd, &action.Spinner) return cmd } @@ -84,6 +85,8 @@ func preRun(cmd *cobra.Command, args []string) (err error) { flags.BindNoJob(cmd) flags.BindRemoteGzip(cmd) action.RemoteGzip = viper.GetBool("remote-gzip") + flags.BindSpinner(cmd) + action.Spinner = viper.GetString("spinner.name") if len(args) > 0 { action.Filename = args[0] diff --git a/cmd/restore/restore.go b/cmd/restore/restore.go index b614687d..1c3a3d34 100644 --- a/cmd/restore/restore.go +++ b/cmd/restore/restore.go @@ -50,6 +50,7 @@ Supported Input Filetypes: flags.Quiet(cmd, &action.Quiet) flags.RemoteGzip(cmd) flags.Analyze(cmd) + flags.Spinner(cmd, &action.Spinner) cmd.Flags().BoolVarP(&action.Force, "force", "f", false, "Do not prompt before restore") return cmd @@ -84,6 +85,8 @@ func preRun(cmd *cobra.Command, args []string) (err error) { action.Analyze = viper.GetBool("restore.analyze") flags.BindJobPodLabels(cmd) flags.BindNoJob(cmd) + flags.BindSpinner(cmd) + action.Spinner = viper.GetString("spinner.name") if len(args) > 0 { action.Filename = args[0] diff --git a/go.mod b/go.mod index 39227b75..e8a54ff3 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/AlecAivazis/survey/v2 v2.3.7 github.com/alessio/shellescape v1.4.2 + github.com/gabe565/go-spinners v1.0.1 github.com/jedib0t/go-pretty/v6 v6.4.7 github.com/klauspost/pgzip v1.2.6 github.com/mattn/go-isatty v0.0.19 diff --git a/go.sum b/go.sum index f2b8d77c..f10e2162 100644 --- a/go.sum +++ b/go.sum @@ -80,6 +80,8 @@ github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0X github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/gabe565/go-spinners v1.0.1 h1:E8C4qV+TxyvSPpIqAjE6gPNsZ9bqb8aHPiVxMPwPBk0= +github.com/gabe565/go-spinners v1.0.1/go.mod h1:xB3UuNr3Rb97UoOcX7Irq5UWFhpPuieXUMnZ+KsIXck= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= diff --git a/internal/actions/dump/dump.go b/internal/actions/dump/dump.go index 0e5450c4..00804cdd 100644 --- a/internal/actions/dump/dump.go +++ b/internal/actions/dump/dump.go @@ -67,7 +67,7 @@ func (action Dump) Run(ctx context.Context) (err error) { startTime := time.Now() - bar := progressbar.New(-1, "downloading") + bar := progressbar.New(-1, "downloading", action.Spinner) plogger := progressbar.NewBarSafeLogger(os.Stderr, bar) log.SetOutput(plogger) diff --git a/internal/actions/restore/restore.go b/internal/actions/restore/restore.go index 0210af72..98f9e452 100644 --- a/internal/actions/restore/restore.go +++ b/internal/actions/restore/restore.go @@ -45,7 +45,7 @@ func (action Restore) Run(ctx context.Context) (err error) { startTime := time.Now() - bar := progressbar.New(-1, "uploading") + bar := progressbar.New(-1, "uploading", action.Spinner) errLog := progressbar.NewBarSafeLogger(os.Stderr, bar) outLog := progressbar.NewBarSafeLogger(os.Stdout, bar) log.SetOutput(errLog) diff --git a/internal/config/dump.go b/internal/config/dump.go index c4d82efc..e7646a3c 100644 --- a/internal/config/dump.go +++ b/internal/config/dump.go @@ -10,4 +10,5 @@ type Dump struct { Tables []string ExcludeTable []string ExcludeTableData []string + Spinner string } diff --git a/internal/config/flags/generic.go b/internal/config/flags/generic.go new file mode 100644 index 00000000..70a9f34b --- /dev/null +++ b/internal/config/flags/generic.go @@ -0,0 +1,21 @@ +package flags + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +const DefaultSpinner = "sand" + +func Spinner(cmd *cobra.Command, p *string) { + cmd.Flags().StringVar(p, "spinner", DefaultSpinner, "Spinner from https://jsfiddle.net/sindresorhus/2eLtsbey/embedded/result/") + if err := cmd.Flags().MarkHidden("spinner"); err != nil { + panic(err) + } +} + +func BindSpinner(cmd *cobra.Command) { + if err := viper.BindPFlag("spinner.name", cmd.Flags().Lookup("spinner")); err != nil { + panic(err) + } +} diff --git a/internal/config/restore.go b/internal/config/restore.go index 82e4c5f5..a8a7ee1f 100644 --- a/internal/config/restore.go +++ b/internal/config/restore.go @@ -7,4 +7,5 @@ type Restore struct { Clean bool NoOwner bool Force bool + Spinner string } diff --git a/internal/progressbar/new.go b/internal/progressbar/new.go index f6b63017..c705b776 100644 --- a/internal/progressbar/new.go +++ b/internal/progressbar/new.go @@ -7,18 +7,27 @@ import ( "os" "time" + "github.com/clevyr/kubedb/internal/config/flags" + "github.com/gabe565/go-spinners" "github.com/mattn/go-isatty" "github.com/schollz/progressbar/v3" + log "github.com/sirupsen/logrus" ) -func New(max int64, label string) *progressbar.ProgressBar { +func New(max int64, label string, spinnerKey string) *progressbar.ProgressBar { + s, ok := spinner.Map[spinnerKey] + if !ok { + log.WithField("spinner", spinnerKey).Warn("invalid spinner") + s = spinner.Map[flags.DefaultSpinner] + } + options := []progressbar.Option{ progressbar.OptionSetDescription(label), progressbar.OptionSetWriter(os.Stderr), progressbar.OptionShowBytes(true), progressbar.OptionSetWidth(10), progressbar.OptionShowCount(), - progressbar.OptionSpinnerType(14), + progressbar.OptionSpinnerCustom(s.Frames), progressbar.OptionFullWidth(), }