Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wizardry #303

Merged
merged 18 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.21"
go-version: "1.22"

- name: Build
run: go build -v ./...
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- run: git fetch --force --tags
- uses: actions/setup-go@v5
with:
go-version: '>=1.19.5'
go-version: '>=1.22.0'
cache: true
# More assembly might be required: Docker logins, GPG, etc. It all depends
# on your needs.
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/apiconfig/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Config command allows to perform different operations on the API limits
configuration file.
`,
Commands: []*base.Command{
CmdConfigNew,
CmdConfigCheck,
CmdConfigNew,
},
}

Expand Down
47 changes: 37 additions & 10 deletions cmd/slackdump/internal/apiconfig/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"context"
"errors"
"fmt"
"os"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/rusq/rbubbles/filemgr"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v3/internal/ui"
)

var CmdConfigCheck = &base.Command{
Expand Down Expand Up @@ -37,23 +42,45 @@ func runConfigCheck(ctx context.Context, cmd *base.Command, args []string) error
return errors.New("config filename must be specified")
}
filename := args[0]
if _, err := Load(filename); err != nil {
if err := checkFile(filename); err != nil {
base.SetExitStatus(base.SUserError)
return fmt.Errorf("config file %q not OK: %s", filename, err)
return err
}
fmt.Printf("Config file %q: OK\n", filename)
return nil
}

func checkFile(filename string) error {
if _, err := Load(filename); err != nil {
return fmt.Errorf("config file %q not OK: %s", filename, err)
}
return nil
}

func wizConfigCheck(ctx context.Context, cmd *base.Command, args []string) error {
filename, err := ui.FileSelector(
"Input a config file name to check",
"Enter the config file name. It must exist and be a regular file.",
ui.WithMustExist(true),
)
if err != nil {
f := filemgr.New(os.DirFS("."), ".", 15, "*.yaml", "*.yml")
f.Focus()
f.ShowHelp = true
f.Style = filemgr.Style{
Normal: cfg.Theme.Focused.File,
Directory: cfg.Theme.Focused.Directory,
Inverted: lipgloss.NewStyle().
Foreground(cfg.Theme.Focused.FocusedButton.GetForeground()).
Background(cfg.Theme.Focused.FocusedButton.GetBackground()),
}
vp := viewport.New(80-filemgr.Width, f.Height)
vp.Style = lipgloss.NewStyle().Border(lipgloss.DoubleBorder(), true).Margin(0, 2)
vp.SetContent("Select a config file to check and press [Enter].")
m := checkerModel{
files: f,
view: vp,
BorderSel: cfg.Theme.Focused.FocusedButton.GetBackground(), // TODO: I DONT LIKE THIS!
BorderBlur: cfg.Theme.Focused.Directory.GetForeground(), // TODO: AND THIS TOO, THIS MUST GO! but ok for now.
}

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

return runConfigCheck(ctx, cmd, []string{filename})
return nil
}
113 changes: 113 additions & 0 deletions cmd/slackdump/internal/apiconfig/checker_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package apiconfig

import (
"strings"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/rusq/rbubbles/filemgr"
)

type checkerModel struct {
files filemgr.Model
view viewport.Model
BorderBlur lipgloss.TerminalColor
BorderSel lipgloss.TerminalColor
viewing bool
width int
finishing bool
}

func (m checkerModel) Init() tea.Cmd {
return tea.Batch(m.files.Init(), m.view.Init())
}

func (m checkerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var keymsg bool
switch msg := msg.(type) {
case wmSetText:
m.view.Style.Foreground(msg.style.GetForeground())
m.view.SetContent(msg.text)
case tea.WindowSizeMsg:
m.width = msg.Width
m.view.Width = msg.Width - filemgr.Width
case tea.KeyMsg:
keymsg = true
switch msg.String() {
case "ctrl+c", "q":
m.finishing = true
return m, tea.Quit
case "tab":
if !m.viewing {
m.viewing = true
m.files.Blur()
} else {
m.viewing = false
m.files.Focus()
}
}
case filemgr.WMSelected:
filename := msg.Filepath
if err := checkFile(filename); err != nil {
cmds = append(cmds, wcmdErr(filename, err))
} else {
cmds = append(cmds, wcmdOK(filename))
}
}

var cmd tea.Cmd
m.files, cmd = m.files.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
}
if !keymsg || m.viewing {
// we do not propagate key messages to the viewport.
m.view, cmd = m.view.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
}
}
return m, tea.Batch(cmds...)
}

func (m checkerModel) View() string {
const crlf = "\r\n"
if m.finishing {
return ""
}
if m.viewing {
m.view.Style.BorderForeground(m.BorderSel)
} else {
m.view.Style.BorderForeground(m.BorderBlur)
}
var buf strings.Builder
buf.WriteString(strings.Repeat("⎯", m.width) + crlf)
buf.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, m.files.View(), m.view.View()) + crlf)
buf.WriteString(strings.Repeat("⎯", m.width))
return buf.String()
}

type wmSetText struct {
text string
style lipgloss.Style
}

func wcmdErr(_ string, err error) tea.Cmd {
return func() tea.Msg {
return wmSetText{
text: err.Error(),
style: lipgloss.NewStyle().Foreground(lipgloss.Color("#ff0000")),
}
}
}

func wcmdOK(filename string) tea.Cmd {
return func() tea.Msg {
return wmSetText{
text: "Config file OK: " + filename,
style: lipgloss.NewStyle().Foreground(lipgloss.Color("#00ff00")),
}
}
}
3 changes: 2 additions & 1 deletion cmd/slackdump/internal/apiconfig/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"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/ui"
"github.com/rusq/slackdump/v3/internal/network"
"github.com/rusq/slackdump/v3/internal/ui"
)

var CmdConfigNew = &base.Command{
Expand Down Expand Up @@ -79,6 +79,7 @@ RESTART:
if err != nil {
return err
}
filename = maybeFixExt(filename)
if err := Save(filename, network.DefLimits); err != nil {
fmt.Printf("Error: %s, please retry\n", err)
trace.Logf(ctx, "error", "error saving file to %q: %s, survey restarted", filename, err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/slackdump/internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
//go:embed assets/archive.md
var mdArchive string

var CmdRecord = &base.Command{
Run: RunRecord,
var CmdArchive = &base.Command{
Run: RunArchive,
UsageLine: "slackdump archive [flags] [link1[ link 2[ link N]]]",
Short: "archive the workspace or individual conversations on disk",
Long: mdArchive,
Expand All @@ -44,7 +44,7 @@ var (
errNoOutput = errors.New("output directory is required")
)

func RunRecord(ctx context.Context, cmd *base.Command, args []string) error {
func RunArchive(ctx context.Context, cmd *base.Command, args []string) error {
list, err := structures.NewEntityList(args)
if err != nil {
base.SetExitStatus(base.SUserError)
Expand Down
26 changes: 26 additions & 0 deletions cmd/slackdump/internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Package bootstrap contains some initialisation functions that are shared
// between main some other top level commands, i.e. wizard.
package bootstrap

import (
"context"

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/workspace"
)

// CurrentProvider is a shortcut function to initialise the current auth
// provider.
func CurrentProvider(ctx context.Context) (auth.Provider, error) {
return workspace.AuthCurrent(ctx, cfg.CacheDir(), cfg.Workspace, cfg.LegacyBrowser)
}

// CurrentProviderCtx returns the context with the current provider.
func CurrentProviderCtx(ctx context.Context) (context.Context, error) {
prov, err := workspace.AuthCurrent(ctx, cfg.CacheDir(), cfg.Workspace, cfg.LegacyBrowser)
if err != nil {
return nil, err
}
return auth.WithContext(ctx, prov), nil
}
5 changes: 5 additions & 0 deletions cmd/slackdump/internal/cfg/wizard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cfg

import "github.com/charmbracelet/huh"

var Theme = huh.ThemeCharm() // Theme is the default Wizard theme.
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/diag/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"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/internal/ui"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui"
"github.com/rusq/slackdump/v3/logger"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/emoji/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

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

func wizard(ctx context.Context, cmd *base.Command, args []string) error {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"strings"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui"
"github.com/rusq/slackdump/v3/internal/structures"
"github.com/rusq/slackdump/v3/internal/ui"
)

// ConversationList asks the user for the list of conversations to dump or
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"time"

"github.com/rusq/slackdump/v3/internal/ui"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui"
)

func MaybeTimeRange() (oldest, latest time.Time, err error) {
Expand Down
19 changes: 19 additions & 0 deletions cmd/slackdump/internal/ui/confirm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ui

import (
"github.com/charmbracelet/huh"
)

func Confirm(msg string, _ bool, opt ...Option) (bool, error) {
var b bool
if err := FieldConfirm(&b, msg, false, opt...).Run(); err != nil {
return false, err
}
return b, nil
}

func FieldConfirm(b *bool, msg string, _ bool, opt ...Option) huh.Field {
var opts = defaultOpts().apply(opt...)
f := huh.NewConfirm().Title(msg).Description(opts.help).Value(b)
return f
}
Loading
Loading