Skip to content

Commit

Permalink
Allow specify list of API keys in TOML
Browse files Browse the repository at this point in the history
  • Loading branch information
mxpv committed Apr 21, 2020
1 parent 82bbd02 commit 2623a30
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ type Custom struct {
type Tokens struct {
// YouTube API key.
// See https://developers.google.com/youtube/registering_an_application
YouTube string `toml:"youtube"`
YouTube StringSlice `toml:"youtube"`
// Vimeo developer key.
// See https://developer.vimeo.com/api/guides/start#generate-access-token
Vimeo string `toml:"vimeo"`
Vimeo StringSlice `toml:"vimeo"`
}

type Server struct {
Expand Down
34 changes: 31 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ func TestLoadConfig(t *testing.T) {
const file = `
[tokens]
youtube = "123"
vimeo = "321"
vimeo = [
"321",
"456"
]
[server]
port = 80
Expand Down Expand Up @@ -51,8 +54,11 @@ self_update = true

assert.Equal(t, "/home/user/db/", config.Database.Dir)

assert.Equal(t, "123", config.Tokens.YouTube)
assert.Equal(t, "321", config.Tokens.Vimeo)
require.Len(t, config.Tokens.YouTube, 1)
assert.Equal(t, "123", config.Tokens.YouTube[0])
require.Len(t, config.Tokens.Vimeo, 2)
assert.Equal(t, "321", config.Tokens.Vimeo[0])
assert.Equal(t, "456", config.Tokens.Vimeo[1])

assert.Len(t, config.Feeds, 1)
feed, ok := config.Feeds["XYZ"]
Expand All @@ -75,6 +81,28 @@ self_update = true
assert.True(t, config.Downloader.SelfUpdate)
}

func TestLoadEmptyKeyList(t *testing.T) {
const file = `
[tokens]
vimeo = []
[server]
data_dir = "/data"
[feeds]
[feeds.A]
url = "https://youtube.com/watch?v=ygIUF678y40"
`
path := setup(t, file)
defer os.Remove(path)

config, err := LoadConfig(path)
assert.NoError(t, err)
require.NotNil(t, config)

require.Len(t, config.Tokens.YouTube, 0)
require.Len(t, config.Tokens.Vimeo, 0)
}

func TestApplyDefaults(t *testing.T) {
const file = `
[server]
Expand Down
22 changes: 22 additions & 0 deletions pkg/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"time"

"github.com/pkg/errors"
)

type Duration struct {
Expand All @@ -17,3 +19,23 @@ func (d *Duration) UnmarshalText(text []byte) error {
*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")
}
4 changes: 2 additions & 2 deletions pkg/feed/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func New(ctx context.Context, cfg *config.Feed, tokens config.Tokens) (Builder,

switch info.Provider {
case model.ProviderYoutube:
provider, err = NewYouTubeBuilder(tokens.YouTube)
provider, err = NewYouTubeBuilder(tokens.YouTube[0])
case model.ProviderVimeo:
provider, err = NewVimeoBuilder(ctx, tokens.Vimeo)
provider, err = NewVimeoBuilder(ctx, tokens.Vimeo[0])
default:
return nil, errors.Errorf("unsupported provider %q", info.Provider)
}
Expand Down

0 comments on commit 2623a30

Please sign in to comment.