Skip to content

Commit

Permalink
implement convert command
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed May 7, 2023
1 parent 8e52ce1 commit 15d0120
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.Var(&Browser, "browser", "browser to use for EZ-Login 3000 (default: firefox)")
}
if mask&OmitDownloadFlag == 0 {
fs.BoolVar(&DumpFiles, "files", true, "enables file attachments download (to disable,\nspecify: -files=false)")
fs.BoolVar(&DumpFiles, "files", true, "enables file attachments (to disable, specify: -files=false)")
}
if mask&OmitConfigFlag == 0 {
fs.StringVar(&ConfigFile, "api-config", "", "configuration `file` with Slack API limits overrides.\nYou can generate one with default values with 'slackdump config new`")
Expand Down
66 changes: 66 additions & 0 deletions cmd/slackdump/internal/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package convert

import (
"context"
"errors"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/internal/chunk"
"github.com/rusq/slackdump/v2/internal/chunk/transform/fileproc"
"github.com/rusq/slackdump/v2/internal/convert"
"github.com/rusq/slackdump/v2/logger"
)

var CmdConvert = &base.Command{
Run: runConvert,
UsageLine: "slackdump convert [flags] <source>",
Short: "convert slackdump chunks to various formats",
Long: `
# Convert Command
Convert slackdump Chunks (output of "record") to various formats.
By default it converts a directory with chunks to an archive or directory
in Slack Export format.
`,
CustomFlags: false,
FlagMask: cfg.OmitAll & ^cfg.OmitDownloadFlag &^ cfg.OmitBaseLocFlag,
PrintFlags: true,
}

var storageType fileproc.StorageType

func init() {
CmdConvert.Flag.Var(&storageType, "storage", "storage type")
}

func runConvert(ctx context.Context, cmd *base.Command, args []string) error {
if len(args) < 1 {
base.SetExitStatus(base.SInvalidParameters)
return errors.New("source and destination are required")
}
src := args[0]
trg := cfg.BaseLocation

lg := logger.FromContext(ctx)
lg.Printf("converting (chunk) %q to (export) %q", src, trg)
cd, err := chunk.OpenDir(src)
if err != nil {
return err
}
fsa, err := fsadapter.New(trg)
if err != nil {
return err
}
defer fsa.Close()

cvt := convert.NewChunkToExport(cd, fsa, convert.WithIncludeFiles(cfg.DumpFiles))
if err := cvt.Convert(ctx); err != nil {
return err
}
lg.Printf("done")

return nil
}
2 changes: 2 additions & 0 deletions cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/rusq/slackdump/v2/cmd/slackdump/internal/apiconfig"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/convert"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/diag"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/dump"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/emoji"
Expand Down Expand Up @@ -50,6 +51,7 @@ func init() {
export.CmdExport,
dump.CmdDump,
record.CmdRecord,
convert.CmdConvert,
list.CmdList,
emoji.CmdEmoji,
workspace.CmdWorkspace,
Expand Down
6 changes: 3 additions & 3 deletions internal/convert/chunkexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type ChunkToExport struct {
type C2EOption func(*ChunkToExport)

// WithIncludeFiles sets the IncludeFiles option.
func WithIncludeFiles() C2EOption {
func WithIncludeFiles(b bool) C2EOption {
return func(c *ChunkToExport) {
c.includeFiles = true
c.includeFiles = b
}
}

Expand Down Expand Up @@ -165,7 +165,7 @@ func (c *ChunkToExport) Convert(ctx context.Context) error {
}
}()
for _, ch := range channels {
lg.Debugf("processing channel %q", ch.Name)
lg.Debugf("processing channel %q", ch.ID)
if err := conv.Convert(ctx, chunk.ToFileID(ch.ID, "", false)); err != nil {
return fmt.Errorf("converter: failed to process %q: %w", ch.ID, err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/convert/chunkexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestChunkToExport_Convert(t *testing.T) {
}
defer fsa.Close()

c := NewChunkToExport(cd, fsa, WithIncludeFiles())
c := NewChunkToExport(cd, fsa, WithIncludeFiles(true))

ctx := logger.NewContext(context.Background(), testLogger)
if err := c.Convert(ctx); err != nil {
Expand Down

0 comments on commit 15d0120

Please sign in to comment.