Skip to content

Commit

Permalink
refactor(core): move storage package to core module (#813)
Browse files Browse the repository at this point in the history
* refactor(core): move storage package to core module

* rename

* lint
  • Loading branch information
iyear authored Nov 22, 2024
1 parent 0d57473 commit eee2d1c
Show file tree
Hide file tree
Showing 34 changed files with 296 additions and 290 deletions.
5 changes: 2 additions & 3 deletions app/chat/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import (
"github.com/jedib0t/go-pretty/v6/progress"
"go.uber.org/multierr"

"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/core/tmedia"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/texpr"
)

Expand Down Expand Up @@ -54,7 +53,7 @@ type Message struct {
// ENUM(time, id, last)
type ExportType int

func Export(ctx context.Context, c *telegram.Client, kvd kv.KV, opts ExportOptions) (rerr error) {
func Export(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts ExportOptions) (rerr error) {
// only output available fields
if opts.Filter == "-" {
fg := texpr.NewFieldsGetter(nil)
Expand Down
5 changes: 2 additions & 3 deletions app/chat/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import (
"go.uber.org/zap"

"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/texpr"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ type ListOptions struct {
Filter string
}

func List(ctx context.Context, c *telegram.Client, kvd kv.KV, opts ListOptions) error {
func List(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts ListOptions) error {
log := logctx.From(ctx)

// align output
Expand Down
5 changes: 2 additions & 3 deletions app/chat/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import (
"github.com/jedib0t/go-pretty/v6/progress"
"go.uber.org/multierr"

"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
)

type UsersOptions struct {
Expand All @@ -38,7 +37,7 @@ type User struct {
LastName string `json:"last_name"`
}

func Users(ctx context.Context, c *telegram.Client, kvd kv.KV, opts UsersOptions) (rerr error) {
func Users(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts UsersOptions) (rerr error) {
manager := peers.Options{Storage: storage.NewPeers(kvd)}.Build(c.API())
if opts.Chat == "" {
return fmt.Errorf("missing domain id")
Expand Down
19 changes: 9 additions & 10 deletions app/dl/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import (
"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/core/downloader"
"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/core/tclient"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/key"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tmessage"
"github.com/iyear/tdl/pkg/utils"
)
Expand Down Expand Up @@ -53,7 +52,7 @@ type parser struct {
Parser tmessage.ParseSource
}

func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr error) {
func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Options) (rerr error) {
pool := dcpool.NewPool(c,
int64(viper.GetInt(consts.FlagPoolSize)),
tclient.NewDefaultMiddlewares(ctx, viper.GetDuration(consts.FlagReconnectTimeout))...)
Expand Down Expand Up @@ -94,7 +93,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
if rerr != nil { // download is interrupted
multierr.AppendInto(&rerr, saveProgress(ctx, kvd, it))
} else { // if finished, we should clear resume key
multierr.AppendInto(&rerr, kvd.Delete(key.Resume(it.Fingerprint())))
multierr.AppendInto(&rerr, kvd.Delete(ctx, key.Resume(it.Fingerprint())))
}
}()

Expand Down Expand Up @@ -137,12 +136,12 @@ func collectDialogs(parsers []parser) ([][]*tmessage.Dialog, error) {
return dialogs, nil
}

func resume(ctx context.Context, kvd kv.KV, iter *iter, ask bool) error {
func resume(ctx context.Context, kvd storage.Storage, iter *iter, ask bool) error {
logctx.From(ctx).Debug("Check resume key",
zap.String("fingerprint", iter.Fingerprint()))

b, err := kvd.Get(key.Resume(iter.Fingerprint()))
if err != nil && !errors.Is(err, kv.ErrNotFound) {
b, err := kvd.Get(ctx, key.Resume(iter.Fingerprint()))
if err != nil && !errors.Is(err, storage.ErrNotFound) {
return err
}
if len(b) == 0 { // no progress
Expand Down Expand Up @@ -177,14 +176,14 @@ func resume(ctx context.Context, kvd kv.KV, iter *iter, ask bool) error {

if !confirm {
// clear resume key
return kvd.Delete(key.Resume(iter.Fingerprint()))
return kvd.Delete(ctx, key.Resume(iter.Fingerprint()))
}

iter.SetFinished(finished)
return nil
}

func saveProgress(ctx context.Context, kvd kv.KV, it *iter) error {
func saveProgress(ctx context.Context, kvd storage.Storage, it *iter) error {
finished := it.Finished()
logctx.From(ctx).Debug("Save progress",
zap.Int("finished", len(finished)))
Expand All @@ -193,5 +192,5 @@ func saveProgress(ctx context.Context, kvd kv.KV, it *iter) error {
if err != nil {
return err
}
return kvd.Set(key.Resume(it.Fingerprint()), b)
return kvd.Set(ctx, key.Resume(it.Fingerprint()), b)
}
5 changes: 2 additions & 3 deletions app/dl/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ import (

"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/core/tmedia"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tmessage"
)

Expand All @@ -39,7 +38,7 @@ type media struct {
var tmpl string

func serve(ctx context.Context,
kvd kv.KV,
kvd storage.Storage,
pool dcpool.Pool,
dialogs [][]*tmessage.Dialog,
port int,
Expand Down
5 changes: 2 additions & 3 deletions app/forward/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"github.com/iyear/tdl/app/internal/tctx"
"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/core/forwarder"
"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/core/tclient"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/texpr"
"github.com/iyear/tdl/pkg/tmessage"
)
Expand All @@ -40,7 +39,7 @@ type Options struct {
Desc bool
}

func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr error) {
func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Options) (rerr error) {
if opts.To == "-" || opts.Edit == "-" {
fg := texpr.NewFieldsGetter(nil)

Expand Down
8 changes: 4 additions & 4 deletions app/internal/tctx/tctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"context"

"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/core/storage"
)

type kvKey struct{}

func KV(ctx context.Context) kv.KV {
return ctx.Value(kvKey{}).(kv.KV)
func KV(ctx context.Context) storage.Storage {
return ctx.Value(kvKey{}).(storage.Storage)
}

func WithKV(ctx context.Context, kv kv.KV) context.Context {
func WithKV(ctx context.Context, kv storage.Storage) context.Context {
return context.WithValue(ctx, kvKey{}, kv)
}

Expand Down
2 changes: 1 addition & 1 deletion app/login/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Code(ctx context.Context) error {
return errors.Wrap(err, "open kv")
}

if err = kvd.Set(key.App(), []byte(tclient.AppDesktop)); err != nil {
if err = kvd.Set(ctx, key.App(), []byte(tclient.AppDesktop)); err != nil {
return errors.Wrap(err, "set app")
}

Expand Down
4 changes: 2 additions & 2 deletions app/login/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
tdtdesktop "github.com/gotd/td/session/tdesktop"
"github.com/spf13/viper"

"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/core/util/fsutil"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/key"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tclient"
"github.com/iyear/tdl/pkg/tdesktop"
"github.com/iyear/tdl/pkg/tpath"
Expand Down Expand Up @@ -74,7 +74,7 @@ func Desktop(ctx context.Context, opts Options) error {
return err
}

if err = kvd.Set(key.App(), []byte(tclient.AppDesktop)); err != nil {
if err = kvd.Set(ctx, key.App(), []byte(tclient.AppDesktop)); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion app/login/qr.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func QR(ctx context.Context) error {
return errors.Wrap(err, "open kv")
}

if err = kvd.Set(key.App(), []byte(tclient.AppDesktop)); err != nil {
if err = kvd.Set(ctx, key.App(), []byte(tclient.AppDesktop)); err != nil {
return errors.Wrap(err, "set app")
}

Expand Down
5 changes: 2 additions & 3 deletions app/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import (
"go.uber.org/multierr"

"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/core/tclient"
"github.com/iyear/tdl/core/uploader"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/utils"
)

Expand All @@ -30,7 +29,7 @@ type Options struct {
Photo bool
}

func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr error) {
func Run(ctx context.Context, c *telegram.Client, kvd storage.Storage, opts Options) (rerr error) {
files, err := walk(opts.Paths, opts.Excludes)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions cmd/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/iyear/tdl/app/chat"
"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/core/storage"
)

var limiter = ratelimit.New(rate.Every(500*time.Millisecond), 2)
Expand All @@ -38,7 +38,7 @@ func NewChatList() *cobra.Command {
Use: "ls",
Short: "List your chats",
RunE: func(cmd *cobra.Command, args []string) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd storage.Storage) error {
return chat.List(logctx.Named(ctx, "ls"), c, kvd, opts)
}, limiter)
},
Expand Down Expand Up @@ -83,7 +83,7 @@ func NewChatExport() *cobra.Command {
return fmt.Errorf("unknown export type: %s", opts.Type)
}

return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd storage.Storage) error {
return chat.Export(logctx.Named(ctx, "export"), c, kvd, opts)
}, limiter)
},
Expand Down Expand Up @@ -138,7 +138,7 @@ func NewChatUsers() *cobra.Command {
Use: "users",
Short: "export users from (protected) channels",
RunE: func(cmd *cobra.Command, args []string) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd storage.Storage) error {
return chat.Users(logctx.Named(ctx, "users"), c, kvd, opts)
}, limiter)
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/iyear/tdl/app/dl"
"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/core/storage"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/kv"
)

func NewDownload() *cobra.Command {
Expand All @@ -29,7 +29,7 @@ func NewDownload() *cobra.Command {

opts.Template = viper.GetString(consts.FlagDlTemplate)

return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd storage.Storage) error {
return dl.Run(logctx.Named(ctx, "dl"), c, kvd, opts)
})
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/spf13/viper"

"github.com/iyear/tdl/app/extension"
"github.com/iyear/tdl/core/storage"
extbase "github.com/iyear/tdl/extension"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/extensions"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tclient"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/iyear/tdl/app/forward"
"github.com/iyear/tdl/core/forwarder"
"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/core/storage"
)

func NewForward() *cobra.Command {
Expand All @@ -22,7 +22,7 @@ func NewForward() *cobra.Command {
Short: "Forward messages with automatic fallback and message routing",
GroupID: groupTools.ID,
RunE: func(cmd *cobra.Command, args []string) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd storage.Storage) error {
return forward.Run(logctx.Named(ctx, "forward"), c, kvd, opts)
})
},
Expand Down
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"golang.org/x/net/proxy"

"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/core/storage"
tclientcore "github.com/iyear/tdl/core/tclient"
"github.com/iyear/tdl/core/util/fsutil"
"github.com/iyear/tdl/core/util/logutil"
Expand Down Expand Up @@ -91,12 +92,12 @@ func New() *cobra.Command {
}
}

storage, err := kv.NewWithMap(viper.GetStringMapString(consts.FlagStorage))
stg, err := kv.NewWithMap(viper.GetStringMapString(consts.FlagStorage))
if err != nil {
return errors.Wrap(err, "create kv storage")
}

cmd.SetContext(kv.With(cmd.Context(), storage))
cmd.SetContext(kv.With(cmd.Context(), stg))

// extension manager client proxy
var dialer proxy.ContextDialer = proxy.Direct
Expand Down Expand Up @@ -228,7 +229,7 @@ func tOptions(ctx context.Context) (tclient.Options, error) {
return o, nil
}

func tRun(ctx context.Context, f func(ctx context.Context, c *telegram.Client, kvd kv.KV) error, middlewares ...telegram.Middleware) error {
func tRun(ctx context.Context, f func(ctx context.Context, c *telegram.Client, kvd storage.Storage) error, middlewares ...telegram.Middleware) error {
o, err := tOptions(ctx)
if err != nil {
return errors.Wrap(err, "build telegram options")
Expand Down
4 changes: 2 additions & 2 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/iyear/tdl/app/up"
"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/core/storage"
)

func NewUpload() *cobra.Command {
Expand All @@ -20,7 +20,7 @@ func NewUpload() *cobra.Command {
Short: "Upload anything to Telegram",
GroupID: groupTools.ID,
RunE: func(cmd *cobra.Command, args []string) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd storage.Storage) error {
return up.Run(logctx.Named(ctx, "up"), c, kvd, opts)
})
},
Expand Down
Loading

0 comments on commit eee2d1c

Please sign in to comment.