Skip to content

Commit

Permalink
feat(cmd loading): add loading indicator for long-running commands
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Sep 14, 2018
1 parent 0e906ae commit d988843
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 12 deletions.
3 changes: 3 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func (o *AddOptions) Complete(f Factory) (err error) {

// Run adds another peer's dataset to this user's repo
func (o *AddOptions) Run(args []string) error {
spinner.Start()
defer spinner.Stop()

for _, arg := range args {
ref, err := parseCmdLineDatasetRef(arg)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (o *BodyOptions) Complete(f Factory, args []string) (err error) {

// Run executes the body command
func (o *BodyOptions) Run() error {
spinner.Start()
defer spinner.Stop()

if o.UsingRPC {
return usingRPCError("body")
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import (
"github.com/spf13/cobra"
)

func init() {
// disable spinner output during testing
spinner.Writer = ioutil.Discard
}

func confirmQriNotRunning() error {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", config.DefaultAPIPort))
if err != nil {
Expand Down
22 changes: 10 additions & 12 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ import (
"github.com/spf13/cobra"
)


// TODO: Tests.


const providingSecretWarningMessage = `
Warning: You are providing secrets to a dataset transformation.
Never provide secrets to a transformation you do not trust.
continue?`


// NewNewCommand creates a new command
func NewNewCommand(f Factory, ioStreams IOStreams) *cobra.Command {
o := &NewOptions{IOStreams: ioStreams}
Expand Down Expand Up @@ -75,13 +72,13 @@ create a dataset with a dataset data file:
type NewOptions struct {
IOStreams

File string
BodyPath string
Title string
Message string
Private bool
Publish bool
Secrets []string
File string
BodyPath string
Title string
Message string
Private bool
Publish bool
Secrets []string

DatasetRequests *lib.DatasetRequests
}
Expand All @@ -95,8 +92,9 @@ func (o *NewOptions) Complete(f Factory) (err error) {
}

// Run creates a new dataset
func (o *NewOptions) Run(args []string) error {
var err error
func (o *NewOptions) Run(args []string) (err error) {
spinner.Start()
defer spinner.Stop()

if o.File == "" && o.BodyPath == "" {
return fmt.Errorf("creating new dataset needs either --file or --body")
Expand Down
6 changes: 6 additions & 0 deletions cmd/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func (o *RegistryOptions) Complete(f Factory, args []string) (err error) {
// Publish executes the publish command
func (o *RegistryOptions) Publish() error {
var res bool
spinner.Start()
defer spinner.Stop()

for _, arg := range o.Refs {
ref, err := repo.ParseDatasetRef(arg)
if err != nil {
Expand All @@ -127,6 +130,9 @@ func (o *RegistryOptions) Publish() error {
// Unpublish executes the unpublish command
func (o *RegistryOptions) Unpublish() error {
var res bool
spinner.Start()
defer spinner.Stop()

for _, arg := range o.Refs {
ref, err := repo.ParseDatasetRef(arg)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (o *RenderOptions) Complete(f Factory, args []string) (err error) {
func (o *RenderOptions) Run() (err error) {
var template []byte

spinner.Start()
defer spinner.Stop()

ref, err := repo.ParseDatasetRef(o.Ref)
if err != nil && err != repo.ErrEmptyRef {
return err
Expand Down
4 changes: 4 additions & 0 deletions cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ func (o *SaveOptions) Validate() error {

// Run executes the save command
func (o *SaveOptions) Run() (err error) {
spinner.Start()
defer spinner.Stop()

ref, err := parseCmdLineDatasetRef(o.Ref)
if err != nil && o.FilePath == "" {
return lib.NewError(lib.ErrBadArgs, "error parsing dataset reference '"+o.Ref+"'")
Expand Down Expand Up @@ -183,6 +186,7 @@ continue?`, true) {
return err
}

spinner.Stop()
printSuccess(o.Out, "dataset saved: %s", res)
if res.Dataset.Structure.ErrCount > 0 {
printWarning(o.Out, fmt.Sprintf("this dataset has %d validation errors", res.Dataset.Structure.ErrCount))
Expand Down
3 changes: 3 additions & 0 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func (o *SearchOptions) Validate() error {

// Run executes the search command
func (o *SearchOptions) Run() (err error) {
spinner.Start()
defer spinner.Stop()

// TODO: add reindex option back in

Expand All @@ -87,6 +89,7 @@ func (o *SearchOptions) Run() (err error) {
return err
}

spinner.Stop()
switch o.Format {
case "":
fmt.Fprintf(o.Out, "showing %d results for '%s'\n", len(results), o.Query)
Expand Down
5 changes: 5 additions & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func (o *ValidateOptions) Run() (err error) {
dataFile, schemaFile *os.File
ref repo.DatasetRef
)
spinner.Start()
defer spinner.Stop()

ref, err = repo.ParseDatasetRef(o.Ref)
if err != nil && err != repo.ErrEmptyRef {
Expand Down Expand Up @@ -147,6 +149,9 @@ func (o *ValidateOptions) Run() (err error) {
if err = o.DatasetRequests.Validate(p, &res); err != nil {
return err
}

spinner.Stop()

if len(res) == 0 {
printSuccess(o.Out, "✔ All good!")
return
Expand Down

0 comments on commit d988843

Please sign in to comment.