Skip to content

Commit 5483455

Browse files
committed
Decouple config package
1 parent 0edf283 commit 5483455

21 files changed

Lines changed: 156 additions & 161 deletions
Lines changed: 13 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,22 @@
1-
package config
1+
package main
22

33
import (
44
"fmt"
55
"io/ioutil"
66
"path/filepath"
77
"regexp"
8-
"time"
98

109
"github.com/hashicorp/go-multierror"
1110
"github.com/pelletier/go-toml"
1211
"github.com/pkg/errors"
1312

13+
"github.com/mxpv/podsync/pkg/db"
14+
"github.com/mxpv/podsync/pkg/feed"
1415
"github.com/mxpv/podsync/pkg/model"
16+
"github.com/mxpv/podsync/pkg/ytdl"
1517
)
1618

17-
// Feed is a configuration for a feed
18-
type Feed struct {
19-
ID string `toml:"-"`
20-
// URL is a full URL of the field
21-
URL string `toml:"url"`
22-
// PageSize is the number of pages to query from YouTube API.
23-
// NOTE: larger page sizes/often requests might drain your API token.
24-
PageSize int `toml:"page_size"`
25-
// UpdatePeriod is how often to check for updates.
26-
// Format is "300ms", "1.5h" or "2h45m".
27-
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
28-
// NOTE: too often update check might drain your API token.
29-
UpdatePeriod time.Duration `toml:"update_period"`
30-
// Cron expression format is how often to check update
31-
// NOTE: too often update check might drain your API token.
32-
CronSchedule string `toml:"cron_schedule"`
33-
// Quality to use for this feed
34-
Quality model.Quality `toml:"quality"`
35-
// Maximum height of video
36-
MaxHeight int `toml:"max_height"`
37-
// Format to use for this feed
38-
Format model.Format `toml:"format"`
39-
// Only download episodes that match this regexp (defaults to matching anything)
40-
Filters Filters `toml:"filters"`
41-
// Clean is a cleanup policy to use for this feed
42-
Clean Cleanup `toml:"clean"`
43-
// Custom is a list of feed customizations
44-
Custom Custom `toml:"custom"`
45-
// List of additional youtube-dl arguments passed at download time
46-
YouTubeDLArgs []string `toml:"youtube_dl_args"`
47-
// Included in OPML file
48-
OPML bool `toml:"opml"`
49-
// Playlist sort
50-
PlaylistSort model.Sorting `toml:"playlist_sort"`
51-
}
52-
53-
type Filters struct {
54-
Title string `toml:"title"`
55-
NotTitle string `toml:"not_title"`
56-
Description string `toml:"description"`
57-
NotDescription string `toml:"not_description"`
58-
// More filters to be added here
59-
}
60-
61-
type Custom struct {
62-
CoverArt string `toml:"cover_art"`
63-
CoverArtQuality model.Quality `toml:"cover_art_quality"`
64-
Category string `toml:"category"`
65-
Subcategories []string `toml:"subcategories"`
66-
Explicit bool `toml:"explicit"`
67-
Language string `toml:"lang"`
68-
Author string `toml:"author"`
69-
Title string `toml:"title"`
70-
Description string `toml:"description"`
71-
OwnerName string `toml:"ownerName"`
72-
OwnerEmail string `toml:"ownerEmail"`
73-
}
74-
75-
type Server struct {
19+
type ServerConfig struct {
7620
// Hostname to use for download links
7721
Hostname string `toml:"hostname"`
7822
// Port is a server port to listen to
@@ -88,24 +32,6 @@ type Server struct {
8832
DataDir string `toml:"data_dir"`
8933
}
9034

91-
type Database struct {
92-
// Dir is a directory to keep database files
93-
Dir string `toml:"dir"`
94-
Badger *Badger `toml:"badger"`
95-
}
96-
97-
// Badger represents BadgerDB configuration parameters
98-
// See https://github.com/dgraph-io/badger#memory-usage
99-
type Badger struct {
100-
Truncate bool `toml:"truncate"`
101-
FileIO bool `toml:"file_io"`
102-
}
103-
104-
type Cleanup struct {
105-
// KeepLast defines how many episodes to keep
106-
KeepLast int `toml:"keep_last"`
107-
}
108-
10935
type Log struct {
11036
// Filename to write the log to (instead of stdout)
11137
Filename string `toml:"filename"`
@@ -119,28 +45,20 @@ type Log struct {
11945
Compress bool `toml:"compress"`
12046
}
12147

122-
// Downloader is a youtube-dl related configuration
123-
type Downloader struct {
124-
// SelfUpdate toggles self update every 24 hour
125-
SelfUpdate bool `toml:"self_update"`
126-
// Timeout in minutes for youtube-dl process to finish download
127-
Timeout int `toml:"timeout"`
128-
}
129-
13048
type Config struct {
13149
// Server is the web server configuration
132-
Server Server `toml:"server"`
50+
Server ServerConfig `toml:"server"`
13351
// Log is the optional logging configuration
13452
Log Log `toml:"log"`
13553
// Database configuration
136-
Database Database `toml:"database"`
54+
Database db.Config `toml:"database"`
13755
// Feeds is a list of feeds to host by this app.
13856
// ID will be used as feed ID in http://podsync.net/{FEED_ID}.xml
139-
Feeds map[string]*Feed
57+
Feeds map[string]*feed.Config
14058
// Tokens is API keys to use to access YouTube/Vimeo APIs.
14159
Tokens map[model.Provider][]string `toml:"tokens"`
14260
// Downloader (youtube-dl) configuration
143-
Downloader Downloader `toml:"downloader"`
61+
Downloader ytdl.Config `toml:"downloader"`
14462
}
14563

14664
// LoadConfig loads TOML configuration from a file path
@@ -155,8 +73,8 @@ func LoadConfig(path string) (*Config, error) {
15573
return nil, errors.Wrap(err, "failed to unmarshal toml")
15674
}
15775

158-
for id, feed := range config.Feeds {
159-
feed.ID = id
76+
for id, f := range config.Feeds {
77+
f.ID = id
16078
}
16179

16280
config.applyDefaults(path)
@@ -186,8 +104,8 @@ func (c *Config) validate() error {
186104
result = multierror.Append(result, errors.New("at least one feed must be specified"))
187105
}
188106

189-
for id, feed := range c.Feeds {
190-
if feed.URL == "" {
107+
for id, f := range c.Feeds {
108+
if f.URL == "" {
191109
result = multierror.Append(result, errors.Errorf("URL is required for %q", id))
192110
}
193111
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package config
1+
package main
22

33
import (
44
"io/ioutil"
@@ -173,7 +173,7 @@ data_dir = "/data"
173173

174174
func TestDefaultHostname(t *testing.T) {
175175
cfg := Config{
176-
Server: Server{},
176+
Server: ServerConfig{},
177177
}
178178

179179
t.Run("empty hostname", func(t *testing.T) {

cmd/podsync/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"time"
1111

1212
"github.com/jessevdk/go-flags"
13+
"github.com/mxpv/podsync/pkg/feed"
1314
"github.com/robfig/cron/v3"
1415
log "github.com/sirupsen/logrus"
1516
"golang.org/x/sync/errgroup"
1617
"gopkg.in/natefinch/lumberjack.v2"
1718

18-
"github.com/mxpv/podsync/pkg/config"
1919
"github.com/mxpv/podsync/pkg/db"
2020
"github.com/mxpv/podsync/pkg/fs"
2121
"github.com/mxpv/podsync/pkg/ytdl"
@@ -75,7 +75,7 @@ func main() {
7575

7676
// Load TOML file
7777
log.Debugf("loading configuration %q", opts.ConfigPath)
78-
cfg, err := config.LoadConfig(opts.ConfigPath)
78+
cfg, err := LoadConfig(opts.ConfigPath)
7979
if err != nil {
8080
log.WithError(err).Fatal("failed to load configuration file")
8181
}
@@ -121,7 +121,7 @@ func main() {
121121
}
122122

123123
// Queue of feeds to update
124-
updates := make(chan *config.Feed, 16)
124+
updates := make(chan *feed.Config, 16)
125125
defer close(updates)
126126

127127
// Create Cron

cmd/podsync/server.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import (
55
"net/http"
66

77
log "github.com/sirupsen/logrus"
8-
9-
"github.com/mxpv/podsync/pkg/config"
108
)
119

1210
type Server struct {
1311
http.Server
1412
}
1513

16-
func NewServer(cfg *config.Config, storage http.FileSystem) *Server {
14+
func NewServer(cfg *Config, storage http.FileSystem) *Server {
1715
port := cfg.Server.Port
1816
if port == 0 {
1917
port = 8080

cmd/podsync/updater.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
log "github.com/sirupsen/logrus"
1616

1717
"github.com/mxpv/podsync/pkg/builder"
18-
"github.com/mxpv/podsync/pkg/config"
1918
"github.com/mxpv/podsync/pkg/db"
2019
"github.com/mxpv/podsync/pkg/feed"
2120
"github.com/mxpv/podsync/pkg/fs"
@@ -24,18 +23,18 @@ import (
2423
)
2524

2625
type Downloader interface {
27-
Download(ctx context.Context, feedConfig *config.Feed, episode *model.Episode) (io.ReadCloser, error)
26+
Download(ctx context.Context, feedConfig *feed.Config, episode *model.Episode) (io.ReadCloser, error)
2827
}
2928

3029
type Updater struct {
31-
config *config.Config
30+
config *Config
3231
downloader Downloader
3332
db db.Storage
3433
fs fs.Storage
3534
keys map[model.Provider]feed.KeyProvider
3635
}
3736

38-
func NewUpdater(config *config.Config, downloader Downloader, db db.Storage, fs fs.Storage) (*Updater, error) {
37+
func NewUpdater(config *Config, downloader Downloader, db db.Storage, fs fs.Storage) (*Updater, error) {
3938
keys := map[model.Provider]feed.KeyProvider{}
4039

4140
for name, list := range config.Tokens {
@@ -55,7 +54,7 @@ func NewUpdater(config *config.Config, downloader Downloader, db db.Storage, fs
5554
}, nil
5655
}
5756

58-
func (u *Updater) Update(ctx context.Context, feedConfig *config.Feed) error {
57+
func (u *Updater) Update(ctx context.Context, feedConfig *feed.Config) error {
5958
log.WithFields(log.Fields{
6059
"feed_id": feedConfig.ID,
6160
"format": feedConfig.Format,
@@ -90,7 +89,7 @@ func (u *Updater) Update(ctx context.Context, feedConfig *config.Feed) error {
9089
}
9190

9291
// updateFeed pulls API for new episodes and saves them to database
93-
func (u *Updater) updateFeed(ctx context.Context, feedConfig *config.Feed) error {
92+
func (u *Updater) updateFeed(ctx context.Context, feedConfig *feed.Config) error {
9493
info, err := builder.ParseURL(feedConfig.URL)
9594
if err != nil {
9695
return errors.Wrapf(err, "failed to parse URL: %s", feedConfig.URL)
@@ -162,7 +161,7 @@ func (u *Updater) matchRegexpFilter(pattern, str string, negative bool, logger l
162161
return true
163162
}
164163

165-
func (u *Updater) matchFilters(episode *model.Episode, filters *config.Filters) bool {
164+
func (u *Updater) matchFilters(episode *model.Episode, filters *feed.Filters) bool {
166165
logger := log.WithFields(log.Fields{"episode_id": episode.ID})
167166
if !u.matchRegexpFilter(filters.Title, episode.Title, false, logger.WithField("filter", "title")) {
168167
return false
@@ -181,7 +180,7 @@ func (u *Updater) matchFilters(episode *model.Episode, filters *config.Filters)
181180
return true
182181
}
183182

184-
func (u *Updater) downloadEpisodes(ctx context.Context, feedConfig *config.Feed) error {
183+
func (u *Updater) downloadEpisodes(ctx context.Context, feedConfig *feed.Config) error {
185184
var (
186185
feedID = feedConfig.ID
187186
downloadList []*model.Episode
@@ -308,7 +307,7 @@ func (u *Updater) downloadEpisodes(ctx context.Context, feedConfig *config.Feed)
308307
return nil
309308
}
310309

311-
func (u *Updater) buildXML(ctx context.Context, feedConfig *config.Feed) error {
310+
func (u *Updater) buildXML(ctx context.Context, feedConfig *feed.Config) error {
312311
f, err := u.db.GetFeed(ctx, feedConfig.ID)
313312
if err != nil {
314313
return err
@@ -336,7 +335,7 @@ func (u *Updater) buildXML(ctx context.Context, feedConfig *config.Feed) error {
336335
func (u *Updater) buildOPML(ctx context.Context) error {
337336
// Build OPML with data received from builder
338337
log.Debug("building podcast OPML")
339-
opml, err := feed.BuildOPML(ctx, u.config, u.db, u.config.Server.Hostname)
338+
opml, err := feed.BuildOPML(ctx, u.config.Feeds, u.db, u.config.Server.Hostname)
340339
if err != nil {
341340
return err
342341
}
@@ -353,7 +352,7 @@ func (u *Updater) buildOPML(ctx context.Context) error {
353352
return nil
354353
}
355354

356-
func (u *Updater) cleanup(ctx context.Context, feedConfig *config.Feed) error {
355+
func (u *Updater) cleanup(ctx context.Context, feedConfig *feed.Config) error {
357356
var (
358357
feedID = feedConfig.ID
359358
logger = log.WithField("feed_id", feedID)

pkg/builder/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package builder
33
import (
44
"context"
55

6+
"github.com/mxpv/podsync/pkg/feed"
67
"github.com/pkg/errors"
78

8-
"github.com/mxpv/podsync/pkg/config"
99
"github.com/mxpv/podsync/pkg/model"
1010
)
1111

1212
type Builder interface {
13-
Build(ctx context.Context, cfg *config.Feed) (*model.Feed, error)
13+
Build(ctx context.Context, cfg *feed.Config) (*model.Feed, error)
1414
}
1515

1616
func New(ctx context.Context, provider model.Provider, key string) (Builder, error) {

pkg/builder/soundcloud.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import (
55
"strconv"
66
"time"
77

8+
"github.com/mxpv/podsync/pkg/feed"
89
"github.com/pkg/errors"
910
soundcloudapi "github.com/zackradisic/soundcloud-api"
1011

11-
"github.com/mxpv/podsync/pkg/config"
1212
"github.com/mxpv/podsync/pkg/model"
1313
)
1414

1515
type SoundCloudBuilder struct {
1616
client *soundcloudapi.API
1717
}
1818

19-
func (s *SoundCloudBuilder) Build(_ctx context.Context, cfg *config.Feed) (*model.Feed, error) {
19+
func (s *SoundCloudBuilder) Build(_ctx context.Context, cfg *feed.Config) (*model.Feed, error) {
2020
info, err := ParseURL(cfg.URL)
2121
if err != nil {
2222
return nil, err

pkg/builder/soundcloud_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package builder
33
import (
44
"testing"
55

6+
"github.com/mxpv/podsync/pkg/feed"
67
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
8-
9-
"github.com/mxpv/podsync/pkg/config"
109
)
1110

1211
func TestSC_BUILDFEED(t *testing.T) {
@@ -20,7 +19,7 @@ func TestSC_BUILDFEED(t *testing.T) {
2019

2120
for _, addr := range urls {
2221
t.Run(addr, func(t *testing.T) {
23-
feed, err := builder.Build(testCtx, &config.Feed{URL: addr})
22+
feed, err := builder.Build(testCtx, &feed.Config{URL: addr})
2423
require.NoError(t, err)
2524

2625
assert.NotEmpty(t, feed.Title)

0 commit comments

Comments
 (0)