From 508b000c140f76f5f1e114860bdf2a29d6c0df29 Mon Sep 17 00:00:00 2001 From: Rustam Gilyazov <16064414+rusq@users.noreply.github.com> Date: Thu, 7 Nov 2024 22:15:12 +1000 Subject: [PATCH] menu brush up --- cmd/slackdump/internal/diag/edge.go | 2 +- cmd/slackdump/internal/diag/eztest.go | 2 - cmd/slackdump/internal/diag/rawoutput.go | 2 +- cmd/slackdump/internal/diag/record.go | 7 +- cmd/slackdump/internal/diag/uninstall.go | 102 ++++++++++++++++----- cmd/slackdump/internal/golang/base/base.go | 3 + cmd/slackdump/internal/ui/cfgui/model.go | 10 +- cmd/slackdump/internal/wizard/wizard.go | 3 +- 8 files changed, 95 insertions(+), 36 deletions(-) diff --git a/cmd/slackdump/internal/diag/edge.go b/cmd/slackdump/internal/diag/edge.go index 7d669676..c538138a 100644 --- a/cmd/slackdump/internal/diag/edge.go +++ b/cmd/slackdump/internal/diag/edge.go @@ -13,10 +13,10 @@ import ( var CmdEdge = &base.Command{ Run: runEdge, - Wizard: func(ctx context.Context, cmd *base.Command, args []string) error { panic("not implemented") }, UsageLine: "slack tools edge", Short: "Edge test", RequireAuth: true, + HideWizard: true, Long: ` # Slack Edge API test tool diff --git a/cmd/slackdump/internal/diag/eztest.go b/cmd/slackdump/internal/diag/eztest.go index 68f9a9ac..f3aaef4f 100644 --- a/cmd/slackdump/internal/diag/eztest.go +++ b/cmd/slackdump/internal/diag/eztest.go @@ -16,7 +16,6 @@ import ( var CmdEzTest = &base.Command{ Run: runEzLoginTest, - Wizard: func(ctx context.Context, cmd *base.Command, args []string) error { panic("not implemented") }, UsageLine: "slack tools eztest", Short: "EZ-Login 3000 test", Long: ` @@ -87,7 +86,6 @@ func runEzLoginTest(ctx context.Context, cmd *base.Command, args []string) error return errors.New(*res.Err) } return nil - } func tryPlaywrightAuth(ctx context.Context, wsp string) ezResult { diff --git a/cmd/slackdump/internal/diag/rawoutput.go b/cmd/slackdump/internal/diag/rawoutput.go index 1878870a..731e55ee 100644 --- a/cmd/slackdump/internal/diag/rawoutput.go +++ b/cmd/slackdump/internal/diag/rawoutput.go @@ -22,7 +22,6 @@ import ( var CmdRawOutput = &base.Command{ Run: nil, // populated by init to break the init cycle - Wizard: func(context.Context, *base.Command, []string) error { panic("not implemented") }, UsageLine: "slackdump tools rawoutput [flags] ", Short: "record raw API output", Long: ` @@ -39,6 +38,7 @@ Running this tool may be requested by developers. PrintFlags: true, RequireAuth: true, Commands: nil, + HideWizard: true, } type rawOutputParams struct { diff --git a/cmd/slackdump/internal/diag/record.go b/cmd/slackdump/internal/diag/record.go index df716f0f..58bcc49f 100644 --- a/cmd/slackdump/internal/diag/record.go +++ b/cmd/slackdump/internal/diag/record.go @@ -15,9 +15,10 @@ import ( ) var CmdRecord = &base.Command{ - UsageLine: "slackdump tools record", - Short: "chunk record commands", - Commands: []*base.Command{cmdRecordStream, cmdRecordState}, + UsageLine: "slackdump tools record", + Short: "chunk record commands", + Commands: []*base.Command{cmdRecordStream, cmdRecordState}, + HideWizard: true, } var cmdRecordStream = &base.Command{ diff --git a/cmd/slackdump/internal/diag/uninstall.go b/cmd/slackdump/internal/diag/uninstall.go index f62822fb..daf28d11 100644 --- a/cmd/slackdump/internal/diag/uninstall.go +++ b/cmd/slackdump/internal/diag/uninstall.go @@ -12,6 +12,9 @@ import ( "github.com/rusq/slackdump/v3/cmd/slackdump/internal/diag/info" "github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base" "github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui" + "github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/cfgui" + "github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/dumpui" + "github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/updaters" "github.com/rusq/slackdump/v3/logger" ) @@ -24,27 +27,30 @@ var CmdUninstall = &base.Command{ PrintFlags: true, } -// uninstallParams holds supported command line parameters -var uninstallParams = struct { +func init() { + CmdUninstall.Wizard = wizUninstall +} + +type uninstOptions struct { legacy bool // playwright dry bool // dry run noConfirm bool // no confirmation from the user -}{} +} + +// uninstParams holds supported command line parameters +var uninstParams = uninstOptions{} func init() { - CmdUninstall.Flag.BoolVar(&uninstallParams.legacy, "legacy-browser", false, "operate on playwright environment (default: rod envronment)") - CmdUninstall.Flag.BoolVar(&uninstallParams.dry, "dry", false, "dry run") - CmdUninstall.Flag.BoolVar(&uninstallParams.noConfirm, "no-confirm", false, "no confirmation from the user") + CmdUninstall.Flag.BoolVar(&uninstParams.legacy, "legacy-browser", false, "operate on playwright environment (default: rod envronment)") + CmdUninstall.Flag.BoolVar(&uninstParams.dry, "dry", false, "dry run") + CmdUninstall.Flag.BoolVar(&uninstParams.noConfirm, "no-confirm", false, "no confirmation from the user") } func runUninstall(ctx context.Context, cmd *base.Command, args []string) error { if len(args) != 0 { base.SetExitStatus(base.SInvalidParameters) } - if uninstallParams.dry { - return nil - } - if !uninstallParams.noConfirm { + if !uninstParams.noConfirm { confirmed, err := ui.Confirm("This will uninstall the EZ-Login browser", true) if err != nil { return err @@ -56,24 +62,34 @@ func runUninstall(ctx context.Context, cmd *base.Command, args []string) error { si := info.CollectRaw() - if uninstallParams.legacy { - return uninstallPlaywright(ctx, si.Playwright) + if uninstParams.legacy { + return uninstallPlaywright(ctx, si.Playwright, uninstParams.dry) } else { - return uninstallRod(ctx, si.Rod) + return uninstallRod(ctx, si.Rod, uninstParams.dry) } } -func uninstallPlaywright(ctx context.Context, si info.PwInfo) error { - if si.Path == "" { - return errors.New("unable to determine playwright path") +func removeFunc(dry bool) func(string) error { + var removeFn = os.RemoveAll + if dry { + removeFn = func(name string) error { + fmt.Printf("Would remove %s\n", name) + return nil + } } + return removeFn +} + +func uninstallPlaywright(ctx context.Context, si info.PwInfo, dry bool) error { + removeFn := removeFunc(dry) lg := logger.FromContext(ctx) lg.Printf("Deleting %s", si.Path) - if err := os.RemoveAll(si.Path); err != nil { + if err := removeFn(si.Path); err != nil { return fmt.Errorf("failed to remove the playwright library: %w", err) } - lg.Printf("Deleting browsers") - if err := os.RemoveAll(si.BrowsersPath); err != nil { + lg.Printf("Deleting browsers in %s", si.BrowsersPath) + + if err := removeFn(si.BrowsersPath); err != nil { return fmt.Errorf("failed to remove the playwright browsers: %w", err) } dir, _ := filepath.Split(si.Path) @@ -81,24 +97,64 @@ func uninstallPlaywright(ctx context.Context, si info.PwInfo) error { return errors.New("unable to reliably determine playwright path") } lg.Printf("Deleting all playwright versions from: %s", dir) - if err := os.RemoveAll(dir); err != nil { + if err := removeFn(dir); err != nil { return fmt.Errorf("failed to remove the playwright versions: %w", err) } return nil } -func uninstallRod(_ context.Context, si info.RodInfo) error { +func uninstallRod(_ context.Context, si info.RodInfo, dry bool) error { + removeFn := removeFunc(dry) if si.Path == "" { return errors.New("unable to determine rod browser path") } lg := cfg.Log lg.Printf("Deleting incognito Browser...") - _ = slackauth.RemoveBrowser() // just to make sure. + if !dry { + _ = slackauth.RemoveBrowser() // just to make sure. + } else { + lg.Printf("Would remove incognito browser") + } + lg.Printf("Deleting %s...", si.Path) - if err := os.RemoveAll(si.Path); err != nil { + if err := removeFn(si.Path); err != nil { return fmt.Errorf("failed to remove the rod browser: %w", err) } return nil } + +func wizUninstall(ctx context.Context, cmd *base.Command, args []string) error { + w := dumpui.Wizard{ + Name: "Uninstall", + Title: "Uninstall Slackdump", + LocalConfig: uninstParams.configuration, + Cmd: CmdUninstall, + } + return w.Run(ctx) +} + +func (p *uninstOptions) configuration() cfgui.Configuration { + p.noConfirm = true + return cfgui.Configuration{ + { + Name: "Uninstall options", + Params: []cfgui.Parameter{ + { + Name: "Playwright", + Value: cfgui.Checkbox(p.legacy), + Description: "Environment to uninstall (if unselected, uninstalls Rod)", + Updater: updaters.NewBool(&p.legacy), + }, + { + Name: "Dry run", + Value: cfgui.Checkbox(p.dry), + Description: "Do not perform the uninstallation, just show what would be done", + Updater: updaters.NewBool(&p.dry), + }, + // TODO: delete slackdump from user cache options. + }, + }, + } +} diff --git a/cmd/slackdump/internal/golang/base/base.go b/cmd/slackdump/internal/golang/base/base.go index 572ef235..8c56ac9c 100644 --- a/cmd/slackdump/internal/golang/base/base.go +++ b/cmd/slackdump/internal/golang/base/base.go @@ -64,6 +64,9 @@ type Command struct { // The order here is the order in which they are printed by 'slackdump help'. // Note that subcommands are in general best avoided. Commands []*Command + + //HideWizard if set to true disables the display in wizard. + HideWizard bool } var Slackdump = &Command{ diff --git a/cmd/slackdump/internal/ui/cfgui/model.go b/cmd/slackdump/internal/ui/cfgui/model.go index 890f5940..3f234299 100644 --- a/cmd/slackdump/internal/ui/cfgui/model.go +++ b/cmd/slackdump/internal/ui/cfgui/model.go @@ -205,15 +205,17 @@ func (m *Model) view(sty StyleSet) string { } fmt.Fprintf(&buf, alignParam+namefmt.Render(fmt.Sprintf("% *s", keyLen, param.Name))+" ") if selected && m.state == inline { - buf.WriteString(m.child.View() + "\n") + buf.WriteString(m.child.View()) } else { - fmt.Fprintf(&buf, valfmt.Render(fmt.Sprintf("%-*s", valLen, nvl(param.Value)))+"\n") + fmt.Fprint(&buf, valfmt.Render(fmt.Sprintf("%-*s\n", valLen, nvl(param.Value)))) } line++ } } - buf.WriteString(alignGroup + sty.Description.Render(descr) + "\n") - buf.WriteString(m.help.ShortHelpView(m.keymap.Bindings())) + if m.focused { + buf.WriteString(alignGroup + sty.Description.Render(descr)) + buf.WriteString("\n" + m.help.ShortHelpView(m.keymap.Bindings())) + } return buf.String() } diff --git a/cmd/slackdump/internal/wizard/wizard.go b/cmd/slackdump/internal/wizard/wizard.go index 29674e0d..c7aa4d74 100644 --- a/cmd/slackdump/internal/wizard/wizard.go +++ b/cmd/slackdump/internal/wizard/wizard.go @@ -96,14 +96,13 @@ func makeMenu(cmds []*base.Command, parent string, title string) (m *menu) { } for _, cmd := range cmds { hasSubcommands := len(cmd.Commands) > 0 - hasWizard := cmd.Wizard != nil + hasWizard := cmd.Wizard != nil && !cmd.HideWizard isMe := strings.EqualFold(cmd.Name(), CmdWizard.Name()) if !(hasWizard || hasSubcommands) || isMe { continue } name := titlecase.String(cmd.Name()) item := menuitem{ - // Name: parent + name, Name: name, Description: cmd.Short, cmd: cmd,