Skip to content

Commit

Permalink
use lightweight markdown renderer and stop using export package
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed May 4, 2023
1 parent 10ef6d7 commit 4cbeb3d
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 171 deletions.
6 changes: 4 additions & 2 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func SetBaseFlags(fs *flag.FlagSet, mask FlagMask) {
fs.DurationVar(&UserCacheRetention, "user-cache-retention", 60*time.Minute, "user cache retention duration. After this time, the cache is considered stale and will be refreshed.")
}
if mask&OmitTimeframeFlag == 0 {
fs.Var(&Oldest, "from", "timestamp of the oldest message to fetch (UTC timezone)")
fs.Var(&Latest, "to", "timestamp of the newest message to fetch (UTC timezone)")
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)")
fs.Var(&Oldest, "date-from", "timestamp of the oldest message to fetch (UTC timezone) (DEPRECATED)")
fs.Var(&Latest, "date-to", "timestamp of the newest message to fetch (UTC timezone) (DEPRECATED)")
}
}
27 changes: 17 additions & 10 deletions cmd/slackdump/internal/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/rusq/slackdump/v2/auth"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/export"
"github.com/rusq/slackdump/v2/internal/chunk/transform/fileproc"
"github.com/rusq/slackdump/v2/internal/structures"
"github.com/rusq/slackdump/v2/logger"
)
Expand All @@ -23,24 +23,34 @@ var CmdExport = &base.Command{
Wizard: nil,
UsageLine: "slackdump export",
Short: "exports the Slack Workspace or individual conversations",
FlagMask: cfg.OmitUserCacheFlag,
Long: ``, // TODO: add long description
CustomFlags: false,
PrintFlags: true,
RequireAuth: true,
}

type exportFlags struct {
ExportStorageType fileproc.StorageType
MemberOnly bool
ExportToken string
Oldest time.Time
Latest time.Time
}

