Skip to content

Commit

Permalink
refactor(prj): separate packages to core module
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Jun 6, 2024
1 parent f0b1903 commit 372f922
Show file tree
Hide file tree
Showing 62 changed files with 179 additions and 176 deletions.
4 changes: 2 additions & 2 deletions app/chat/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"github.com/jedib0t/go-pretty/v6/progress"
"go.uber.org/multierr"

"github.com/iyear/tdl/core/tutil"
"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"
"github.com/iyear/tdl/pkg/tmedia"
)

//go:generate go-enum --names --values --flag --nocase
Expand Down
6 changes: 3 additions & 3 deletions app/chat/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"github.com/mattn/go-runewidth"
"go.uber.org/zap"

"github.com/iyear/tdl/core/tutil"
"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/texpr"
)
Expand Down Expand Up @@ -56,7 +56,7 @@ type ListOptions struct {
}

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

// align output
runewidth.EastAsianWidth = false
Expand Down
2 changes: 1 addition & 1 deletion app/chat/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/jedib0t/go-pretty/v6/progress"
"go.uber.org/multierr"

"github.com/iyear/tdl/core/tutil"
"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"
Expand Down
16 changes: 8 additions & 8 deletions app/dl/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/core/downloader"
"github.com/iyear/tdl/core/logctx"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/downloader"
"github.com/iyear/tdl/pkg/key"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
"github.com/iyear/tdl/pkg/tclient"
Expand Down Expand Up @@ -66,7 +66,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
if err != nil {
return err
}
logger.From(ctx).Debug("Collect dialogs",
logctx.From(ctx).Debug("Collect dialogs",
zap.Any("dialogs", dialogs))

if opts.Serve {
Expand Down Expand Up @@ -110,7 +110,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
}
limit := viper.GetInt(consts.FlagLimit)

logger.From(ctx).Info("Start download",
logctx.From(ctx).Info("Start download",
zap.String("dir", opts.Dir),
zap.Bool("rewrite_ext", opts.RewriteExt),
zap.Bool("skip_same", opts.SkipSame),
Expand Down Expand Up @@ -139,7 +139,7 @@ func collectDialogs(parsers []parser) ([][]*tmessage.Dialog, error) {
}

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

b, err := kvd.Get(key.Resume(iter.Fingerprint()))
Expand Down Expand Up @@ -173,7 +173,7 @@ func resume(ctx context.Context, kvd kv.KV, iter *iter, ask bool) error {
confirm = true
}

logger.From(ctx).Debug("Resume download",
logctx.From(ctx).Debug("Resume download",
zap.Int("finished", len(finished)))

if !confirm {
Expand All @@ -187,7 +187,7 @@ func resume(ctx context.Context, kvd kv.KV, iter *iter, ask bool) error {

func saveProgress(ctx context.Context, kvd kv.KV, it *iter) error {
finished := it.Finished()
logger.From(ctx).Debug("Save progress",
logctx.From(ctx).Debug("Save progress",
zap.Int("finished", len(finished)))

b, err := json.Marshal(finished)
Expand Down
4 changes: 2 additions & 2 deletions app/dl/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"

"github.com/iyear/tdl/pkg/downloader"
"github.com/iyear/tdl/pkg/tmedia"
"github.com/iyear/tdl/core/downloader"
"github.com/iyear/tdl/core/tmedia"
)

type iterElem struct {
Expand Down
15 changes: 8 additions & 7 deletions app/dl/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
"github.com/go-faster/errors"
"github.com/gotd/td/telegram/peers"

"github.com/iyear/tdl/core/tutil"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/downloader"
"github.com/iyear/tdl/pkg/tmedia"
"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/core/downloader"
"github.com/iyear/tdl/core/tmedia"
"github.com/iyear/tdl/core/util/fsutil"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/tmessage"
"github.com/iyear/tdl/pkg/tplfunc"
"github.com/iyear/tdl/pkg/utils"
Expand Down Expand Up @@ -73,8 +74,8 @@ func newIter(pool dcpool.Pool, manager *peers.Manager, dialog [][]*tmessage.Dial
}

// include and exclude
includeMap := filterMap(opts.Include, utils.FS.AddPrefixDot)
excludeMap := filterMap(opts.Exclude, utils.FS.AddPrefixDot)
includeMap := filterMap(opts.Include, fsutil.AddPrefixDot)
excludeMap := filterMap(opts.Exclude, fsutil.AddPrefixDot)

// to keep fingerprint stable
sortDialogs(dialogs, opts.Desc)
Expand Down Expand Up @@ -190,7 +191,7 @@ func (i *iter) process(ctx context.Context) (ret bool, skip bool) {

if i.opts.SkipSame {
if stat, err := os.Stat(filepath.Join(i.opts.Dir, toName.String())); err == nil {
if utils.FS.GetNameWithoutExt(toName.String()) == utils.FS.GetNameWithoutExt(stat.Name()) &&
if fsutil.GetNameWithoutExt(toName.String()) == fsutil.GetNameWithoutExt(stat.Name()) &&
stat.Size() == item.Size {
return false, true
}
Expand Down
5 changes: 3 additions & 2 deletions app/dl/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"github.com/go-faster/errors"
pw "github.com/jedib0t/go-pretty/v6/progress"

"github.com/iyear/tdl/pkg/downloader"
"github.com/iyear/tdl/core/downloader"
"github.com/iyear/tdl/core/util/fsutil"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/utils"
)
Expand Down Expand Up @@ -91,7 +92,7 @@ func (p *progress) donePost(elem *iterElem) error {
}
ext := mime.Extension()
if ext != "" && (filepath.Ext(newfile) != ext) {
newfile = utils.FS.GetNameWithoutExt(newfile) + ext
newfile = fsutil.GetNameWithoutExt(newfile) + ext
}
}

Expand Down
10 changes: 5 additions & 5 deletions app/dl/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"github.com/gotd/td/tg"
"github.com/spf13/viper"

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

Expand Down Expand Up @@ -95,7 +95,7 @@ func serve(ctx context.Context,

http_io.NewHandler(u, item.Size).
WithContentType(item.MIME).
WithLog(logger.From(ctx).Named("serve")).
WithLog(logctx.From(ctx).Named("serve")).
ServeHTTP(w, r)
return nil
}))
Expand Down
2 changes: 1 addition & 1 deletion app/forward/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"

"github.com/iyear/tdl/pkg/forwarder"
"github.com/iyear/tdl/core/forwarder"
)

type iterElem struct {
Expand Down
6 changes: 3 additions & 3 deletions app/forward/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"go.uber.org/multierr"

"github.com/iyear/tdl/app/internal/tctx"
"github.com/iyear/tdl/core/tutil"
"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/core/forwarder"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/forwarder"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/storage"
Expand Down
6 changes: 3 additions & 3 deletions app/forward/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/gotd/td/tg"
"github.com/mitchellh/mapstructure"

"github.com/iyear/tdl/core/tutil"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/forwarder"
"github.com/iyear/tdl/core/dcpool"
"github.com/iyear/tdl/core/forwarder"
"github.com/iyear/tdl/core/util/tutil"
"github.com/iyear/tdl/pkg/texpr"
"github.com/iyear/tdl/pkg/tmessage"
)
Expand Down
2 changes: 1 addition & 1 deletion app/forward/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
pw "github.com/jedib0t/go-pretty/v6/progress"
"github.com/mattn/go-runewidth"

"github.com/iyear/tdl/pkg/forwarder"
"github.com/iyear/tdl/core/forwarder"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion app/internal/tctx/tctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tctx
import (
"context"

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

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,14 +14,14 @@ import (
tdtdesktop "github.com/gotd/td/session/tdesktop"
"github.com/spf13/viper"

"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"
"github.com/iyear/tdl/pkg/utils"
)

const tdata = "tdata"
Expand Down Expand Up @@ -124,7 +124,7 @@ func findDesktop(desktop string) (string, error) {

func detectAppData() string {
for _, p := range tpath.Desktop.AppData(consts.HomeDir) {
if path := appendTData(p); utils.FS.PathExists(path) {
if path := appendTData(p); fsutil.PathExists(path) {
return path
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/up/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/tg"

"github.com/iyear/tdl/pkg/uploader"
"github.com/iyear/tdl/core/uploader"
)

type iterElem struct {
Expand Down
6 changes: 3 additions & 3 deletions app/up/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/go-faster/errors"
"github.com/gotd/td/telegram/peers"

"github.com/iyear/tdl/pkg/uploader"
"github.com/iyear/tdl/pkg/utils"
"github.com/iyear/tdl/core/uploader"
"github.com/iyear/tdl/core/util/mediautil"
)

type file struct {
Expand Down Expand Up @@ -74,7 +74,7 @@ func (i *iter) Next(ctx context.Context) bool {
// has thumbnail
if cur.thumb != "" {
tMime, err := mimetype.DetectFile(cur.thumb)
if err != nil || !utils.Media.IsImage(tMime.String()) { // TODO(iyear): jpg only
if err != nil || !mediautil.IsImage(tMime.String()) { // TODO(iyear): jpg only
i.err = errors.Wrapf(err, "invalid thumbnail file: %v", cur.thumb)
return false
}
Expand Down
2 changes: 1 addition & 1 deletion app/up/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/go-faster/errors"
pw "github.com/jedib0t/go-pretty/v6/progress"

"github.com/iyear/tdl/core/uploader"
"github.com/iyear/tdl/pkg/prog"
"github.com/iyear/tdl/pkg/uploader"
"github.com/iyear/tdl/pkg/utils"
)

Expand Down
6 changes: 3 additions & 3 deletions app/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"github.com/spf13/viper"
"go.uber.org/multierr"

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

Expand Down
4 changes: 2 additions & 2 deletions app/up/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"path/filepath"
"strings"

"github.com/iyear/tdl/core/util/fsutil"
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/utils"
)

func walk(paths, excludes []string) ([]*file, error) {
Expand All @@ -33,7 +33,7 @@ func walk(paths, excludes []string) ([]*file, error) {

f := file{file: path}
t := strings.TrimRight(path, filepath.Ext(path)) + consts.UploadThumbExt
if utils.FS.PathExists(t) {
if fsutil.PathExists(t) {
f.thumb = t
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"golang.org/x/time/rate"

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

var limiter = ratelimit.New(rate.Every(500*time.Millisecond), 2)
Expand All @@ -38,7 +38,7 @@ func NewChatList() *cobra.Command {
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 chat.List(logger.Named(ctx, "ls"), c, kvd, opts)
return chat.List(logctx.Named(ctx, "ls"), c, kvd, opts)
}, limiter)
},
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func NewChatExport() *cobra.Command {
}

return tRun(cmd.Context(), func(ctx context.Context, c *telegram.Client, kvd kv.KV) error {
return chat.Export(logger.Named(ctx, "export"), c, kvd, opts)
return chat.Export(logctx.Named(ctx, "export"), c, kvd, opts)
}, limiter)
},
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func NewChatUsers() *cobra.Command {
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 chat.Users(logger.Named(ctx, "users"), c, kvd, opts)
return chat.Users(logctx.Named(ctx, "users"), c, kvd, opts)
}, limiter)
},
}
Expand Down
Loading

0 comments on commit 372f922

Please sign in to comment.