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

refactor: pkg layout #8

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ builds:
- -trimpath
ldflags:
- -s -w
- -X "github.com/autobrr/tqm/runtime.Version={{ .Version }}"
- -X "github.com/autobrr/tqm/runtime.GitCommit={{ .ShortCommit }}"
- -X "github.com/autobrr/tqm/runtime.Timestamp={{ .Timestamp }}"
- -X "github.com/autobrr/tqm/pkg/runtime.Version={{ .Version }}"
- -X "github.com/autobrr/tqm/pkg/runtime.GitCommit={{ .ShortCommit }}"
- -X "github.com/autobrr/tqm/pkg/runtime.Timestamp={{ .Timestamp }}"
goos:
- linux
- windows
Expand All @@ -40,7 +40,7 @@ builds:
goarch: arm
- goos: freebsd
goarch: arm64
main: main.go
main: cmd/tqm/main.go
binary: tqm

archives:
Expand Down
51 changes: 28 additions & 23 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,39 @@ import (
"encoding/json"
"time"

"github.com/autobrr/tqm/client"
"github.com/autobrr/tqm/config"
"github.com/autobrr/tqm/expression"
"github.com/autobrr/tqm/hardlinkfilemap"
"github.com/autobrr/tqm/logger"
"github.com/autobrr/tqm/sliceutils"
"github.com/autobrr/tqm/torrentfilemap"
"github.com/autobrr/tqm/tracker"
"github.com/autobrr/tqm/pkg/client"
"github.com/autobrr/tqm/pkg/config"
"github.com/autobrr/tqm/pkg/expression"
"github.com/autobrr/tqm/pkg/hardlinkfilemap"
"github.com/autobrr/tqm/pkg/logger"
"github.com/autobrr/tqm/pkg/sliceutils"
"github.com/autobrr/tqm/pkg/torrentfilemap"
"github.com/autobrr/tqm/pkg/tracker"

"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)

var cleanCmd = &cobra.Command{
Use: "clean [CLIENT]",
Short: "Check torrent client for torrents to remove",
Long: `This command can be used to check a torrent clients queue for torrents to remove based on its configured filters.`,
func CleanCommand() *cobra.Command {
var command = &cobra.Command{
Use: "clean [CLIENT]",
Short: "Check torrent client for torrents to remove",
Long: `This command can be used to check a torrent clients queue for torrents to remove based on its configured filters.`,

Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Args: cobra.ExactArgs(1),
}

var (
flagFilterName string
)

command.Flags().StringVar(&flagFilterName, "filter", "", "Filter to use instead of client")

command.Run = func(cmd *cobra.Command, args []string) {
// init core
if !initialized {
if !Initialized {
initCore(true)
initialized = true
Initialized = true
}

// set log
Expand Down Expand Up @@ -107,7 +116,7 @@ var cleanCmd = &cobra.Command{
log.Infof("Retrieved %d torrents", len(torrents))
}

if flagLogLevel > 1 {
if FlagLogLevel > 1 {
if b, err := json.Marshal(torrents); err != nil {
log.WithError(err).Error("Failed marshalling torrents")
} else {
Expand Down Expand Up @@ -150,11 +159,7 @@ var cleanCmd = &cobra.Command{
if err := removeEligibleTorrents(log, c, torrents, tfm, hfm); err != nil {
log.WithError(err).Fatal("Failed removing eligible torrents...")
}
},
}

func init() {
rootCmd.AddCommand(cleanCmd)
}

cleanCmd.Flags().StringVar(&flagFilterName, "filter", "", "Filter to use instead of client")
return command
}
16 changes: 8 additions & 8 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"strings"
"time"

"github.com/autobrr/tqm/client"
"github.com/autobrr/tqm/config"
"github.com/autobrr/tqm/hardlinkfilemap"
"github.com/autobrr/tqm/torrentfilemap"
"github.com/autobrr/tqm/pkg/client"
"github.com/autobrr/tqm/pkg/config"
"github.com/autobrr/tqm/pkg/hardlinkfilemap"
"github.com/autobrr/tqm/pkg/torrentfilemap"

"github.com/dustin/go-humanize"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -52,7 +52,7 @@ func retagEligibleTorrents(log *logrus.Entry, c client.TagInterface, torrents ma
log.Infof("Ratio: %.3f / Seed days: %.3f / Seeds: %d / Label: %s / Tags: %s / Tracker: %s / "+
"Tracker Status: %q", t.Ratio, t.SeedingDays, t.Seeds, t.Label, strings.Join(t.Tags, ", "), t.TrackerName, t.TrackerStatus)

if !flagDryRun {
if !FlagDryRun {
error := 0
if err := c.AddTags(t.Hash, retagInfo.Add); err != nil {
log.WithError(err).Fatalf("Failed adding tags to torrent: %+v", t)
Expand Down Expand Up @@ -113,7 +113,7 @@ func relabelEligibleTorrents(log *logrus.Entry, c client.Interface, torrents map

hardlink := false
if !tfm.IsUnique(t) {
if !flagExperimentalRelabelForCrossSeeds {
if !FlagExperimentalRelabelForCrossSeeds {
// torrent file is not unique, files are contained within another torrent
// so we cannot safely change the label in-case of auto move
nonUniqueTorrents++
Expand All @@ -134,7 +134,7 @@ func relabelEligibleTorrents(log *logrus.Entry, c client.Interface, torrents map
log.Infof("Ratio: %.3f / Seed days: %.3f / Seeds: %d / Label: %s / Tags: %s / Tracker: %s / "+
"Tracker Status: %q", t.Ratio, t.SeedingDays, t.Seeds, t.Label, strings.Join(t.Tags, ", "), t.TrackerName, t.TrackerStatus)

if !flagDryRun {
if !FlagDryRun {
if err := c.SetTorrentLabel(t.Hash, label, hardlink); err != nil {
log.WithError(err).Fatalf("Failed relabeling torrent: %+v", t)
errorRelabelTorrents++
Expand Down Expand Up @@ -184,7 +184,7 @@ func removeEligibleTorrents(log *logrus.Entry, c client.Interface, torrents map[
log.Infof("Ratio: %.3f / Seed days: %.3f / Seeds: %d / Label: %s / Tags: %s / Tracker: %s / "+
"Tracker Status: %q", t.Ratio, t.SeedingDays, t.Seeds, t.Label, strings.Join(t.Tags, ", "), t.TrackerName, t.TrackerStatus)

if !flagDryRun {
if !FlagDryRun {
// do remove
removed, err := c.RemoveTorrent(t.Hash, true)
if err != nil {
Expand Down
47 changes: 26 additions & 21 deletions cmd/orphan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@ import (
"os"
"strings"

"github.com/autobrr/tqm/client"
"github.com/autobrr/tqm/config"
"github.com/autobrr/tqm/logger"
paths "github.com/autobrr/tqm/pathutils"
"github.com/autobrr/tqm/torrentfilemap"
"github.com/autobrr/tqm/tracker"
"github.com/autobrr/tqm/pkg/client"
"github.com/autobrr/tqm/pkg/config"
"github.com/autobrr/tqm/pkg/logger"
"github.com/autobrr/tqm/pkg/pathutils"
"github.com/autobrr/tqm/pkg/torrentfilemap"
"github.com/autobrr/tqm/pkg/tracker"

"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)

var orphanCmd = &cobra.Command{
Use: "orphan [CLIENT]",
Short: "Check download location for orphan files/folders not in torrent client",
Long: `This command can be used to find files and folders in the download_location that are no longer in the torrent client.`,
func OrphanCommand() *cobra.Command {
var command = &cobra.Command{
Use: "orphan [CLIENT]",
Short: "Check download location for orphan files/folders not in torrent client",
Long: `This command can be used to find files and folders in the download_location that are no longer in the torrent client.`,
}

Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var (
flagFilterName string
)

command.Flags().StringVar(&flagFilterName, "filter", "", "Filter to use instead of client")

command.Run = func(cmd *cobra.Command, args []string) {
// init core
if !initialized {
if !Initialized {
initCore(true)
initialized = true
Initialized = true
}

// set log
Expand Down Expand Up @@ -90,7 +97,7 @@ var orphanCmd = &cobra.Command{
log.Infof("Retrieved %d torrents", len(torrents))
}

if flagLogLevel > 1 {
if FlagLogLevel > 1 {
if b, err := json.Marshal(torrents); err != nil {
log.WithError(err).Error("Failed marshalling torrents")
} else {
Expand Down Expand Up @@ -143,7 +150,7 @@ var orphanCmd = &cobra.Command{
removed := true

log.Infof("Removing orphan: %q", localPath)
if flagDryRun {
if FlagDryRun {
log.Warn("Dry-run enabled, skipping remove...")
} else {
// remove file
Expand Down Expand Up @@ -176,7 +183,7 @@ var orphanCmd = &cobra.Command{
removed := true

log.Infof("Removing orphan: %q", localPath)
if flagDryRun {
if FlagDryRun {
log.Warn("Dry-run enabled, skipping remove...")
} else {
// remove folder
Expand All @@ -199,9 +206,7 @@ var orphanCmd = &cobra.Command{
log.WithField("reclaimed_space", humanize.IBytes(removedLocalFilesSize)).
Infof("Removed orphans: %d files, %d folders and %d failures",
removedLocalFiles, removedLocalFolders, removeFailures)
},
}
}

func init() {
rootCmd.AddCommand(orphanCmd)
return command
}
53 changes: 29 additions & 24 deletions cmd/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,39 @@ import (
"encoding/json"
"time"

"github.com/autobrr/tqm/client"
"github.com/autobrr/tqm/config"
"github.com/autobrr/tqm/expression"
"github.com/autobrr/tqm/hardlinkfilemap"
"github.com/autobrr/tqm/logger"
"github.com/autobrr/tqm/sliceutils"
"github.com/autobrr/tqm/torrentfilemap"
"github.com/autobrr/tqm/tracker"

"github.com/autobrr/tqm/pkg/client"
"github.com/autobrr/tqm/pkg/expression"
"github.com/autobrr/tqm/pkg/hardlinkfilemap"
"github.com/autobrr/tqm/pkg/logger"
"github.com/autobrr/tqm/pkg/sliceutils"
"github.com/autobrr/tqm/pkg/torrentfilemap"
"github.com/autobrr/tqm/pkg/tracker"

"github.com/autobrr/tqm/pkg/config"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)

var relabelCmd = &cobra.Command{
Use: "relabel [CLIENT]",
Short: "Check torrent client for torrents to relabel",
Long: `This command can be used to check a torrent clients queue for torrents to relabel based on its configured filters.`,
func RelabelCommand() *cobra.Command {
var command = &cobra.Command{
Use: "relabel [CLIENT]",
Short: "Check torrent client for torrents to relabel",
Long: `This command can be used to check a torrent clients queue for torrents to relabel based on its configured filters.`,

Args: cobra.ExactArgs(1),
}

var (
flagFilterName string
)

Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
command.Flags().StringVar(&flagFilterName, "filter", "", "Filter to use instead of client")

command.Run = func(cmd *cobra.Command, args []string) {
// init core
if !initialized {
if !Initialized {
initCore(true)
initialized = true
Initialized = true
}

// set log
Expand Down Expand Up @@ -112,7 +121,7 @@ var relabelCmd = &cobra.Command{
log.Infof("Retrieved %d torrents", len(torrents))
}

if flagLogLevel > 1 {
if FlagLogLevel > 1 {
if b, err := json.Marshal(torrents); err != nil {
log.WithError(err).Error("Failed marshalling torrents")
} else {
Expand Down Expand Up @@ -153,11 +162,7 @@ var relabelCmd = &cobra.Command{
if err := relabelEligibleTorrents(log, c, torrents, tfm); err != nil {
log.WithError(err).Fatal("Failed relabeling eligible torrents...")
}
},
}

func init() {
rootCmd.AddCommand(relabelCmd)
}

relabelCmd.Flags().StringVar(&flagFilterName, "filter", "", "Filter to use instead of client")
return command
}
51 changes: 28 additions & 23 deletions cmd/retag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,38 @@ import (
"encoding/json"
"time"

"github.com/autobrr/tqm/client"
"github.com/autobrr/tqm/config"
"github.com/autobrr/tqm/expression"
"github.com/autobrr/tqm/hardlinkfilemap"
"github.com/autobrr/tqm/logger"
"github.com/autobrr/tqm/sliceutils"
"github.com/autobrr/tqm/tracker"

"github.com/autobrr/tqm/pkg/client"
"github.com/autobrr/tqm/pkg/expression"
"github.com/autobrr/tqm/pkg/hardlinkfilemap"
"github.com/autobrr/tqm/pkg/logger"
"github.com/autobrr/tqm/pkg/sliceutils"
"github.com/autobrr/tqm/pkg/tracker"

"github.com/autobrr/tqm/pkg/config"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)

var retagCmd = &cobra.Command{
Use: "retag [CLIENT]",
Short: "Check client (only qbit) for torrents to retag",
Long: `This command can be used to check a torrent clients queue for torrents to retag based on its configured filters.`,
func RetagCommand() *cobra.Command {
var command = &cobra.Command{
Use: "retag [CLIENT]",
Short: "Check client (only qbit) for torrents to retag",
Long: `This command can be used to check a torrent clients queue for torrents to retag based on its configured filters.`,

Args: cobra.ExactArgs(1),
}

var (
flagFilterName string
)

Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
command.Flags().StringVar(&flagFilterName, "filter", "", "Filter to use instead of client")

command.Run = func(cmd *cobra.Command, args []string) {
// init core
if !initialized {
if !Initialized {
initCore(true)
initialized = true
Initialized = true
}

// set log
Expand Down Expand Up @@ -116,7 +125,7 @@ var retagCmd = &cobra.Command{
log.Infof("Retrieved %d torrents", len(torrents))
}

if flagLogLevel > 1 {
if FlagLogLevel > 1 {
if b, err := json.Marshal(torrents); err != nil {
log.WithError(err).Error("Failed marshalling torrents")
} else {
Expand Down Expand Up @@ -164,11 +173,7 @@ var retagCmd = &cobra.Command{
if err := retagEligibleTorrents(log, ct, torrents); err != nil {
log.WithError(err).Fatal("Failed retagging eligible torrents...")
}
},
}

func init() {
rootCmd.AddCommand(retagCmd)
}

retagCmd.Flags().StringVar(&flagFilterName, "filter", "", "Filter to use instead of client")
return command
}
Loading
Loading