Skip to content

Commit

Permalink
add archive wizard stub
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Oct 13, 2024
1 parent dbcb576 commit 564e44c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
3 changes: 3 additions & 0 deletions auth/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var ErrNoProvider = errors.New("internal error: no provider in context")

// FromContext returns the auth provider from the context.
func FromContext(ctx context.Context) (Provider, error) {
if ctx == nil {
return nil, errors.New("internal error: nil context")
}
prov, ok := ctx.Value(providerKey).(Provider)
if !ok {
return nil, ErrNoProvider
Expand Down
63 changes: 63 additions & 0 deletions cmd/slackdump/internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
_ "embed"
"errors"
"fmt"
"strings"
"time"

"github.com/charmbracelet/huh"
"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
Expand All @@ -28,12 +30,14 @@ var CmdArchive = &base.Command{
Short: "archive the workspace or individual conversations on disk",
Long: mdArchive,
FlagMask: cfg.OmitUserCacheFlag | cfg.OmitCacheDir,
Wizard: archiveWizard,
RequireAuth: true,
PrintFlags: true,
}

const zipExt = ".ZIP"

// StripZipExt removes the .zip extension from the string.
func StripZipExt(s string) string {
if strings.HasSuffix(strings.ToUpper(s), zipExt) {
return s[:len(s)-len(zipExt)]
Expand Down Expand Up @@ -101,3 +105,62 @@ func resultLogger(lg logger.Interface) func(sr stream.Result) error {
return nil
}
}

func archiveWizard(ctx context.Context, cmd *base.Command, args []string) error {
selected := "dates"
LOOP:
for {
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Key("selection").
Title("Select the workspace or conversation to archive").
Description("Select the workspace or conversation to archive").
Options(
huh.NewOption("Specify date range", "dates"),
huh.NewOption("Custom API limits config file", "config"),
huh.NewOption(fmt.Sprintf("Enterprise mode enabled? (%v)", cfg.ForceEnterprise), "enterprise"),
huh.NewOption(fmt.Sprintf("Export files? (%v)", cfg.DownloadFiles), "files"),
huh.NewOption(fmt.Sprintf("Output directory: %q", StripZipExt(cfg.Output)), "output"),
huh.NewOption("Run!", "run"),
huh.NewOption(strings.Repeat("-", 10), ""),
huh.NewOption("Exit archive wizard", "exit"),
).Value(&selected).
DescriptionFunc(func() string {
switch selected {
case "dates":
return "Specify the date range for the archive"
case "config":
return "Specify the custom API limits configuration file"
case "enterprise":
return "Enable or disable enterprise mode"
case "files":
return "Enable or disable files download"
case "output":
return "Specify the output directory, or use the default one"
case "run":
return "Run the archive"
case "exit":
return "Exit the archive wizard"
default:
return ""
}
}, &selected).
WithTheme(cfg.Theme),
),
)
if err := form.Run(); err != nil {
return err
}
switch selected {
case "exit":
break LOOP
case "enterprise":
cfg.ForceEnterprise = !cfg.ForceEnterprise
case "files":
cfg.DownloadFiles = !cfg.DownloadFiles
}
}

return nil
}
45 changes: 45 additions & 0 deletions cmd/slackdump/internal/archive/archive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package archive

import (
_ "embed"
"testing"
)

func TestStripZipExt(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
"zip",
args{"foo.zip"},
"foo",
},
{
"tar.gz",
args{"foo.tar.gz"},
"foo.tar.gz",
},
{
"ZIP",
args{"foo.ZIP"},
"foo",
},
{
"empty",
args{""},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StripZipExt(tt.args.s); got != tt.want {
t.Errorf("StripZipExt() = %v, want %v", got, tt.want)
}
})
}
}
12 changes: 6 additions & 6 deletions cmd/slackdump/internal/wizard/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ func runWizard(ctx context.Context, cmd *base.Command, args []string) error {

menu := makeMenu(baseCommands, "", "What would you like to do?")
if err := show(menu, func(cmd *base.Command) error {
var cmdCtx context.Context
ctx, cancel := context.WithCancel(ctx)
defer cancel()

if cmd.RequireAuth {
var err error
ctx, err = bootstrap.CurrentProviderCtx(ctx)
if err != nil {
return err
}
} else {
cmdCtx = ctx
}
return cmd.Wizard(cmdCtx, cmd, args)
return cmd.Wizard(ctx, cmd, args)
}); err != nil {
base.SetExitStatus(base.SApplicationError)
return fmt.Errorf("error running wizard: %s", err)
Expand Down Expand Up @@ -115,9 +115,9 @@ func makeMenu(cmds []*base.Command, parent string, title string) (m *menu) {
m.Add(item)
}
if parent != "" {
// m.Add(miExit)
// } else {
m.Add(miBack)
} else {
m.Add(miExit)
}
return
}
Expand Down

0 comments on commit 564e44c

Please sign in to comment.