From db4de62430dee991d0ad27baa872fda00c9a7a3f Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Sun, 2 Jan 2022 13:46:43 +0200 Subject: [PATCH] Migrate to pelletier/go-toml --- README.md | 2 +- go.mod | 4 +--- go.sum | 8 ++------ pkg/config/config.go | 11 ++++++----- pkg/config/config_test.go | 11 ++++------- pkg/config/toml.go | 41 --------------------------------------- 6 files changed, 14 insertions(+), 63 deletions(-) delete mode 100644 pkg/config/toml.go diff --git a/README.md b/README.md index 21f06bae..dd8d783d 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ data_dir = "/app/data" # Don't change if you run podsync via docker # Tokens from `Access tokens` section [tokens] -youtube = "YOUTUBE_API_TOKEN" # YouTube API Key. See https://developers.google.com/youtube/registering_an_application +youtube = ["YOUTUBE_API_TOKEN"] # YouTube API Key. See https://developers.google.com/youtube/registering_an_application vimeo = [ # Multiple keys will be rotated. "VIMEO_API_KEY_1", # Vimeo developer keys. See https://developer.vimeo.com/api/guides/start#generate-access-token "VIMEO_API_KEY_2" diff --git a/go.mod b/go.mod index c4a89e3d..b9f9de48 100644 --- a/go.mod +++ b/go.mod @@ -8,9 +8,7 @@ require ( github.com/golang/mock v1.6.0 github.com/hashicorp/go-multierror v1.1.1 github.com/jessevdk/go-flags v1.5.0 - github.com/kylelemons/godebug v1.1.0 // indirect - github.com/naoina/go-stringutil v0.1.0 // indirect - github.com/naoina/toml v0.1.1 + github.com/pelletier/go-toml v1.9.4 github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 github.com/silentsokolov/go-vimeo v0.0.0-20190116124215-06829264260c diff --git a/go.sum b/go.sum index 9a5b3efa..05896858 100644 --- a/go.sum +++ b/go.sum @@ -48,16 +48,12 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= -github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks= -github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0= -github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8= -github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= +github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/pkg/config/config.go b/pkg/config/config.go index dcb01971..cd1a4a40 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,9 +5,10 @@ import ( "io/ioutil" "path/filepath" "regexp" + "time" "github.com/hashicorp/go-multierror" - "github.com/naoina/toml" + "github.com/pelletier/go-toml" "github.com/pkg/errors" "github.com/mxpv/podsync/pkg/model" @@ -25,7 +26,7 @@ type Feed struct { // Format is "300ms", "1.5h" or "2h45m". // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". // NOTE: too often update check might drain your API token. - UpdatePeriod Duration `toml:"update_period"` + UpdatePeriod time.Duration `toml:"update_period"` // Cron expression format is how often to check update // NOTE: too often update check might drain your API token. CronSchedule string `toml:"cron_schedule"` @@ -137,7 +138,7 @@ type Config struct { // ID will be used as feed ID in http://podsync.net/{FEED_ID}.xml Feeds map[string]*Feed // Tokens is API keys to use to access YouTube/Vimeo APIs. - Tokens map[model.Provider]StringSlice `toml:"tokens"` + Tokens map[model.Provider][]string `toml:"tokens"` // Downloader (youtube-dl) configuration Downloader Downloader `toml:"downloader"` } @@ -220,8 +221,8 @@ func (c *Config) applyDefaults(configPath string) { } for _, feed := range c.Feeds { - if feed.UpdatePeriod.Duration == 0 { - feed.UpdatePeriod.Duration = model.DefaultUpdatePeriod + if feed.UpdatePeriod == 0 { + feed.UpdatePeriod = model.DefaultUpdatePeriod } if feed.Quality == "" { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index cc79a3e3..2b3e1c7f 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -15,11 +15,8 @@ import ( func TestLoadConfig(t *testing.T) { const file = ` [tokens] -youtube = "123" -vimeo = [ - "321", - "456" -] +youtube = ["123"] +vimeo = ["321", "456"] [server] port = 80 @@ -76,7 +73,7 @@ timeout = 15 assert.True(t, ok) assert.Equal(t, "https://youtube.com/watch?v=ygIUF678y40", feed.URL) assert.EqualValues(t, 48, feed.PageSize) - assert.EqualValues(t, Duration{5 * time.Hour}, feed.UpdatePeriod) + assert.EqualValues(t, 5*time.Hour, feed.UpdatePeriod) assert.EqualValues(t, "audio", feed.Format) assert.EqualValues(t, "low", feed.Quality) assert.EqualValues(t, "regex for title here", feed.Filters.Title) @@ -142,7 +139,7 @@ data_dir = "/data" feed, ok := config.Feeds["A"] require.True(t, ok) - assert.EqualValues(t, feed.UpdatePeriod, Duration{model.DefaultUpdatePeriod}) + assert.EqualValues(t, feed.UpdatePeriod, model.DefaultUpdatePeriod) assert.EqualValues(t, feed.PageSize, 50) assert.EqualValues(t, feed.Quality, "high") assert.EqualValues(t, feed.Custom.CoverArtQuality, "high") diff --git a/pkg/config/toml.go b/pkg/config/toml.go deleted file mode 100644 index bdcf7717..00000000 --- a/pkg/config/toml.go +++ /dev/null @@ -1,41 +0,0 @@ -package config - -import ( - "time" - - "github.com/pkg/errors" -) - -type Duration struct { - time.Duration -} - -func (d *Duration) UnmarshalText(text []byte) error { - res, err := time.ParseDuration(string(text)) - if err != nil { - return err - } - - *d = Duration{res} - return nil -} - -// StringSlice is a toml extension that lets you to specify either a string -// value (a slice with just one element) or a string slice. -type StringSlice []string - -func (s *StringSlice) UnmarshalTOML(decode func(interface{}) error) error { - var single string - if err := decode(&single); err == nil { - *s = []string{single} - return nil - } - - var slice []string - if err := decode(&slice); err == nil { - *s = slice - return nil - } - - return errors.New("failed to decode string (slice) field") -}