Skip to content

Commit

Permalink
Merge pull request #384 from rusq/i287
Browse files Browse the repository at this point in the history
members only + minor fixes
  • Loading branch information
rusq authored Dec 21, 2024
2 parents 2ec0e64 + 88ffd0b commit 5241fe5
Show file tree
Hide file tree
Showing 24 changed files with 168 additions and 72 deletions.
13 changes: 9 additions & 4 deletions cmd/slackdump/internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/rusq/fsadapter"

"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/golang/base"
Expand Down Expand Up @@ -35,9 +36,7 @@ func init() {
CmdArchive.Wizard = archiveWizard
}

var (
errNoOutput = errors.New("output directory is required")
)
var errNoOutput = errors.New("output directory is required")

func RunArchive(ctx context.Context, cmd *base.Command, args []string) error {
start := time.Now()
Expand Down Expand Up @@ -80,7 +79,13 @@ func RunArchive(ctx context.Context, cmd *base.Command, args []string) error {
defer stop()
// we are using the same file subprocessor as the mattermost export.
subproc := fileproc.NewExport(fileproc.STmattermost, dl)
ctrl := control.New(cd, stream, control.WithLogger(lg), control.WithFiler(subproc))
ctrl := control.New(
cd,
stream,
control.WithLogger(lg),
control.WithFiler(subproc),
control.WithFlags(control.Flags{MemberOnly: cfg.MemberOnly}),
)
if err := ctrl.Run(ctx, list); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
Expand Down
9 changes: 9 additions & 0 deletions cmd/slackdump/internal/archive/archive_wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package archive
import (
"context"

"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/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/internal/structures"
)

Expand Down Expand Up @@ -33,6 +35,13 @@ func configuration() cfgui.Configuration {
Name: "Optional parameters",
Params: []cfgui.Parameter{
cfgui.ChannelIDs(&entryList, false),
{
Name: "Member Only",
Value: cfgui.Checkbox(cfg.MemberOnly),
Description: "Export only channels, which current user belongs to",
Inline: true,
Updater: updaters.NewBool(&cfg.MemberOnly),
},
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/archive/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var CmdSearch = &base.Command{
},
}

const flagMask = cfg.OmitUserCacheFlag | cfg.OmitCacheDir | cfg.OmitTimeframeFlag
const flagMask = cfg.OmitUserCacheFlag | cfg.OmitCacheDir | cfg.OmitTimeframeFlag | cfg.OmitMemberOnlyFlag

var cmdSearchMessages = &base.Command{
UsageLine: "slackdump search messages [flags] query terms",
Expand Down
13 changes: 9 additions & 4 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ var (
LegacyBrowser bool
ForceEnterprise bool

MemberOnly bool
DownloadFiles bool
NoChunkCache bool

// Oldest is the default timestamp of the oldest message to fetch, that is
// used by the dump and export commands.
Expand All @@ -54,6 +54,7 @@ var (
LocalCacheDir string
UserCacheRetention time.Duration
NoUserCache bool
NoChunkCache bool

Log *slog.Logger = slog.Default()
// LoadSecrets is a flag that indicates whether to load secrets from the
Expand Down Expand Up @@ -86,6 +87,7 @@ const (
OmitUserCacheFlag
OmitTimeframeFlag
OmitChunkCacheFlag
OmitMemberOnlyFlag

OmitAll = OmitConfigFlag |
OmitDownloadFlag |
Expand All @@ -95,11 +97,11 @@ const (
OmitAuthFlags |
OmitUserCacheFlag |
OmitTimeframeFlag |
OmitChunkCacheFlag
OmitChunkCacheFlag |
OmitMemberOnlyFlag
)

// SetBaseFlags sets base flags
// TODO: tests.
func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.StringVar(&TraceFile, "trace", os.Getenv("TRACE_FILE"), "trace `filename`")
fs.StringVar(&LogFile, "log", os.Getenv("LOG_FILE"), "log `file`, if not specified, messages are printed to STDERR")
Expand Down Expand Up @@ -136,7 +138,7 @@ func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
LocalCacheDir = CacheDir()
}
if mask&OmitWorkspaceFlag == 0 {
fs.StringVar(&Workspace, "workspace", osenv.Value("SLACK_WORKSPACE", ""), "Slack workspace to use") // TODO: load from configuration.
fs.StringVar(&Workspace, "workspace", osenv.Value("SLACK_WORKSPACE", ""), "Slack workspace to use")
}
if mask&OmitUserCacheFlag == 0 {
fs.BoolVar(&NoUserCache, "no-user-cache", false, "disable user cache (file cache)")
Expand All @@ -151,4 +153,7 @@ func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.Var(&Oldest, "time-from", "timestamp of the oldest message to fetch (UTC timezone)")
fs.Var(&Latest, "time-to", "timestamp of the newest message to fetch (UTC timezone)")
}
if mask&OmitMemberOnlyFlag == 0 {
fs.BoolVar(&MemberOnly, "member-only", false, "export only channels, which the current user belongs to (if no channels are specified)")
}
}
9 changes: 6 additions & 3 deletions cmd/slackdump/internal/diag/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import (
"io"
"os"

"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"
)

// cmdInfo is the information command.
var cmdInfo = &base.Command{
UsageLine: "slackdump tools info",
Short: "show information about Slackdump environment",
Run: runInfo,
UsageLine: "slackdump tools info",
Short: "show information about Slackdump environment",
Run: runInfo,
FlagMask: cfg.OmitAll,
PrintFlags: true,

Long: `# Info Command
Expand Down
9 changes: 3 additions & 6 deletions cmd/slackdump/internal/diag/info/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"os"
"path/filepath"
"strings"
"text/tabwriter"

"github.com/rusq/encio"

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/internal/cache"
Expand Down Expand Up @@ -43,6 +43,7 @@ func CollectAuth(ctx context.Context, w io.Writer) error {
if err != nil {
return fmt.Errorf("cache error: %w", err)
}
fmt.Fprintf(w, "TOKEN=%s\n", prov.Token)
if err := dumpCookiesMozilla(ctx, w, prov.Cookies()); err != nil {
return err
}
Expand All @@ -51,12 +52,8 @@ func CollectAuth(ctx context.Context, w io.Writer) error {

// dumpCookiesMozilla dumps cookies in Mozilla format.
func dumpCookiesMozilla(_ context.Context, w io.Writer, cookies []*http.Cookie) error {
tw := tabwriter.NewWriter(w, 0, 8, 0, '\t', 0)
defer tw.Flush()
fmt.Fprintf(tw, "# name@domain\tvalue_len\tflag\tpath\tsecure\texpiration\n")
for _, c := range cookies {
fmt.Fprintf(tw, "%s\t%9d\t%s\t%s\t%s\t%d\n",
c.Name+"@"+c.Domain, len(c.Value), "TRUE", c.Path, strings.ToUpper(fmt.Sprintf("%v", c.Secure)), c.Expires.Unix())
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\t%s\t%s\n", c.Domain, "TRUE", c.Path, strings.ToUpper(fmt.Sprintf("%v", c.Secure)), c.Expires.Unix(), c.Name, c.Value)
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/diag/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (p *uninstOptions) configuration() cfgui.Configuration {
Description: "Do not perform the uninstallation, just show what would be done",
Updater: updaters.NewBool(&p.dry),
},
// TODO: delete slackdump from user cache options.
// TODO: add an option to delete slackdump from user cache options.
},
},
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/slackdump/internal/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/rusq/fsadapter"

"github.com/rusq/slackdump/v3"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
Expand All @@ -37,6 +38,7 @@ var CmdDump = &base.Command{
Long: dumpMd,
RequireAuth: true,
PrintFlags: true,
FlagMask: cfg.OmitMemberOnlyFlag,
}

func init() {
Expand Down Expand Up @@ -175,7 +177,9 @@ func dump(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, p dump
var sdl fileproc.Downloader
if p.downloadFiles {
dl := downloader.New(sess.Client(), fsa, downloader.WithLogger(lg))
dl.Start(ctx)
if err := dl.Start(ctx); err != nil {
return err
}
defer dl.Stop()
sdl = dl
} else {
Expand Down Expand Up @@ -225,7 +229,6 @@ func dump(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, p dump
return sr.Err
}
if sr.IsLast {
//TODO: this is unbeautiful.
lg.InfoContext(ctx, "dumped", "sr", sr.String())
}
return nil
Expand Down
4 changes: 1 addition & 3 deletions cmd/slackdump/internal/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/rusq/fsadapter"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"

"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
Expand All @@ -30,7 +31,6 @@ var CmdExport = &base.Command{

type exportFlags struct {
ExportStorageType fileproc.StorageType
MemberOnly bool
ExportToken string
}

Expand All @@ -42,9 +42,7 @@ var (
)

func init() {
// TODO: move TimeValue somewhere more appropriate once v1 is sunset.
CmdExport.Flag.Var(&options.ExportStorageType, "type", "export file storage type")
CmdExport.Flag.BoolVar(&options.MemberOnly, "member-only", false, "export only channels, which current user belongs to")
CmdExport.Flag.StringVar(&options.ExportToken, "export-token", "", "file export token to append to each of the file URLs")

CmdExport.Run = runExport
Expand Down
4 changes: 1 addition & 3 deletions cmd/slackdump/internal/export/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"github.com/rusq/slackdump/v3/stream"
)

// TODO: check if the features is on par with Export v2.

// export runs the export v3.
func export(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, list *structures.EntityList, params exportFlags) error {
lg := cfg.Log
Expand Down Expand Up @@ -73,7 +71,7 @@ func export(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, list
)

flags := control.Flags{
MemberOnly: params.MemberOnly,
MemberOnly: cfg.MemberOnly,
}
ctr := control.New(
chunkdir,
Expand Down
6 changes: 4 additions & 2 deletions cmd/slackdump/internal/export/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"

"github.com/charmbracelet/huh"

"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/cfgui"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/ui/dumpui"
Expand Down Expand Up @@ -51,10 +53,10 @@ func (fl *exportFlags) configuration() cfgui.Configuration {
},
{
Name: "Member Only",
Value: cfgui.Checkbox(fl.MemberOnly),
Value: cfgui.Checkbox(cfg.MemberOnly),
Description: "Export only channels, which current user belongs to",
Inline: true,
Updater: updaters.NewBool(&fl.MemberOnly),
Updater: updaters.NewBool(&cfg.MemberOnly),
},
{
Name: "Export Token",
Expand Down
5 changes: 3 additions & 2 deletions cmd/slackdump/internal/list/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/rusq/slack"

"github.com/rusq/slackdump/v3"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
Expand All @@ -19,7 +20,7 @@ var CmdListChannels = &base.Command{
Run: runListChannels,
UsageLine: "slackdump list channels [flags] [filename]",
PrintFlags: true,
FlagMask: cfg.OmitDownloadFlag,
FlagMask: flagMask,
Short: "list workspace channels",
Long: fmt.Sprintf(`
# List Channels Command
Expand Down Expand Up @@ -75,7 +76,7 @@ func runListChannels(ctx context.Context, cmd *base.Command, args []string) erro
return err
}

var l = &channels{
l := &channels{
opts: chanFlags,
common: commonFlags,
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/slackdump/internal/list/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

"github.com/rusq/slack"

"github.com/rusq/slackdump/v3"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
Expand All @@ -16,6 +17,8 @@ import (
"github.com/rusq/slackdump/v3/types"
)

const flagMask = cfg.OmitDownloadFlag | cfg.OmitMemberOnlyFlag

// CmdList is the list command. The logic is in the subcommands.
var CmdList = &base.Command{
UsageLine: "slackdump list",
Expand Down
6 changes: 3 additions & 3 deletions cmd/slackdump/internal/list/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/rusq/slack"

"github.com/rusq/slackdump/v3"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
Expand All @@ -21,7 +22,7 @@ var CmdListUsers = &base.Command{
Run: runListUsers,
UsageLine: "slackdump list users [flags] [filename]",
PrintFlags: true,
FlagMask: cfg.OmitDownloadFlag,
FlagMask: flagMask,
Short: "list workspace users",
RequireAuth: true,
Long: fmt.Sprintf(`
Expand All @@ -46,7 +47,7 @@ func runListUsers(ctx context.Context, cmd *base.Command, args []string) error {
return err
}

var l = &users{
l := &users{
common: commonFlags,
}

Expand Down Expand Up @@ -81,7 +82,6 @@ func (u *users) Retrieve(ctx context.Context, sess *slackdump.Session, m *cache.
}

//go:generate mockgen -source=users.go -destination=mocks_test.go -package=list userGetter,userCacher

type userGetter interface {
GetUsers(ctx context.Context) (types.Users, error)
}
Expand Down
Loading

0 comments on commit 5241fe5

Please sign in to comment.