11package config
22
33import (
4- "fmt "
5- "os "
6-
4+ "github.com/knadh/koanf "
5+ "github.com/knadh/koanf/parsers/yaml "
6+ "github.com/knadh/koanf/providers/file"
77 "github.com/l3uddz/crop/logger"
88 "github.com/l3uddz/crop/stringutils"
9-
10- "github.com/pkg/errors"
11- "github.com/spf13/viper"
129)
1310
14- // BuildVars build details
15- type BuildVars struct {
16- // Version
17- Version string
18- // Timestamp
19- Timestamp string
20- // Git commit
21- GitCommit string
22- }
23-
2411type Configuration struct {
2512 Rclone RcloneConfig
2613 Uploader []UploaderConfig
3623 Config * Configuration
3724
3825 // Internal
39- log = logger .GetLogger ("cfg" )
40- newOptionLen = 0
26+ delimiter = "."
27+ k = koanf .New (delimiter )
28+
29+ log = logger .GetLogger ("cfg" )
4130)
4231
4332/* Public */
@@ -46,46 +35,14 @@ func Init(configFilePath string) error {
4635 // set package variables
4736 cfgPath = configFilePath
4837
49- /* Initialize Configuration */
50- viper .SetConfigType ("yaml" )
51- viper .SetConfigFile (configFilePath )
52-
53- // read matching env vars
54- viper .AutomaticEnv ()
55-
56- // Load config
57- if err := viper .ReadInConfig (); err != nil {
58- if _ , ok := err .(viper.ConfigFileNotFoundError ); ok || os .IsNotExist (err ) {
59- // set the default config to be written
60- if err := setConfigDefaults (false ); err != nil {
61- log .WithError (err ).Error ("Failed to add config defaults" )
62- return errors .Wrap (err , "failed adding config defaults" )
63- }
64-
65- // write default config
66- if err := viper .WriteConfig (); err != nil {
67- log .WithError (err ).Fatalf ("Failed dumping default configuration to %q" , configFilePath )
68- }
69-
70- log .Infof ("Dumped default configuration to %q. Please edit before running again!" ,
71- viper .ConfigFileUsed ())
72- log .Logger .Exit (0 )
73- }
74-
75- log .WithError (err ).Error ("Configuration read error" )
76- return errors .Wrap (err , "failed reading config" )
38+ // load config file
39+ if err := k .Load (file .Provider (configFilePath ), yaml .Parser ()); err != nil {
40+ return err
7741 }
7842
79- // Set defaults (checking whether new options were added)
80- if err := setConfigDefaults (true ); err != nil {
81- log .WithError (err ).Error ("Failed to add new config defaults" )
82- return errors .Wrap (err , "failed adding new config defaults" )
83- }
84-
85- // Unmarshal into Config struct
86- if err := viper .Unmarshal (& Config ); err != nil {
87- log .WithError (err ).Error ("Configuration decode error" )
88- return errors .Wrap (err , "failed decoding config" )
43+ // unmarshal into struct
44+ if err := k .Unmarshal ("" , & Config ); err != nil {
45+ return err
8946 }
9047
9148 return nil
@@ -94,47 +51,3 @@ func Init(configFilePath string) error {
9451func ShowUsing () {
9552 log .Infof ("Using %s = %q" , stringutils .LeftJust ("CONFIG" , " " , 10 ), cfgPath )
9653}
97-
98- /* Private */
99-
100- func setConfigDefault (key string , value interface {}, check bool ) int {
101- if check {
102- if viper .IsSet (key ) {
103- return 0
104- }
105-
106- // determine padding to use for new key
107- if keyLen := len (key ); (keyLen + 2 ) > newOptionLen {
108- newOptionLen = keyLen + 2
109- }
110-
111- log .Warnf ("New config option: %s = %v" , stringutils .LeftJust (fmt .Sprintf ("%q" , key ),
112- " " , newOptionLen ), value )
113- }
114-
115- viper .SetDefault (key , value )
116-
117- return 1
118- }
119-
120- func setConfigDefaults (check bool ) error {
121- added := 0
122-
123- // rclone settings
124- added += setConfigDefault ("rclone.path" , "/usr/bin/rclone" , check )
125- added += setConfigDefault ("rclone.config" , "/Users/l3uddz/.config/rclone/rclone.conf" , check )
126- added += setConfigDefault ("rclone.live_rotate" , false , check )
127-
128- // were new settings added?
129- if check && added > 0 {
130- if err := viper .WriteConfig (); err != nil {
131- log .WithError (err ).Error ("Failed saving configuration with new options..." )
132- return errors .Wrap (err , "failed saving updated configuration" )
133- }
134-
135- log .Info ("Configuration was saved with new options!" )
136- log .Logger .Exit (0 )
137- }
138-
139- return nil
140- }
0 commit comments