Skip to content

Commit

Permalink
fix wizard error handling and config new wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Dec 10, 2022
1 parent 5b5980f commit 559aa7e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 27 additions & 2 deletions cmd/slackdump/internal/apiconfig/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/internal/app/ui"
)

var CmdConfigNew = &base.Command{
Expand All @@ -36,6 +37,7 @@ var (

func init() {
CmdConfigNew.Run = runConfigNew
CmdConfigNew.Wizard = wizConfigNew
}

func runConfigNew(ctx context.Context, cmd *base.Command, args []string) error {
Expand All @@ -59,8 +61,31 @@ func runConfigNew(ctx context.Context, cmd *base.Command, args []string) error {
return fmt.Errorf("error writing the API limits config %q: %w", filename, err)
}

fmt.Printf("Your new API limits config is ready: %q\n", filename)
return nil
_, err := printConfigOK(filename)
return err
}

func printConfigOK(filename string) (n int, err error) {
return fmt.Printf("Your new API limits config is ready: %q\n", filename)
}

func wizConfigNew(ctx context.Context, cmd *base.Command, args []string) error {
ctx, task := trace.NewTask(ctx, "wizConfigNew")
defer task.End()

RESTART:
filename, err := ui.FileSelector("New config file name", "Enter new limiter config file name")
if err != nil {
return err
}
if err := Save(filename, slackdump.DefOptions.Limits); err != nil {
fmt.Printf("Error: %s, please retry\n", err)
trace.Logf(ctx, "error", "error saving file to %q: %s, survey restarted", filename, err)
goto RESTART
}

_, err = printConfigOK(filename)
return err
}

// shouldOverwrite returns true if the file can be overwritten. If override
Expand Down
5 changes: 3 additions & 2 deletions cmd/slackdump/internal/wizard/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func show(m *menu, onMatch func(cmd *base.Command) error) error {
return nil
} else if errors.Is(err, errInvalid) {
return err
} else {
return err
}
}
}
Expand All @@ -138,8 +140,7 @@ func run(m *menu, choice string, onMatch func(cmd *base.Command) error) error {
continue
}
if mi.Submenu != nil {
show(mi.Submenu, onMatch)
return nil
return show(mi.Submenu, onMatch)
}
// found
if mi.cmd == nil { // only Exit and back won't have this.
Expand Down

0 comments on commit 559aa7e

Please sign in to comment.