var (
compat bool
options = export.Config{
Type: export.TMattermost,
Oldest: time.Time(cfg.Oldest),
Latest: time.Time(cfg.Latest),
options = exportFlags{
ExportStorageType: fileproc.STMattermost,
Oldest: time.Time(cfg.Oldest),
Latest: time.Time(cfg.Latest),
}
)

func init() {
// TODO: move TimeValue somewhere more appropriate once v1 is sunset.
CmdExport.Flag.Var(&options.Type, "type", "export type")
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.Flag.BoolVar(&compat, "compat", false, "use the v2 export code")

Expand All @@ -54,7 +64,7 @@ func runExport(ctx context.Context, cmd *base.Command, args []string) error {
return errors.New("use -base to set the base output location")
}
if !cfg.DumpFiles {
options.Type = export.TNoDownload
options.ExportStorageType = fileproc.STNone
}
list, err := structures.NewEntityList(args)
if err != nil {
Expand All @@ -80,9 +90,6 @@ func runExport(ctx context.Context, cmd *base.Command, args []string) error {
fsa.Close()
}()

options.List = list
options.Logger = logger.FromContext(ctx)

var expfn = exportV3
if compat {
expfn = exportV2
Expand Down
15 changes: 13 additions & 2 deletions cmd/slackdump/internal/export/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ package export

import (
"context"
"time"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/export"
"github.com/rusq/slackdump/v2/internal/structures"
"github.com/rusq/slackdump/v2/logger"
)

func exportV2(ctx context.Context, sess *slackdump.Session, fs fsadapter.FS, list *structures.EntityList, options export.Config) error {
exp := export.New(sess, fs, options)
func exportV2(ctx context.Context, sess *slackdump.Session, fs fsadapter.FS, list *structures.EntityList, flags exportFlags) error {
config := export.Config{
Oldest: time.Time(flags.Oldest),
Latest: time.Time(flags.Latest),
Logger: logger.FromContext(ctx),
List: list,
Type: export.ExportType(flags.ExportStorageType),
MemberOnly: flags.MemberOnly,
ExportToken: flags.ExportToken,
}
exp := export.New(sess, fs, config)
return exp.Run(ctx)
}
17 changes: 8 additions & 9 deletions cmd/slackdump/internal/export/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/downloader"
"github.com/rusq/slackdump/v2/export"
"github.com/rusq/slackdump/v2/internal/chunk"
"github.com/rusq/slackdump/v2/internal/chunk/control"
"github.com/rusq/slackdump/v2/internal/chunk/transform"
Expand All @@ -21,7 +20,7 @@ import (
"github.com/rusq/slackdump/v2/logger"
)

func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, list *structures.EntityList, options export.Config) error {
func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, list *structures.EntityList, params exportFlags) error {
lg := logger.FromContext(ctx)

tmpdir, err := os.MkdirTemp("", "slackdump-*")
Expand All @@ -37,14 +36,14 @@ func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, li
if !lg.IsDebug() {
defer chunkdir.RemoveAll()
}
tf, err := transform.NewExport(ctx, fsa, tmpdir, transform.WithBufferSize(1000), transform.WithMsgUpdateFunc(fileproc.ExportTokenUpdateFn(options.ExportToken)))
tf, err := transform.NewExport(ctx, fsa, tmpdir, transform.WithBufferSize(1000), transform.WithMsgUpdateFunc(fileproc.ExportTokenUpdateFn(params.ExportToken)))
if err != nil {
return fmt.Errorf("failed to create transformer: %w", err)
}
defer tf.Close()

// starting the downloader
sdl, stop := initDownloader(ctx, cfg.DumpFiles, sess.Client(), options.Type, fsa, lg)
sdl, stop := initDownloader(ctx, cfg.DumpFiles, sess.Client(), params.ExportStorageType, fsa, lg)
defer stop()

pb := newProgressBar(progressbar.NewOptions(
Expand All @@ -56,12 +55,12 @@ func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, li
pb.RenderBlank()

flags := control.Flags{
MemberOnly: options.MemberOnly,
MemberOnly: params.MemberOnly,
}
ctr := control.New(
chunkdir,
sess.Stream(),
control.WithFiler(fileproc.NewExport(options.Type, sdl)),
sess.Stream(slackdump.OptOldest(params.Oldest), slackdump.OptLatest(params.Latest)),
control.WithFiler(fileproc.NewExport(params.ExportStorageType, sdl)),
control.WithLogger(lg),
control.WithFlags(flags),
control.WithTransformer(tf),
Expand Down Expand Up @@ -90,8 +89,8 @@ func exportV3(ctx context.Context, sess *slackdump.Session, fsa fsadapter.FS, li
return nil
}

func initDownloader(ctx context.Context, gEnabled bool, cl downloader.Downloader, t export.ExportType, fsa fsadapter.FS, lg logger.Interface) (sdl fileproc.Downloader, stop func()) {
if t == export.TNoDownload || !gEnabled {
func initDownloader(ctx context.Context, gEnabled bool, cl downloader.Downloader, t fileproc.StorageType, fsa fsadapter.FS, lg logger.Interface) (sdl fileproc.Downloader, stop func()) {
if t == fileproc.STNone || !gEnabled {
return fileproc.NoopDownloader{}, func() {}
} else {
dl := downloader.New(cl, fsa, downloader.WithLogger(lg))
Expand Down
3 changes: 1 addition & 2 deletions cmd/slackdump/internal/export/v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/rusq/dlog"
"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/export"
"github.com/rusq/slackdump/v2/internal/chunk/chunktest"
"github.com/rusq/slackdump/v2/internal/structures"
"github.com/rusq/slackdump/v2/logger"
Expand Down Expand Up @@ -80,7 +79,7 @@ func Test_exportV3(t *testing.T) {
defer fsa.Close()

list := &structures.EntityList{}
if err := exportV3(ctx, sess, fsa, list, export.Config{List: list}); err != nil {
if err := exportV3(ctx, sess, fsa, list, exportFlags{}); err != nil {
t.Fatal(err)
}
})
Expand Down
110 changes: 52 additions & 58 deletions cmd/slackdump/internal/export/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,66 @@ package export
import (
"context"

"github.com/rusq/fsadapter"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/auth"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/export"
"github.com/rusq/slackdump/v2/internal/ui"
"github.com/rusq/slackdump/v2/internal/ui/ask"
"github.com/rusq/slackdump/v2/logger"
)

func wizExport(ctx context.Context, cmd *base.Command, args []string) error {
options.Logger = logger.FromContext(ctx)
prov, err := auth.FromContext(ctx)
if err != nil {
return err
}
// ask for the list
list, err := ask.ConversationList("Enter conversations to export (optional)?")
if err != nil {
return err
}
options.List = list
// options.Logger = logger.FromContext(ctx)
// prov, err := auth.FromContext(ctx)
// if err != nil {
// return err
// }
// // ask for the list
// list, err := ask.ConversationList("Enter conversations to export (optional)?")
// if err != nil {
// return err
// }
// options.List = list

// ask if user wants time range
options.Oldest, options.Latest, err = ask.MaybeTimeRange()
if err != nil {
return err
}
// // ask if user wants time range
// options.Oldest, options.Latest, err = ask.MaybeTimeRange()
// if err != nil {
// return err
// }

// ask for the type
exportType, err := ask.ExportType()
if err != nil {
return err
} else {
options.Type = exportType
}
// // ask for the type
// exportType, err := ask.ExportType()
// if err != nil {
// return err
// } else {
// options.Type = exportType
// }

if wantExportToken, err := ui.Confirm("Do you want to specify an export token for attachments?", false); err != nil {
return err
} else if wantExportToken {
// ask for the export token
exportToken, err := ui.String("Export token", "Enter the export token, that will be appended to each of the attachment URLs.")
if err != nil {
return err
}
options.ExportToken = exportToken
}
// if wantExportToken, err := ui.Confirm("Do you want to specify an export token for attachments?", false); err != nil {
// return err
// } else if wantExportToken {
// // ask for the export token
// exportToken, err := ui.String("Export token", "Enter the export token, that will be appended to each of the attachment URLs.")
// if err != nil {
// return err
// }
// options.ExportToken = exportToken
// }

// ask for the save location
baseLoc, err := ui.FileSelector("Output ZIP or Directory name", "Enter the name of the ZIP or directory to save the export to.")
if err != nil {
return err
}
fsa, err := fsadapter.New(baseLoc)
if err != nil {
return err
}
defer fsa.Close()
// // ask for the save location
// baseLoc, err := ui.FileSelector("Output ZIP or Directory name", "Enter the name of the ZIP or directory to save the export to.")
// if err != nil {
// return err
// }
// fsa, err := fsadapter.New(baseLoc)
// if err != nil {
// return err
// }
// defer fsa.Close()

sess, err := slackdump.New(ctx, prov, slackdump.WithFilesystem(fsa), slackdump.WithLogger(options.Logger))
if err != nil {
return err
}
// TODO v3
exp := export.New(sess, fsa, options)
// sess, err := slackdump.New(ctx, prov, slackdump.WithFilesystem(fsa), slackdump.WithLogger(options.Logger))
// if err != nil {
// return err
// }
// // TODO v3
// exp := export.New(sess, fsa, options)

// run export
return exp.Run(ctx)
// // run export
// return exp.Run(ctx)
return nil
}
16 changes: 10 additions & 6 deletions cmd/slackdump/internal/golang/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import (
"strings"
"sync"

markdown "github.com/MichaelMure/go-term-markdown"
"golang.org/x/term"

"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"golang.org/x/term"
"src.elv.sh/pkg/md"
)

var CmdName string
Expand Down Expand Up @@ -176,8 +175,13 @@ func Render(s string) string {
// we're not running in the terminal, output the markdown source.
return s
}
leftIndent := int(float64(width) * 0.075)
rightIndent := int(float64(width) * 0.02)
if width == 0 {
width = defWidth
}
return md.RenderString(s, &md.TTYCodec{Width: width})

// leftIndent := int(float64(width) * 0.075)
// rightIndent := int(float64(width) * 0.02)

return string(markdown.Render(s, width-rightIndent, leftIndent))
// return string(markdown.Render(s, width-rightIndent, leftIndent))
}
12 changes: 1 addition & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.19
require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403
github.com/MichaelMure/go-term-markdown v0.1.4
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2
github.com/fatih/color v1.15.0
github.com/go-playground/locales v0.14.1
Expand All @@ -29,26 +28,18 @@ require (
golang.org/x/text v0.9.0
golang.org/x/time v0.3.0
gopkg.in/yaml.v3 v3.0.1
src.elv.sh v0.19.2
)

require (
github.com/MichaelMure/go-term-text v0.3.1 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/creack/pty v1.1.18 // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/dlclark/regexp2 v1.8.1 // indirect
github.com/eliukblau/pixterm/pkg/ansimage v0.0.0-20191210081756-9fb6cf8c2f75 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kyokomi/emoji/v2 v2.2.12 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
Expand All @@ -58,7 +49,6 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rusq/secure v0.0.4 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
Expand Down
Loading

0 comments on commit 4cbeb3d

Please sign in to comment.