Skip to content

Commit

Permalink
menu brush up
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 7, 2024
1 parent bbeb09b commit 508b000
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/diag/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions cmd/slackdump/internal/diag/eztest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/diag/rawoutput.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] <id>",
Short: "record raw API output",
Long: `
Expand All @@ -39,6 +38,7 @@ Running this tool may be requested by developers.
PrintFlags: true,
RequireAuth: true,
Commands: nil,
HideWizard: true,
}

type rawOutputParams struct {
Expand Down
7 changes: 4 additions & 3 deletions cmd/slackdump/internal/diag/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
102 changes: 79 additions & 23 deletions cmd/slackdump/internal/diag/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand All @@ -56,49 +62,99 @@ 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)
if len(dir) == 0 {
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.
},
},
}
}
3 changes: 3 additions & 0 deletions cmd/slackdump/internal/golang/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 6 additions & 4 deletions cmd/slackdump/internal/ui/cfgui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/slackdump/internal/wizard/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 508b000

Please sign in to comment.