This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (88 loc) · 3.44 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Package config implements all configuration aspects of series-cleanup
package config
import (
"encoding/json"
"log"
"path"
"strings"
"github.com/knadh/koanf"
koanf_json "github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/bjw-s/series-cleanup/internal/helpers"
"github.com/bjw-s/series-cleanup/internal/mediafile"
"github.com/go-playground/validator/v10"
flag "github.com/spf13/pflag"
)
// Config exposes the collected configuration
var Config config
type sensitiveString string
func (s sensitiveString) String() string {
return "[REDACTED]"
}
func (s sensitiveString) MarshalJSON() ([]byte, error) {
return json.Marshal("[REDACTED]")
}
type folderOverride struct {
Folder string `mapstructure:"folder"`
Mapping mediafile.ShowMapping `mapstructure:"mapping"`
Skip bool `mapstructure:"skip"`
SkipSeasons []int `mapstructure:"skipSeasons"`
}
type traktConfig struct {
CacheFolder string `mapstructure:"cacheFolder"`
ClientID string `mapstructure:"clientId" validate:"required"`
ClientSecret sensitiveString `mapstructure:"clientSecret" validate:"required"`
User string `mapstructure:"user" validate:"required"`
}
type config struct {
DeleteAfterHours int `mapstructure:"deleteAfterHours"`
DryRun bool `mapstructure:"dryRun"`
FolderRegex string `mapstructure:"folderRegex"`
LogLevel string `mapstructure:"logLevel"`
Overrides []folderOverride `mapstructure:"overrides"`
ScanFolders []string `mapstructure:"scanFolders"`
Trakt traktConfig `mapstructure:"trakt"`
}
func init() {
var k = koanf.New(".")
// Use the POSIX compliant pflag lib instead of Go's flag lib.
var configFolder = flag.String("configFolder", "/config", "path to store the configuration")
flag.Parse()
// Check pre-requisites
if !helpers.FolderExists(*configFolder) {
log.Fatalf("Could not find configuration folder: %s", *configFolder)
}
// Load default values using the confmap provider.
// We provide a flat map with the "." delimiter.
k.Load(confmap.Provider(map[string]interface{}{
"dryRun": false,
"deleteAfterHours": 24,
"folderRegex": "(?P<Show>.*)",
"loglevel": "info",
"trakt.CacheFolder": configFolder,
}, "."), nil)
// Load provided JSON config
if err := k.Load(file.Provider(path.Join(*configFolder, "settings.json")), koanf_json.Parser()); err != nil {
log.Fatalf("Error loading file: %v", err)
}
// Load environment variables and merge into the loaded config.
k.Load(env.ProviderWithValue("SC_", ".", func(s string, v string) (string, interface{}) {
// Strip out the SC_ prefix and lowercase and get the key while also replacing
// the _ character with . in the key (koanf delimeter).
key := strings.Replace(strings.ToLower(strings.TrimPrefix(s, "SC_")), "_", ".", -1)
// If there is a space in the value, split the value into a slice by the space.
if strings.Contains(v, " ") {
return key, strings.Split(v, " ")
}
// Otherwise, return the plain string.
return key, v
}), nil)
k.Unmarshal("", &Config)
// Validate the rendered configuration
validate := validator.New()
if err := validate.Struct(&Config); err != nil {
log.Fatalf("Configuration validation failed: %v\n", err)
}
}