Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Export package and simplify app #96

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,12 @@ func run(ctx context.Context, p params) error {
// trace startup parameters for debugging
trace.Logf(ctx, "info", "params: input: %+v", p)

// initialise the application
application, err := app.New(p.appCfg, provider)
if err != nil {
trace.Logf(ctx, "error", "app.New: %s", err.Error())
return fmt.Errorf("application failed to initialise: %w", err)
}
defer application.Close()

// override default handler for SIGTERM and SIGQUIT signals.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

// run the application
if err := application.Run(ctx); err != nil {
if err := app.Run(ctx, p.appCfg, provider); err != nil {
trace.Logf(ctx, "error", "app.Run: %s", err.Error())
if isInvalidAuth(err) {
return fmt.Errorf("failed to authenticate: please double check that token/cookie values are correct (error: %w)", err)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
138 changes: 10 additions & 128 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,150 +2,32 @@ package app

import (
"context"
"html/template"
"runtime/trace"
"time"

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

const (
OutputTypeJSON = "json"
OutputTypeText = "text"
)

type App struct {
sd *slackdump.Session
tmpl *template.Template
fs fsadapter.FS

prov auth.Provider
cfg Config
}

// New creates a new slackdump app. It inherits the logging from slack options
// in the Config.
func New(cfg Config, provider auth.Provider) (*App, error) {
// Run starts the Slackdump.
func Run(ctx context.Context, cfg Config, prov auth.Provider) error {
if err := cfg.Validate(); err != nil {
return nil, err
}
tmpl, err := cfg.compileTemplates()
if err != nil {
return nil, err
}
fs, err := fsadapter.ForFilename(cfg.Output.Base)
if err != nil {
return nil, err
}
app := &App{cfg: cfg, prov: provider, tmpl: tmpl, fs: fs}
return app, nil
}

func (app *App) Run(ctx context.Context) error {
ctx, task := trace.NewTask(ctx, "app.Run")
defer task.End()

if err := app.initSlackdump(ctx); err != nil {
return err
}

start := time.Now()
var err error
switch {
case app.cfg.ListFlags.FlagsPresent():
err = app.runListEntities(ctx)
case app.cfg.ExportName != "":
app.l().Debug("export mode ON")
err = app.runExport(ctx)
default:
err = app.runDump(ctx)
}

if err != nil {
trace.Log(ctx, "error", err.Error())
return err
}

app.l().Printf("completed, time taken: %s", time.Since(start))
return nil
}

// Close closes all open handles.
func (app *App) Close() error {
return fsadapter.Close(app.fs)
}

// initSlackdump initialises the slack dumper app.
func (app *App) initSlackdump(ctx context.Context) error {
sd, err := slackdump.NewWithOptions(
ctx,
app.prov,
app.cfg.Options,
)
if err != nil {
return err
}
app.sd = sd
app.sd.SetFS(app.fs)
return nil
}

func (app *App) runListEntities(ctx context.Context) error {
ctx, task := trace.NewTask(ctx, "runListEntities")
ctx, task := trace.NewTask(ctx, "Run")
defer task.End()

if err := app.listEntities(ctx, app.cfg.Output, app.cfg.ListFlags); err != nil {
return err
}

return nil
}

func (app *App) runExport(ctx context.Context) error {
ctx, task := trace.NewTask(ctx, "runExport")
defer task.End()
start := time.Now()

if err := app.Export(ctx,
app.cfg.ExportName,
); err != nil {
return err
var err error
if cfg.ExportName != "" {
err = Export(ctx, cfg, prov)
} else {
err = Dump(ctx, cfg, prov)
}

return nil
}

func (app *App) runDump(ctx context.Context) error {
ctx, task := trace.NewTask(ctx, "runDump")
defer task.End()

n, err := app.dump(ctx, app.cfg.Input)
if err != nil {
return err
}

app.l().Printf("dumped %d item(s)", n)
cfg.Logger().Printf("completed, time taken: %s", time.Since(start))
return nil
}

func (app *App) l() logger.Interface {
// inherit the logger from the slackdump options
if app.cfg.Options.Logger == nil {
app.cfg.Options.Logger = logger.Default
}
return app.cfg.Options.Logger
}

// td outputs the message to trace and logs a debug message.
func (app *App) td(ctx context.Context, category string, fmt string, a ...any) {
app.l().Debugf(fmt, a...)
trace.Logf(ctx, category, fmt, a...)
}

// td outputs the message to trace and logs a debug message.
func (app *App) tl(ctx context.Context, category string, fmt string, a ...any) {
app.l().Printf(fmt, a...)
trace.Logf(ctx, category, fmt, a...)
}
17 changes: 15 additions & 2 deletions internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ import (

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

const (
OutputTypeJSON = "json"
OutputTypeText = "text"
)

const (
filenameTmplName = "fnt"
)

var errSkip = errors.New("skip")

type Config struct {
ListFlags ListFlags

Expand Down Expand Up @@ -45,8 +53,6 @@ type Input struct {

var (
ErrInvalidInput = errors.New("no valid input")

errSkip = errors.New("skip")
)

func (in *Input) IsValid() bool {
Expand Down Expand Up @@ -173,3 +179,10 @@ func (in Input) producer(fn func(string) error) error {

return in.listProducer(fn)
}

func (cfg Config) Logger() logger.Interface {
if cfg.Options.Logger == nil {
return logger.Default
}
return cfg.Options.Logger
}
Loading