Skip to content

Commit

Permalink
break dep cycle, workspace new
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 10, 2024
1 parent f77347b commit 8b65cc7
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 60 deletions.
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/diag/wizdebug.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func runWizDebug(ctx context.Context, cmd *base.Command, args []string) error {
}

func debugDumpUI(ctx context.Context) error {
mnu := []menu.MenuItem{
mnu := []menu.Item{
{
ID: "run",
Name: "Run",
Expand Down
8 changes: 4 additions & 4 deletions cmd/slackdump/internal/ui/bubbles/menu/menuitem.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package menu

// MenuItem is an item in a menu.
type MenuItem struct {
// Item is an item in a menu.
type Item struct {
// ID is an arbitrary ID, up to caller.
ID string
// Separator is a flag that determines whether the item is a separator or
Expand All @@ -24,11 +24,11 @@ type MenuItem struct {
Validate func() error // when to enable the item
}

func (m MenuItem) IsDisabled() bool {
func (m Item) IsDisabled() bool {
return m.Validate != nil && m.Validate() != nil
}

func (m MenuItem) DisabledReason() string {
func (m Item) DisabledReason() string {
if m.Validate != nil {
if err := m.Validate(); err != nil {
return err.Error()
Expand Down
8 changes: 4 additions & 4 deletions cmd/slackdump/internal/ui/bubbles/menu/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

type Model struct {
// Selected will be set to the selected item from the items.
Selected MenuItem
Selected Item
Cancelled bool

title string
items []MenuItem
items []Item
finishing bool
focused bool
preview bool // preview child model
Expand All @@ -31,7 +31,7 @@ type Model struct {
cursor int
}

func New(title string, items []MenuItem, preview bool) *Model {
func New(title string, items []Item, preview bool) *Model {
return &Model{
title: title,
items: items,
Expand Down Expand Up @@ -164,7 +164,7 @@ func (m *Model) view() string {
// Header
p(sty.Title.Render(m.title) + "\n")
if currentDisabled {
p(sty.Description.Render("Requirements not satisfied: " + capfirst(currentItem.Validate().Error())))
p(sty.Description.Render("Requirements not met: " + capfirst(currentItem.Validate().Error())))
} else {
p(sty.Description.Render(m.items[m.cursor].Help))
}
Expand Down
11 changes: 0 additions & 11 deletions cmd/slackdump/internal/ui/cfgui/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/rusq/rbubbles/filemgr"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/apiconfig"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/updaters"
)
Expand All @@ -32,16 +31,6 @@ type Parameter struct {

func globalConfig() Configuration {
return Configuration{
{
Name: "Authentication",
Params: []Parameter{
{
Name: "Slack Workspace",
Value: bootstrap.CurrentWsp(),
Description: "Currently selected Slack Workspace",
},
},
},
{
Name: "Timeframe",
Params: []Parameter{
Expand Down
16 changes: 8 additions & 8 deletions cmd/slackdump/internal/ui/dumpui/dumpui.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ var description = map[string]string{

func (w *Wizard) Run(ctx context.Context) error {
var menu = func() *menu.Model {
var items []menu.MenuItem
var items []menu.Item
if w.LocalConfig != nil {
items = append(items, menu.MenuItem{
items = append(items, menu.Item{
ID: actLocalConfig,
Name: w.Name + " Options...",
Help: description[actLocalConfig],
Expand All @@ -57,7 +57,7 @@ func (w *Wizard) Run(ctx context.Context) error {

items = append(
items,
menu.MenuItem{
menu.Item{
ID: actRun,
Name: "Run " + w.Name,
Help: description[actRun],
Expand All @@ -70,23 +70,23 @@ func (w *Wizard) Run(ctx context.Context) error {
},
)
if w.Help != "" {
items = append(items, menu.MenuItem{
items = append(items, menu.Item{
ID: "help",
Name: "Help",
Help: "Read help for " + w.Name,
})
}

items = append(items,
menu.MenuItem{Separator: true},
menu.MenuItem{
menu.Item{Separator: true},
menu.Item{
ID: actGlobalConfig,
Name: "Global Configuration...",
Help: description[actGlobalConfig],
Model: cfgui.NewConfigUI(cfgui.DefaultStyle(), cfgui.GlobalConfig), // TODO: filthy cast
},
menu.MenuItem{Separator: true},
menu.MenuItem{ID: actExit, Name: "Exit", Help: description[actExit]},
menu.Item{Separator: true},
menu.Item{ID: actExit, Name: "Exit", Help: description[actExit]},
)

return menu.New(w.Title, items, true)
Expand Down
2 changes: 2 additions & 0 deletions cmd/slackdump/internal/workspace/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace/workspaceui"
"github.com/rusq/slackdump/v3/internal/cache"
)

Expand All @@ -23,6 +24,7 @@ var CmdWspNew = &base.Command{
`,
FlagMask: flagmask &^ cfg.OmitAuthFlags, // only auth flags.
PrintFlags: true,
Wizard: workspaceui.WorkspaceNew,
}

var newParams = struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,37 @@ import (

// TODO: organise as a self-sufficient model with proper error handling.

func WorkspaceSelectModel(ctx context.Context, m *cache.Manager) (tea.Model, error) {
func wizSelect(ctx context.Context, cmd *base.Command, args []string) error {
m, err := cache.NewManager(cfg.CacheDir())
if err != nil {
base.SetExitStatus(base.SCacheError)
return err
}

sm, err := workspaceSelectModel(ctx, m)
if err != nil {
return err
}
if sm == nil {
// TODO: handle this case
return nil
}
mod, err := tea.NewProgram(sm).Run()
if err != nil {
return fmt.Errorf("workspace select wizard error: %w", err)
}
if newWsp := mod.(selectModel).selected; newWsp != "" {
if err := m.Select(newWsp); err != nil {
base.SetExitStatus(base.SWorkspaceError)
return fmt.Errorf("error setting the current workspace: %s", err)
}
logger.FromContext(ctx).Debugf("selected workspace: %s", newWsp)
}

return nil
}

func workspaceSelectModel(ctx context.Context, m *cache.Manager) (tea.Model, error) {
wspList, err := m.List()
if err != nil {
base.SetExitStatus(base.SCacheError)
Expand All @@ -39,7 +69,7 @@ func WorkspaceSelectModel(ctx context.Context, m *cache.Manager) (tea.Model, err
{Title: "Name", Width: 14},
{Title: "Team", Width: 15},
{Title: "User", Width: 15},
{Title: "Error", Width: 30},
{Title: "Status", Width: 30},
}

var rows []table.Row
Expand Down Expand Up @@ -69,36 +99,6 @@ func WorkspaceSelectModel(ctx context.Context, m *cache.Manager) (tea.Model, err
}, nil
}

func wizSelect(ctx context.Context, cmd *base.Command, args []string) error {
m, err := cache.NewManager(cfg.CacheDir())
if err != nil {
base.SetExitStatus(base.SCacheError)
return err
}

sm, err := WorkspaceSelectModel(ctx, m)
if err != nil {
return err
}
if sm == nil {
// TODO: handle this case
return nil
}
mod, err := tea.NewProgram(sm).Run()
if err != nil {
return fmt.Errorf("workspace select wizard error: %w", err)
}
if newWsp := mod.(selectModel).selected; newWsp != "" {
if err := m.Select(newWsp); err != nil {
base.SetExitStatus(base.SWorkspaceError)
return fmt.Errorf("error setting the current workspace: %s", err)
}
logger.FromContext(ctx).Debugf("selected workspace: %s", newWsp)
}

return nil
}

type selectModel struct {
table table.Model
selected string
Expand Down
61 changes: 61 additions & 0 deletions cmd/slackdump/internal/workspace/workspaceui/wsp_new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package workspaceui

import (
"context"

tea "github.com/charmbracelet/bubbletea"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/bubbles/menu"
)

type wizModel struct {
m *menu.Model
}

func WorkspaceNew(ctx context.Context, _ *base.Command, _ []string) error {
items := []menu.Item{
{
ID: "ezlogin",
Name: "Login in Browser",
Help: "Login to Slack in your browser",
},
{
ID: "token",
Name: "Token/Cookie",
Help: "Enter token and cookie that you grabbed from the browser",
},
{
ID: "secrets",
Name: "From file with secrets",
Help: "Read from secrets.txt or .env file",
},
{
Separator: true,
},
{
ID: "exit",
Name: "Exit",
Help: "Exit to main menu",
},
}

m := menu.New("New Workspace", items, true)

if _, err := tea.NewProgram(&wizModel{m: m}, tea.WithContext(ctx)).Run(); err != nil {
return err
}
return nil
}

func (m *wizModel) Init() tea.Cmd {
return m.m.Init()
}

func (m *wizModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.m.Update(msg)
}

func (m *wizModel) View() string {
return m.m.View()
}

0 comments on commit 8b65cc7

Please sign in to comment.