Skip to content

Commit

Permalink
feat(flags): Add configurable spinner easter egg
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 10, 2023
1 parent 2683aba commit c48420e
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion internal/actions/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/actions/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/config/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ type Dump struct {
Tables []string
ExcludeTable []string
ExcludeTableData []string
Spinner string
}
21 changes: 21 additions & 0 deletions internal/config/flags/generic.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
1 change: 1 addition & 0 deletions internal/config/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type Restore struct {
Clean bool
NoOwner bool
Force bool
Spinner string
}
13 changes: 11 additions & 2 deletions internal/progressbar/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}

Expand Down

0 comments on commit c48420e

Please sign in to comment.