feat(confg): reload on save and refactor logging (#275)

* feat(confg): reload on save

* refactor(logging): rework
This commit is contained in:
Ludvig Lundgren 2022-05-20 09:27:01 +02:00 committed by GitHub
parent 198528a474
commit 91b094f4f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 995 additions and 873 deletions

View file

@ -1,6 +1,7 @@
package domain
type Config struct {
Version string
ConfigPath string
Host string `toml:"host"`
Port int `toml:"port"`

View file

@ -3,11 +3,11 @@ package domain
import (
"bytes"
"context"
"errors"
"net/url"
"text/template"
"github.com/dustin/go-humanize"
"github.com/rs/zerolog/log"
)
type IndexerRepo interface {
@ -148,15 +148,13 @@ func (p *IndexerParse) ParseTorrentUrl(vars map[string]string, extraVars map[str
// setup text template to inject variables into
tmpl, err := template.New("torrenturl").Parse(p.Match.TorrentURL)
if err != nil {
log.Error().Err(err).Msg("could not create torrent url template")
return err
return errors.New("could not create torrent url template")
}
var urlBytes bytes.Buffer
err = tmpl.Execute(&urlBytes, &tmpVars)
if err != nil {
log.Error().Err(err).Msg("could not write torrent url template output")
return err
return errors.New("could not write torrent url template output")
}
release.TorrentURL = urlBytes.String()

View file

@ -20,7 +20,6 @@ import (
"github.com/dustin/go-humanize"
"github.com/moistari/rls"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
type ReleaseRepo interface {
@ -274,8 +273,7 @@ func (r *Release) DownloadTorrentFile() error {
req, err := http.NewRequest("GET", r.TorrentURL, nil)
if err != nil {
log.Error().Stack().Err(err).Msg("error downloading file")
return err
return errors.Wrap(err, "error downloading file")
}
if r.RawCookie != "" {
@ -287,43 +285,37 @@ func (r *Release) DownloadTorrentFile() error {
// Get the data
resp, err := client.Do(req)
if err != nil {
log.Error().Stack().Err(err).Msg("error downloading file")
return err
return errors.Wrap(err, "error downloading file")
}
defer resp.Body.Close()
// retry logic
if resp.StatusCode != http.StatusOK {
log.Error().Stack().Err(err).Msgf("error downloading file from: %v - bad status: %d", r.TorrentURL, resp.StatusCode)
return fmt.Errorf("error downloading torrent (%v) file (%v) from '%v' - status code: %d", r.TorrentName, r.TorrentURL, r.Indexer, resp.StatusCode)
}
// Create tmp file
tmpFile, err := os.CreateTemp("", "autobrr-")
if err != nil {
log.Error().Stack().Err(err).Msg("error creating temp file")
return err
return errors.Wrap(err, "error creating tmp file")
}
defer tmpFile.Close()
// Write the body to file
_, err = io.Copy(tmpFile, resp.Body)
if err != nil {
log.Error().Stack().Err(err).Msgf("error writing downloaded file: %v", tmpFile.Name())
return err
return errors.Wrap(err, fmt.Sprintf("error writing downloaded file: %v", tmpFile.Name()))
}
meta, err := metainfo.LoadFromFile(tmpFile.Name())
if err != nil {
log.Error().Stack().Err(err).Msgf("metainfo could not load file contents: %v", tmpFile.Name())
return err
return errors.Wrap(err, fmt.Sprintf("metainfo could not load file contents: %v", tmpFile.Name()))
}
torrentMetaInfo, err := meta.UnmarshalInfo()
if err != nil {
log.Error().Stack().Err(err).Msgf("metainfo could not unmarshal info from torrent: %v", tmpFile.Name())
return err
return errors.Wrap(err, fmt.Sprintf("metainfo could not unmarshal info from torrent: %v", tmpFile.Name()))
}
r.TorrentTmpFile = tmpFile.Name()
@ -332,7 +324,7 @@ func (r *Release) DownloadTorrentFile() error {
// remove file if fail
log.Debug().Msgf("successfully downloaded file: %v", tmpFile.Name())
//log.Debug().Msgf("successfully downloaded file: %v", tmpFile.Name())
return nil
}

View file

@ -438,7 +438,7 @@ func TestRelease_MapVars(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := tt.fields
_ = r.MapVars(tt.args.definition, tt.args.varMap)
_ = r.MapVars(&tt.args.definition, tt.args.varMap)
assert.Equal(t, tt.want, r)
})