Skip to content

Commit

Permalink
fs logic refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 29, 2023
1 parent db5efca commit 910c3c1
Show file tree
Hide file tree
Showing 26 changed files with 167 additions and 157 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Example
log.Print(err)
return
}
defer sd.Close()
_ = sd
}
Expand Down Expand Up @@ -165,6 +166,7 @@ Good news is logrus_ can be plugged in straight away, as it implements the
return
}
}
defer sd.Close()
Glog and others
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 @@ -62,7 +62,7 @@ func readLimits(r io.Reader) (slackdump.Limits, error) {
return slackdump.Limits{}, err
}

if err := cfg.SlackOptions.Limits.Apply(limits); err != nil {
if err := cfg.SlackConfig.Limits.Apply(limits); err != nil {
if err := printErrors(os.Stderr, err); err != nil {
return slackdump.Limits{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/cfg/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func ucd(ucdFn func() (string, error)) string {
}

func CacheDir() string {
if SlackOptions.CacheDir == "" {
if SlackConfig.CacheDir == "" {
return ucd(os.UserCacheDir)
}
return SlackOptions.CacheDir
return SlackConfig.CacheDir
}
21 changes: 10 additions & 11 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ var (
Verbose bool

ConfigFile string
BaseLoc string // base location - directory or a zip file.
Workspace string

SlackToken string
SlackCookie string
Browser browser.Browser
SlackOptions = slackdump.DefOptions
SlackToken string
SlackCookie string
Browser browser.Browser
SlackConfig = slackdump.DefOptions
)

type FlagMask int
Expand Down Expand Up @@ -63,28 +62,28 @@ 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(&SlackOptions.DumpFiles, "files", true, "enables file attachments download (to disable,\nspecify: -files=false)")
fs.BoolVar(&SlackConfig.DumpFiles, "files", true, "enables file attachments download (to disable,\nspecify: -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`")
}
if mask&OmitBaseLoc == 0 {
base := fmt.Sprintf("slackdump_%s.zip", time.Now().Format("20060102_150405"))
fs.StringVar(&BaseLoc, "base", osenv.Value("BASE_LOC", base), "a `location` (a directory or a ZIP file) on the local disk to save\ndownloaded files to.")
fs.StringVar(&SlackConfig.BaseLocation, "base", osenv.Value("BASE_LOC", base), "a `location` (a directory or a ZIP file) on the local disk to save\ndownloaded files to.")
}
if mask&OmitCacheDir == 0 {
fs.StringVar(&SlackOptions.CacheDir, "cache-dir", osenv.Value("CACHE_DIR", CacheDir()), "cache `directory` location\n")
fs.StringVar(&SlackConfig.CacheDir, "cache-dir", osenv.Value("CACHE_DIR", CacheDir()), "cache `directory` location\n")
} else {
// If the OmitCacheDir is specified, then the CacheDir will end up being
// the default value, which is "". Therefore, we need to init the
// cache directory.
SlackOptions.CacheDir = CacheDir()
SlackConfig.CacheDir = CacheDir()
}
if mask&OmitWorkspaceFlag == 0 {
fs.StringVar(&Workspace, "workspace", osenv.Value("SLACK_WORKSPACE", ""), "Slack workspace to use") // TODO: load from configuration.
}
if mask&OmitUserCacheFlag == 0 {
fs.BoolVar(&SlackOptions.UserCache.Disabled, "no-user-cache", false, "disable user cache")
fs.DurationVar(&SlackOptions.UserCache.MaxAge, "user-cache-age", slackdump.DefOptions.UserCache.MaxAge, "maximum user cache age")
fs.BoolVar(&SlackConfig.UserCache.Disabled, "no-user-cache", false, "disable user cache")
fs.DurationVar(&SlackConfig.UserCache.MaxAge, "user-cache-age", slackdump.DefOptions.UserCache.MaxAge, "maximum user cache age")
}
}
5 changes: 3 additions & 2 deletions cmd/slackdump/internal/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,11 @@ func getUsersOnline(ctx context.Context, cacheDir, wsp string) ([]slack.User, er
if err != nil {
return nil, err
}
sess, err := slackdump.New(ctx, prov, cfg.SlackOptions)
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
if err != nil {
return nil, err
}
defer sess.Close()
return sess.GetUsers(ctx)
}

Expand All @@ -253,7 +254,7 @@ var errNoMatch = errors.New("no matching users")
// searchCache searches the cache directory for cached workspace users that have
// the same ids, and returns the user slice from that cache.
func searchCache(ctx context.Context, cacheDir string, ids []string) ([]slack.User, error) {
ctx, task := trace.NewTask(ctx, "searchCache")
_, task := trace.NewTask(ctx, "searchCache")
defer task.End()
m, err := cache.NewManager(cacheDir)
if err != nil {
Expand Down
17 changes: 4 additions & 13 deletions cmd/slackdump/internal/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,14 @@ func RunDump(ctx context.Context, cmd *base.Command, args []string) error {
return fmt.Errorf("file template error: %w", err)
}

// Initialize the filesystem.
fs, err := fsadapter.New(cfg.BaseLoc)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
defer fs.Close()

// Initialize the session.
cfg.SlackOptions.Filesystem = fs
cfg.SlackOptions.Logger = dlog.FromContext(ctx)
cfg.SlackConfig.Logger = dlog.FromContext(ctx)

sess, err := slackdump.New(ctx, prov, cfg.SlackOptions)
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
defer sess.Close()

// Dump conversations.
for _, link := range list.Include {
Expand All @@ -125,7 +116,7 @@ func RunDump(ctx context.Context, cmd *base.Command, args []string) error {
return err
}

if err := save(ctx, fs, namer.Filename(conv), conv); err != nil {
if err := save(ctx, sess.Filesystem(), namer.Filename(conv), conv); err != nil {
return err
}
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/slackdump/internal/emoji/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ func run(ctx context.Context, cmd *base.Command, args []string) error {
base.SetExitStatus(base.SAuthError)
return fmt.Errorf("auth error: %s", err)
}
cfg.SlackOptions.UserCache.Disabled = true // don't need users for emojis
sess, err := slackdump.New(ctx, prov, cfg.SlackOptions)
cfg.SlackConfig.UserCache.Disabled = true // don't need users for emojis
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return fmt.Errorf("application error: %s", err)
}
if err := emoji.Dl(ctx, sess, cfg.BaseLoc, ignoreErrors); err != nil {
defer sess.Close()
if err := emoji.Dl(ctx, sess, cfg.SlackConfig.BaseLocation, ignoreErrors); err != nil {
base.SetExitStatus(base.SApplicationError)
return fmt.Errorf("application error: %s", err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/slackdump/internal/emoji/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
)

func wizard(ctx context.Context, cmd *base.Command, args []string) error {
var baseDir string
var baseloc string
for {
var err error
baseDir, err = ui.FileSelector("Enter directory or ZIP file name: ", "Emojis will be saved to this directory or ZIP file")
baseloc, err = ui.FileSelector("Enter directory or ZIP file name: ", "Emojis will be saved to this directory or ZIP file")
if err != nil {
return err
}
if baseDir != "-" && baseDir != "" {
if baseloc != "-" && baseloc != "" {
break
}
fmt.Println("invalid filename")
}
cfg.BaseLoc = baseDir
cfg.SlackConfig.BaseLocation = baseloc

var err error
ignoreErrors, err = ui.Confirm("Ignore download errors?", true)
Expand Down
18 changes: 5 additions & 13 deletions cmd/slackdump/internal/export/export.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/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/export"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/app/config"
"github.com/rusq/slackdump/v2/internal/structures"
)
Expand All @@ -29,7 +28,7 @@ var CmdExport = &base.Command{
}

var (
options export.Options
options export.Config
)

func init() {
Expand All @@ -50,7 +49,7 @@ func init() {
}

func runExport(ctx context.Context, cmd *base.Command, args []string) error {
if cfg.BaseLoc == "" {
if cfg.SlackConfig.BaseLocation == "" {
return errors.New("use -base to set the base output location")
}
var err error
Expand All @@ -64,21 +63,14 @@ func runExport(ctx context.Context, cmd *base.Command, args []string) error {
return err
}

fs, err := fsadapter.New(cfg.BaseLoc)
if err != nil {
return err
}
defer fs.Close()

lg := dlog.FromContext(ctx)
options.Logger = lg
lg.Printf("initialised output location: %s", cfg.BaseLoc)

sess, err := slackdump.New(ctx, prov, cfg.SlackOptions)
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
if err != nil {
return err
}
defer sess.Close()

exp := export.New(sess, fs, options)
exp := export.New(sess, options)
return exp.Run(ctx)
}
11 changes: 4 additions & 7 deletions cmd/slackdump/internal/export/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"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/fsadapter"
"github.com/rusq/slackdump/v2/internal/ui"
"github.com/rusq/slackdump/v2/internal/ui/ask"
)
Expand Down Expand Up @@ -57,17 +56,15 @@ func wizExport(ctx context.Context, cmd *base.Command, args []string) error {
if err != nil {
return err
}
fs, err := fsadapter.New(baseLoc)
if err != nil {
return err
}
cfg.SlackConfig.BaseLocation = baseLoc

sess, err := slackdump.New(ctx, prov, cfg.SlackOptions)
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
if err != nil {
return err
}
defer sess.Close()

exp := export.New(sess, fs, options)
exp := export.New(sess, options)

// run export
return exp.Run(ctx)
Expand Down
14 changes: 4 additions & 10 deletions cmd/slackdump/internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ func list(ctx context.Context, listFn listFunc) error {
}

// initialize the session.
sess, err := slackdump.New(ctx, prov, cfg.SlackOptions)
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
defer sess.Close()

data, filename, err := listFn(ctx, sess)
if err != nil {
Expand All @@ -99,17 +100,10 @@ func list(ctx context.Context, listFn listFunc) error {
// saveData saves the given data to the given filename.
func saveData(ctx context.Context, sess *slackdump.Session, data any, filename string, typ format.Type) error {
// save to a filesystem.
fs, err := fsadapter.New(cfg.BaseLoc)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
defer fs.Close()

if err := writeData(ctx, fs, filename, data, typ, sess.Users); err != nil {
if err := writeData(ctx, sess.Filesystem(), filename, data, typ, sess.Users); err != nil {
return err
}
dlog.FromContext(ctx).Printf("Data saved to: %q\n", filepath.Join(cfg.BaseLoc, filename))
dlog.FromContext(ctx).Printf("Data saved to: %q\n", filepath.Join(cfg.SlackConfig.BaseLocation, filename))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/v1/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ func surveyExport(p *params) error {
if err != nil {
return err
}
p.appCfg.Options.DumpFiles, err = ui.Confirm("Export files?", true)
p.appCfg.SlackConfig.DumpFiles, err = ui.Confirm("Export files?", true)
if err != nil {
return err
}
if p.appCfg.Options.DumpFiles {
if p.appCfg.SlackConfig.DumpFiles {
p.appCfg.ExportType, err = ask.ExportType()
if err != nil {
return err
Expand Down
Loading

0 comments on commit 910c3c1

Please sign in to comment.