Skip to content

Commit

Permalink
remove Logger from slackdump.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Feb 3, 2023
1 parent 14ff51b commit b97c90c
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 97 deletions.
4 changes: 2 additions & 2 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ func (s *Session) getChannels(ctx context.Context, chanTypes []string, cb func(t
}
total += len(chans)

s.l().Printf("channels request #%5d, fetched: %4d, total: %8d (speed: %6.2f/sec, avg: %6.2f/sec)\n",
s.log.Printf("channels request #%5d, fetched: %4d, total: %8d (speed: %6.2f/sec, avg: %6.2f/sec)\n",
i, len(chans), total,
float64(len(chans))/float64(time.Since(reqStart).Seconds()),
float64(total)/float64(time.Since(fetchStart).Seconds()),
)

if nextcur == "" {
s.l().Printf("channels fetch complete, total: %d channels", total)
s.log.Printf("channels fetch complete, total: %d channels", total)
break
}

Expand Down
2 changes: 2 additions & 0 deletions channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/slack-go/slack"
"github.com/stretchr/testify/assert"

"github.com/rusq/slackdump/v2/logger"
"github.com/rusq/slackdump/v2/types"
)

Expand Down Expand Up @@ -80,6 +81,7 @@ func TestSession_getChannels(t *testing.T) {
client: mc,
Users: tt.fields.Users,
cfg: tt.fields.options,
log: logger.Silent,
}

if tt.expectFn != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/slackdump/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/auth/browser"
"github.com/rusq/slackdump/v2/logger"
)

