-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
58 lines (46 loc) · 1.26 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"os"
"gopkg.in/yaml.v2"
)
type OAuthConfig struct {
Port string `yaml:"port"`
RedirectURI string `yaml:"redirect_uri"`
}
type SiteConfig struct {
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
AuthURL string `yaml:"auth_url"`
TokenURL string `yaml:"token_url"`
Username string `yaml:"username"`
}
type Config struct {
OAuth OAuthConfig `yaml:"oauth"`
Anilist SiteConfig `yaml:"anilist"`
MyAnimeList SiteConfig `yaml:"myanimelist"`
TokenFilePath string `yaml:"token_file_path"`
}
func loadConfigFromFile(filename string) (Config, error) {
data, err := os.ReadFile(filename)
if err != nil {
return Config{}, err
}
var cfg Config
err = yaml.Unmarshal(data, &cfg)
if err != nil {
return Config{}, err
}
if port := os.Getenv("PORT"); port != "" {
cfg.OAuth.Port = port
}
if clientSecret := os.Getenv("CLIENT_SECRET_ANILIST"); clientSecret != "" {
cfg.Anilist.ClientSecret = clientSecret
}
if clientSecret := os.Getenv("CLIENT_SECRET_MYANIMELIST"); clientSecret != "" {
cfg.MyAnimeList.ClientSecret = clientSecret
}
if cfg.TokenFilePath == "" {
cfg.TokenFilePath = os.ExpandEnv("$HOME/.config/anilist-mal-sync/token.json")
}
return cfg, nil
}