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

more lint #457

Merged
merged 2 commits into from
Jul 13, 2024
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
27 changes: 23 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ linters:
- exhaustive
- dupl
- nlreturn
- nonamedreturns
- varnamelen
- tagliatelle
- cyclop
- depguard
- testpackage
run:
timeout: 3m
Expand All @@ -22,4 +19,26 @@ issues:
max-issues-per-linter: 0
max-same-issues: 0
output:
sort-results: true
sort-results: true

linters-settings:
ireturn:
allow: generic,stdlib,error
depguard:
rules:
main:
files:
- $all
allow:
- $gostd
- github.com/Unpackerr/unpackerr
- github.com/lestrrat-go/apache-logformat
- github.com/gen2brain/dlgs
- github.com/getlantern/systray
- github.com/hako/durafmt
- github.com/mitchellh/go-homedir
- github.com/fsnotify/fsnotify
- github.com/radovskyb/watcher
- github.com/prometheus/client_golang/
- github.com/spf13/pflag
- github.com/julienschmidt/httprouter
24 changes: 13 additions & 11 deletions pkg/unpackerr/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,30 @@ func (u *Unpackerr) watchWorkThread() {
// retrieveAppQueues polls all the starr app queues. At the same time.
// Then calls the check methods to scan their queue contents for changes.
func (u *Unpackerr) retrieveAppQueues(now time.Time) {
wg := sync.WaitGroup{}
wg.Add(len(u.Lidarr) + len(u.Radarr) + len(u.Readarr) + len(u.Sonarr) + len(u.Whisparr))
wait := sync.WaitGroup{}
wait.Add(len(u.Lidarr) + len(u.Radarr) + len(u.Readarr) + len(u.Sonarr) + len(u.Whisparr))
// Run each app's getQueue method in a go routine as a waitgroup.
for _, server := range u.Lidarr {
u.workChan <- []func(){func() { u.getLidarrQueue(server, now) }, wg.Done}
u.workChan <- []func(){func() { u.getLidarrQueue(server, now) }, wait.Done}
}

for _, server := range u.Radarr {
u.workChan <- []func(){func() { u.getRadarrQueue(server, now) }, wg.Done}
u.workChan <- []func(){func() { u.getRadarrQueue(server, now) }, wait.Done}
}

for _, server := range u.Readarr {
u.workChan <- []func(){func() { u.getReadarrQueue(server, now) }, wg.Done}
u.workChan <- []func(){func() { u.getReadarrQueue(server, now) }, wait.Done}
}

for _, server := range u.Sonarr {
u.workChan <- []func(){func() { u.getSonarrQueue(server, now) }, wg.Done}
u.workChan <- []func(){func() { u.getSonarrQueue(server, now) }, wait.Done}
}

for _, server := range u.Whisparr {
u.workChan <- []func(){func() { u.getWhisparrQueue(server, now) }, wg.Done}
u.workChan <- []func(){func() { u.getWhisparrQueue(server, now) }, wait.Done}
}

wg.Wait()
wait.Wait()
// These are not thread safe because they call saveCompletedDownload.
u.checkLidarrQueue(now)
u.checkRadarrQueue(now)
Expand Down Expand Up @@ -193,9 +193,11 @@ func (slice StringSlice) MarshalENV(tag string) (map[string]string, error) {
return map[string]string{tag: strings.Join(slice, ",")}, nil
}

func buildStatusReason(status string, messages []*starr.StatusMessage) (output string) {
for i := range messages {
for _, msg := range messages[i].Messages {
func buildStatusReason(status string, messages []*starr.StatusMessage) string {
var output string

for idx := range messages {
for _, msg := range messages[idx].Messages {
if output != "" {
output += "; "
}
Expand Down
38 changes: 20 additions & 18 deletions pkg/unpackerr/cmdhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ var (
)

func (u *Unpackerr) validateCmdhook() error {
for i := range u.Cmdhook {
u.Cmdhook[i].URL = ""
for idx := range u.Cmdhook {
u.Cmdhook[idx].URL = ""

u.Cmdhook[i].Command = expandHomedir(u.Cmdhook[i].Command)
if u.Cmdhook[i].Command == "" {
u.Cmdhook[idx].Command = expandHomedir(u.Cmdhook[idx].Command)
if u.Cmdhook[idx].Command == "" {
return ErrCmdhookNoCmd
}

if u.Cmdhook[i].Name == "" {
u.Cmdhook[i].Name = strings.Fields(u.Cmdhook[i].Command)[0]
if u.Cmdhook[idx].Name == "" {
u.Cmdhook[idx].Name = strings.Fields(u.Cmdhook[idx].Command)[0]
}

if u.Cmdhook[i].Timeout.Duration == 0 {
u.Cmdhook[i].Timeout.Duration = u.Timeout.Duration
if u.Cmdhook[idx].Timeout.Duration == 0 {
u.Cmdhook[idx].Timeout.Duration = u.Timeout.Duration
}

if len(u.Cmdhook[i].Events) == 0 {
u.Cmdhook[i].Events = []ExtractStatus{WAITING}
if len(u.Cmdhook[idx].Events) == 0 {
u.Cmdhook[idx].Events = []ExtractStatus{WAITING}
}
}

Expand Down Expand Up @@ -121,27 +121,29 @@ func (u *Unpackerr) runCmdhook(hook *WebhookConfig, payload *WebhookPayload) (*b
}

func (u *Unpackerr) logCmdhook() {
var pfx string
var prefix string

if len(u.Cmdhook) == 1 {
pfx = " => Command Hook Config: 1 cmd"
prefix = " => Command Hook Config: 1 cmd"
} else {
u.Printf(" => Command Hook Configs: %d commands", len(u.Cmdhook))
pfx = " => Command" //nolint:wsl
prefix = " => Command" //nolint:wsl
}

for _, f := range u.Cmdhook {
u.Printf("%s: %s, timeout: %v, silent: %v, events: %v, shell: %v, cmd: %s",
pfx, f.Name, f.Timeout, f.Silent, logEvents(f.Events), f.Shell, f.Command)
prefix, f.Name, f.Timeout, f.Silent, logEvents(f.Events), f.Shell, f.Command)
}
}

// CmdhookCounts returns the total count of requests and errors for all webhooks.
func (u *Unpackerr) CmdhookCounts() (total uint, fails uint) {
func (u *Unpackerr) CmdhookCounts() (uint, uint) {
var total, fails uint

for _, hook := range u.Cmdhook {
t, f := hook.Counts()
total += t
fails += f
posts, failures := hook.Counts()
total += posts
fails += failures
}

return total, fails
Expand Down
84 changes: 42 additions & 42 deletions pkg/unpackerr/cnfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ const (
)

func (u *Unpackerr) unmarshalConfig() (uint64, uint64, string, error) {
var f, msg string
var configFile, msg string

// Load up the default file path and a list of alternate paths.
def, cfl := configFileLocactions()
// Search for one, starting with the default.
for _, f = range append([]string{u.Flags.ConfigFile}, cfl...) {
f = expandHomedir(f)
if _, err := os.Stat(f); err == nil {
for _, configFile = range append([]string{u.Flags.ConfigFile}, cfl...) {
configFile = expandHomedir(configFile)
if _, err := os.Stat(configFile); err == nil {
break // found one, bail out.
} // else { u.Print("rip:", err) }

f = ""
configFile = ""
}

// it's possible to get here with or without a file found.
msg = msgNoConfigFile

if f != "" {
u.Flags.ConfigFile, _ = filepath.Abs(f)
if configFile != "" {
u.Flags.ConfigFile, _ = filepath.Abs(configFile)
msg = msgConfigFound + u.Flags.ConfigFileWithAge()

if err := cnfgfile.Unmarshal(u.Config, u.Flags.ConfigFile); err != nil {
Expand All @@ -67,9 +67,9 @@ func (u *Unpackerr) unmarshalConfig() (uint64, uint64, string, error) {
return 0, 0, msg, err
}

fm, dm := u.validateConfig()
fileMode, dirMode := u.validateConfig()

return fm, dm, msg, nil
return fileMode, dirMode, msg, nil
}

func (f *Flags) ConfigFileWithAge() string {
Expand Down Expand Up @@ -130,16 +130,16 @@ func (u *Unpackerr) validateConfig() (uint64, uint64) { //nolint:cyclop
base = 32
)

fm, err := strconv.ParseUint(u.FileMode, bits, base)
fileMode, err := strconv.ParseUint(u.FileMode, bits, base)
if err != nil || u.FileMode == "" {
fm = defaultFileMode
u.FileMode = strconv.FormatUint(fm, bits)
fileMode = defaultFileMode
u.FileMode = strconv.FormatUint(fileMode, bits)
}

dm, err := strconv.ParseUint(u.DirMode, bits, base)
dirMode, err := strconv.ParseUint(u.DirMode, bits, base)
if err != nil || u.DirMode == "" {
dm = defaultDirMode
u.DirMode = strconv.FormatUint(dm, bits)
dirMode = defaultDirMode
u.DirMode = strconv.FormatUint(dirMode, bits)
}

if u.Parallel == 0 {
Expand Down Expand Up @@ -176,7 +176,7 @@ func (u *Unpackerr) validateConfig() (uint64, uint64) { //nolint:cyclop
u.History.Items = make([]string, u.KeepHistory)
}

return fm, dm
return fileMode, dirMode
}

// createConfigFile attempts to avoid creating a config file on linux or freebsd.
Expand All @@ -202,15 +202,15 @@ func (u *Unpackerr) createConfigFile(file string) (string, error) {
return "", fmt.Errorf("making config dir: %w", err)
}

f, err := os.Create(file)
fOpen, err := os.Create(file)
if err != nil {
return "", fmt.Errorf("creating config file: %w", err)
}
defer f.Close()
defer fOpen.Close()

if a, err := bindata.Asset("../../examples/unpackerr.conf.example"); err != nil {
if asset, err := bindata.Asset("../../examples/unpackerr.conf.example"); err != nil {
return "", fmt.Errorf("getting config file: %w", err)
} else if _, err = f.Write(a); err != nil {
} else if _, err = fOpen.Write(asset); err != nil {
return "", fmt.Errorf("writing config file: %w", err)
}

Expand Down Expand Up @@ -270,54 +270,54 @@ func expandHomedir(filePath string) string {
return expanded
}

func (u *Unpackerr) validateApp(s *StarrConfig, app starr.App) error {
if s.URL == "" {
func (u *Unpackerr) validateApp(conf *StarrConfig, app starr.App) error {
if conf.URL == "" {
u.Errorf("Missing %s URL in one of your configurations, skipped and ignored.", app)
return ErrInvalidURL // this error is not printed.
}

if s.APIKey == "" {
if conf.APIKey == "" {
u.Errorf("Missing %s API Key in one of your configurations, skipped and ignored.", app)
return ErrInvalidURL // this error is not printed.
}

if !strings.HasPrefix(s.URL, "http://") && !strings.HasPrefix(s.URL, "https://") {
return fmt.Errorf("%w: (%s) %s", ErrInvalidURL, app, s.URL)
if !strings.HasPrefix(conf.URL, "http://") && !strings.HasPrefix(conf.URL, "https://") {
return fmt.Errorf("%w: (%s) %s", ErrInvalidURL, app, conf.URL)
}

if len(s.APIKey) != apiKeyLength {
if len(conf.APIKey) != apiKeyLength {
return fmt.Errorf("%s (%s) %w, your key length: %d",
app, s.URL, ErrInvalidKey, len(s.APIKey))
app, conf.URL, ErrInvalidKey, len(conf.APIKey))
}

if s.Timeout.Duration == 0 {
s.Timeout.Duration = u.Timeout.Duration
if conf.Timeout.Duration == 0 {
conf.Timeout.Duration = u.Timeout.Duration
}

if s.DeleteDelay.Duration == 0 {
s.DeleteDelay.Duration = u.DeleteDelay.Duration
if conf.DeleteDelay.Duration == 0 {
conf.DeleteDelay.Duration = u.DeleteDelay.Duration
}

if s.Path != "" {
s.Paths = append(s.Paths, s.Path)
if conf.Path != "" {
conf.Paths = append(conf.Paths, conf.Path)
}

for idx, path := range s.Paths {
s.Paths[idx] = expandHomedir(path)
for idx, path := range conf.Paths {
conf.Paths[idx] = expandHomedir(path)
}

if len(s.Paths) == 0 {
s.Paths = []string{defaultSavePath}
if len(conf.Paths) == 0 {
conf.Paths = []string{defaultSavePath}
}

if s.Protocols == "" {
s.Protocols = defaultProtocol
if conf.Protocols == "" {
conf.Protocols = defaultProtocol
}

s.Config.Client = &http.Client{
Timeout: s.Timeout.Duration,
conf.Config.Client = &http.Client{
Timeout: conf.Timeout.Duration,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !s.ValidSSL}, //nolint:gosec
TLSClientConfig: &tls.Config{InsecureSkipVerify: !conf.ValidSSL}, //nolint:gosec
},
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/unpackerr/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,19 @@ func (u *Unpackerr) logFolders() {
epath = ", extract to: " + folder.ExtractPath
}

u.Printf(" => Folder Config: 1 path: %s%s (delete after:%v, delete orig:%v, "+
"log file: %v, move back:%v, isos:%v, event buffer:%d)",
u.Printf(" => Folder Config: 1 path: %s%s (delete_after:%v, delete_orig:%v, "+
"log_file:%v, move_back:%v, isos:%v, event_buffer:%d)",
folder.Path, epath, folder.DeleteAfter, folder.DeleteOrig,
!folder.DisableLog, folder.MoveBack, folder.ExtractISOs, u.Folder.Buffer)
} else {
u.Printf(" => Folder Config: %d paths, event buffer: %d ", count, u.Folder.Buffer)
u.Printf(" => Folder Config: %d paths, event_buffer:%d ", count, u.Folder.Buffer)

for _, folder := range u.Folders {
if epath = ""; folder.ExtractPath != "" {
epath = ", extract to: " + folder.ExtractPath
}

u.Printf(" => Path: %s%s (delete after:%v, delete orig:%v, log file: %v, move back:%v, isos:%v)",
u.Printf(" => Path: %s%s (delete_after:%v, delete_orig:%v, log_file:%v, move_back:%v, isos:%v)",
folder.Path, epath, folder.DeleteAfter, folder.DeleteOrig, !folder.DisableLog, folder.MoveBack, folder.ExtractISOs)
}
}
Expand Down Expand Up @@ -397,10 +397,10 @@ func (f *Folders) handleFileEvent(name, operation string) {
}

// processEvent (below) handles events sent to f.Events.
if p := filepath.Dir(name); p == cnfg.Path {
if dir := filepath.Dir(name); dir == cnfg.Path {
f.Events <- &eventData{name: filepath.Base(name), cnfg: cnfg, file: name, op: operation}
} else {
f.Events <- &eventData{name: filepath.Base(p), cnfg: cnfg, file: name, op: operation}
f.Events <- &eventData{name: filepath.Base(dir), cnfg: cnfg, file: name, op: operation}
}

return
Expand Down
Loading