var (
Expand All @@ -25,6 +26,8 @@ var (
SlackCookie string
Browser browser.Browser
SlackConfig = slackdump.DefOptions

Log logger.Interface
)

type FlagMask int
Expand Down
4 changes: 1 addition & 3 deletions cmd/slackdump/internal/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ func RunDump(ctx context.Context, cmd *base.Command, args []string) error {
return fmt.Errorf("file template error: %w", err)
}

cfg.SlackConfig.Logger = dlog.FromContext(ctx)

sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig, slackdump.WithLogger(dlog.FromContext(ctx)))
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/v1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func run(ctx context.Context, p params) error {
ctx = dlog.NewContext(ctx, lg)

// - setting the logger for the application.
p.appCfg.SlackConfig.Logger = lg
p.appCfg.Log = lg

// - trace init
if traceStopFn, err := initTrace(lg, p.traceFile); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/slackdump/internal/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func printAll(m manager, current string, wsps []string) {
fmt.Fprintln(tw,
"C\tname\tfilename\tmodified\tteam\tuser\terror\n"+
"-\t-------\t------------\t-------------------\t---------\t--------\t-----")
cfg.SlackConfig.Logger = logger.Silent
cfg.SlackConfig.UserCache.Disabled = true
for _, name := range wsps {
curr := ""
Expand All @@ -119,7 +118,7 @@ func userInfo(ctx context.Context, m manager, name string) (*slack.AuthTestRespo
if err != nil {
return nil, err
}
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig, slackdump.WithLogger(logger.Silent))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func invoke(cmd *base.Command, args []string) error {
} else {
lg.SetPrefix(cmd.Name() + ": ")
ctx = dlog.NewContext(ctx, lg)
cfg.SlackConfig.Logger = lg
cfg.Log = lg
}

if cmd.RequireAuth {
Expand Down
10 changes: 4 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
translations "github.com/go-playground/validator/v10/translations/en"

"github.com/rusq/slackdump/v2/logger"
)

// Config is the option set for the Session.
Expand All @@ -24,7 +22,7 @@ type Config struct {
UserCache CacheConfig

BaseLocation string // base location for the dump files
Logger logger.Interface
Logfile string
}

// CacheConfig represents the options for the cache.
Expand Down Expand Up @@ -92,9 +90,9 @@ var DefOptions = Config{
},
DumpFiles: false,
UserCache: CacheConfig{Filename: "users.cache", MaxAge: 4 * time.Hour},
CacheDir: "", // default cache dir
Logger: logger.Default, // default logger is the... default logger
BaseLocation: ".", // default location is the current directory
CacheDir: "", // default cache dir
BaseLocation: ".", // default location is the current directory
Logfile: "", // empty, means STDERR
}

var (
Expand Down
8 changes: 4 additions & 4 deletions export/expfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ func Test_newFileExporter(t *testing.T) {
args args
wantT string
}{
{"unknown is nodownload", args{t: ExportType(255), l: logger.Default, token: "abcd"}, "dl.Nothing"},
{"no", args{t: TNoDownload, l: logger.Default, token: "abcd"}, "dl.Nothing"},
{"standard", args{t: TStandard, fs: fsadapter.NewDirectory("."), cl: &slack.Client{}, l: logger.Default, token: "abcd"}, "*dl.Std"},
{"mattermost", args{t: TMattermost, fs: fsadapter.NewDirectory("."), cl: &slack.Client{}, l: logger.Default, token: "abcd"}, "*dl.Mattermost"},
{"unknown is nodownload", args{t: ExportType(255), l: logger.Silent, token: "abcd"}, "dl.Nothing"},
{"no", args{t: TNoDownload, l: logger.Silent, token: "abcd"}, "dl.Nothing"},
{"standard", args{t: TStandard, fs: fsadapter.NewDirectory("."), cl: &slack.Client{}, l: logger.Silent, token: "abcd"}, "*dl.Std"},
{"mattermost", args{t: TMattermost, fs: fsadapter.NewDirectory("."), cl: &slack.Client{}, l: logger.Silent, token: "abcd"}, "*dl.Mattermost"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions internal/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Params struct {
Emoji EmojiParams

SlackConfig slackdump.Config

Log logger.Interface
}

type EmojiParams struct {
Expand Down Expand Up @@ -160,8 +162,8 @@ func (in Input) Producer(fn func(string) error) error {
}

func (p *Params) Logger() logger.Interface {
if p.SlackConfig.Logger == nil {
if p.Log == nil {
return logger.Default
}
return p.SlackConfig.Logger
return p.Log
}
4 changes: 2 additions & 2 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ func (s *Session) dumpChannel(ctx context.Context, channelID string, oldest, lat

messages = append(messages, chunk...)

s.l().Printf("messages request #%5d, fetched: %4d (%s), total: %8d (speed: %6.2f/sec, avg: %6.2f/sec)\n",
s.log.Printf("messages request #%5d, fetched: %4d (%s), total: %8d (speed: %6.2f/sec, avg: %6.2f/sec)\n",
i, len(resp.Messages), results, len(messages),
float64(len(resp.Messages))/float64(time.Since(reqStart).Seconds()),
float64(len(messages))/float64(time.Since(fetchStart).Seconds()),
)

if !resp.HasMore {
s.l().Printf("messages fetch complete, total: %d", len(messages))
s.log.Printf("messages fetch complete, total: %d", len(messages))
break
}

Expand Down
3 changes: 3 additions & 0 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/rusq/slackdump/v2/internal/fixtures"
"github.com/rusq/slackdump/v2/internal/network"
"github.com/rusq/slackdump/v2/logger"
"github.com/rusq/slackdump/v2/types"
)

Expand Down Expand Up @@ -220,6 +221,7 @@ func TestSession_DumpMessages(t *testing.T) {
client: mc,
Users: tt.fields.Users,
cfg: tt.fields.options,
log: logger.Silent,
}
got, err := sd.DumpAll(tt.args.ctx, tt.args.channelID)
if (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -302,6 +304,7 @@ func TestSession_DumpAll(t *testing.T) {
client: mc,
Users: tt.fields.Users,
cfg: tt.fields.options,
log: logger.Silent,
}
got, err := sd.DumpAll(tt.args.ctx, tt.args.slackURL)
if (err != nil) != tt.wantErr {
Expand Down
2 changes: 1 addition & 1 deletion processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s *Session) newFileProcessFn(ctx context.Context, dir string, l *rate.Limi
downloader.Limiter(l),
downloader.Retries(s.cfg.Limits.DownloadRetries),
downloader.Workers(s.cfg.Limits.Workers),
downloader.Logger(s.l()),
downloader.Logger(s.log),
)
var filesC = make(chan *slack.File, filesCbufSz)

Expand Down
89 changes: 66 additions & 23 deletions slackdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"runtime/trace"

"github.com/go-playground/validator/v10"
"github.com/slack-go/slack"
"golang.org/x/time/rate"

"github.com/rusq/dlog"
"github.com/rusq/slackdump/v2/auth"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/network"
Expand All @@ -24,7 +26,8 @@ import (
//go:generate sh -c "mockgen -source slackdump.go -destination clienter_mock_test.go -package slackdump -mock_names clienter=mockClienter,Reporter=mockReporter"
//go:generate sed -i ~ -e "s/NewmockClienter/newmockClienter/g" -e "s/NewmockReporter/newmockReporter/g" clienter_mock_test.go

// Session stores basic session parameters.
// Session stores basic session parameters. Zero value is not usable, must be
// initialised with New.
type Session struct {
client clienter // Slack client

Expand All @@ -33,8 +36,10 @@ type Session struct {
// Users contains the list of users and populated on NewSession
Users types.Users `json:"users"`

fs fsadapter.FSCloser // filesystem adapter
ownFS bool // whether the filesystem adapter was created by the session
fs fsadapter.FS // filesystem adapter
log logger.Interface // logger

atClose []func() error // functions to call on exit

cfg Config
}
Expand Down Expand Up @@ -77,6 +82,18 @@ func WithFilesystem(fs fsadapter.FSCloser) Option {
}
}

// WithLogger sets the logger to use for the session. If this option is not
// given, the default logger is initialised with the filename specified in
// Config.Logfile. If the Config.Logfile is empty, the default logger writes
// to STDERR.
func WithLogger(l logger.Interface) Option {
return func(s *Session) {
if l != nil {
s.log = l
}
}
}

// New creates new Slackdump session with provided options, and populates the
// internal cache of users and channels for lookups. If it fails to
// authenticate, AuthError is returned.
Expand Down Expand Up @@ -106,22 +123,23 @@ func New(ctx context.Context, prov auth.Provider, cfg Config, opts ...Option) (*
client: cl,
cfg: cfg,
wspInfo: authTestResp,
log: logger.Default,
}
for _, opt := range opts {
opt(sd)
}
if sd.fs == nil {
// if no filesystem adapter is provided through Options, initialise
// the default one.
var err error
sd.fs, err = fsadapter.New(cfg.BaseLocation)
if err != nil {
if err := sd.openFS(cfg.BaseLocation); err != nil {
return nil, fmt.Errorf("failed to initialise filesystem adapter: %s", err)
}
sd.ownFS = true
}
if sd.log == nil {
if err := sd.openLogger(cfg.Logfile); err != nil {
return nil, fmt.Errorf("failed to initialise logger: %s", err)
}
}

sd.propagateLogger(sd.l())
sd.propagateLogger()

if err := os.MkdirAll(cfg.CacheDir, 0700); err != nil {
return nil, fmt.Errorf("failed to create the cache directory: %s", err)
Expand All @@ -144,18 +162,51 @@ func (s *Session) Client() *slack.Client {
return s.client.(*slack.Client)
}

func (s *Session) openFS(loc string) error {
// if no filesystem adapter is provided through Options, initialise
// the default one.
fs, err := fsadapter.New(loc)
if err != nil {
return err
}
s.fs = fs
s.atClose = append(s.atClose, func() error {
return fs.Close()
})
return nil
}

// Filesystem returns the filesystem adapter used by the session.
func (s *Session) Filesystem() fsadapter.FS {
return s.fs
}

func (s *Session) openLogger(filename string) error {
if filename == "" {
s.log = logger.Default
return nil
}
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return err
}
s.atClose = append(s.atClose, func() error {
return f.Close()
})
s.log = dlog.New(f, "", log.LstdFlags, false)
return nil
}

// Close closes the handles if they were created by the session.
// It must be called when the session is no longer needed.
func (s *Session) Close() error {
if s.ownFS {
return s.fs.Close()
var last error
for _, fn := range s.atClose {
if err := fn(); err != nil {
last = err
}
}
return nil
return last
}

// Me returns the current authenticated user in a rather dirty manner.
Expand Down Expand Up @@ -190,17 +241,9 @@ func withRetry(ctx context.Context, l *rate.Limiter, maxAttempts int, fn func()
return network.WithRetry(ctx, l, maxAttempts, fn)
}

// l returns the current logger.
func (s *Session) l() logger.Interface {
if s.cfg.Logger == nil {
return logger.Default
}
return s.cfg.Logger
}

// propagateLogger propagates the slackdump logger to some dumb packages.
func (s *Session) propagateLogger(l logger.Interface) {
network.Logger = l
func (s *Session) propagateLogger() {
network.Logger = s.log
}

// Info returns a workspace information. Slackdump retrieves workspace
Expand Down
Loading

0 comments on commit b97c90c

Please sign in to comment.