mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Move default values to model package
This commit is contained in:
@@ -9,13 +9,6 @@ import (
|
|||||||
"github.com/mxpv/podsync/pkg/model"
|
"github.com/mxpv/podsync/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
DefaultFormat = model.FormatVideo
|
|
||||||
DefaultQuality = model.QualityHigh
|
|
||||||
DefaultPageSize = 50
|
|
||||||
DefaultUpdatePeriod = 24 * time.Hour
|
|
||||||
)
|
|
||||||
|
|
||||||
// Feed is a configuration for a feed
|
// Feed is a configuration for a feed
|
||||||
type Feed struct {
|
type Feed struct {
|
||||||
// URL is a full URL of the field
|
// URL is a full URL of the field
|
||||||
@@ -73,30 +66,33 @@ func LoadConfig(path string) (*Config, error) {
|
|||||||
return nil, errors.Wrap(err, "failed to load config file")
|
return nil, errors.Wrap(err, "failed to load config file")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply defaults
|
config.applyDefaults()
|
||||||
if config.Server.Hostname == "" {
|
|
||||||
config.Server.Hostname = "http://localhost"
|
return &config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) applyDefaults() {
|
||||||
|
if c.Server.Hostname == "" {
|
||||||
|
c.Server.Hostname = model.DefaultHostname
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, feed := range config.Feeds {
|
for _, feed := range c.Feeds {
|
||||||
if feed.UpdatePeriod.Duration == 0 {
|
if feed.UpdatePeriod.Duration == 0 {
|
||||||
feed.UpdatePeriod.Duration = DefaultUpdatePeriod
|
feed.UpdatePeriod.Duration = model.DefaultUpdatePeriod
|
||||||
}
|
}
|
||||||
|
|
||||||
if feed.Quality == "" {
|
if feed.Quality == "" {
|
||||||
feed.Quality = DefaultQuality
|
feed.Quality = model.DefaultQuality
|
||||||
}
|
}
|
||||||
|
|
||||||
if feed.Format == "" {
|
if feed.Format == "" {
|
||||||
feed.Format = DefaultFormat
|
feed.Format = model.DefaultFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
if feed.PageSize == 0 {
|
if feed.PageSize == 0 {
|
||||||
feed.PageSize = DefaultPageSize
|
feed.PageSize = model.DefaultPageSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &config, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Duration struct {
|
type Duration struct {
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/mxpv/podsync/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadConfig(t *testing.T) {
|
func TestLoadConfig(t *testing.T) {
|
||||||
@@ -79,7 +81,7 @@ func TestApplyDefaults(t *testing.T) {
|
|||||||
feed, ok := config.Feeds["A"]
|
feed, ok := config.Feeds["A"]
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
|
|
||||||
assert.EqualValues(t, feed.UpdatePeriod, Duration{DefaultUpdatePeriod})
|
assert.EqualValues(t, feed.UpdatePeriod, Duration{model.DefaultUpdatePeriod})
|
||||||
assert.EqualValues(t, feed.PageSize, 50)
|
assert.EqualValues(t, feed.PageSize, 50)
|
||||||
assert.EqualValues(t, feed.Quality, "high")
|
assert.EqualValues(t, feed.Quality, "high")
|
||||||
assert.EqualValues(t, feed.Format, "video")
|
assert.EqualValues(t, feed.Format, "video")
|
||||||
|
|||||||
13
pkg/model/defaults.go
Normal file
13
pkg/model/defaults.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultHostname = "http://localhost"
|
||||||
|
DefaultFormat = FormatVideo
|
||||||
|
DefaultQuality = QualityHigh
|
||||||
|
DefaultPageSize = 50
|
||||||
|
DefaultUpdatePeriod = 24 * time.Hour
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user