Skip to content

Commit 1fd3ed6

Browse files
committed
change(config): switch viper to koanf
1 parent 56ae791 commit 1fd3ed6

File tree

6 files changed

+49
-295
lines changed

6 files changed

+49
-295
lines changed

config/config.go

Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
package config
22

33
import (
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-
2411
type Configuration struct {
2512
Rclone RcloneConfig
2613
Uploader []UploaderConfig
@@ -36,8 +23,10 @@ var (
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 {
9451
func 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-
}

config/rclone.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package config
22

33
type RcloneConfig struct {
4-
Path string
5-
Config string
6-
Stats string
7-
LiveRotate bool `mapstructure:"live_rotate"`
8-
DryRun bool `mapstructure:"dry_run"`
9-
ServiceAccountRemotes map[string][]string `mapstructure:"service_account_remotes"`
10-
GlobalParams map[string]RcloneParams `mapstructure:"global_params"`
4+
Path string `koanf:"path"`
5+
Config string `koanf:"config"`
6+
Stats string `koanf:"stats"`
7+
LiveRotate bool `koanf:"live_rotate"`
8+
DryRun bool `koanf:"dry_run"`
9+
ServiceAccountRemotes map[string][]string `koanf:"service_account_remotes"`
10+
GlobalParams map[string]RcloneParams `koanf:"global_params"`
1111
}
1212

1313
type RcloneServerSide struct {
@@ -18,7 +18,7 @@ type RcloneServerSide struct {
1818
type RcloneParams struct {
1919
Copy []string
2020
Move []string
21-
MoveServerSide []string `mapstructure:"move_server_side"`
21+
MoveServerSide []string `koanf:"move_server_side"`
2222
Sync []string
2323
Dedupe []string
2424
}

config/syncer.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ package config
33
type SyncerRemotes struct {
44
Copy []string
55
Sync []string
6-
MoveServerSide []RcloneServerSide `mapstructure:"move_server_side"`
6+
MoveServerSide []RcloneServerSide `koanf:"move_server_side"`
77
Dedupe []string
88
}
99

1010
type SyncerRcloneParams struct {
1111
Copy []string
12-
GlobalCopy string `mapstructure:"global_copy"`
12+
GlobalCopy string `koanf:"global_copy"`
1313
Sync []string
14-
GlobalSync string `mapstructure:"global_sync"`
15-
MoveServerSide []string `mapstructure:"move_server_side"`
16-
GlobalMoveServerSide string `mapstructure:"global_move_server_side"`
14+
GlobalSync string `koanf:"global_sync"`
15+
MoveServerSide []string `koanf:"move_server_side"`
16+
GlobalMoveServerSide string `koanf:"global_move_server_side"`
1717
Dedupe []string
18-
GlobalDedupe string `mapstructure:"global_dedupe"`
18+
GlobalDedupe string `koanf:"global_dedupe"`
1919
}
2020

2121
type SyncerConfig struct {
2222
Name string
2323
Enabled bool
24-
SourceRemote string `mapstructure:"source_remote"`
24+
SourceRemote string `koanf:"source_remote"`
2525
Remotes SyncerRemotes
26-
RcloneParams SyncerRcloneParams `mapstructure:"rclone_params"`
26+
RcloneParams SyncerRcloneParams `koanf:"rclone_params"`
2727
}

config/uploader.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@ type UploaderRemotes struct {
2020
Clean []string
2121
Copy []string
2222
Move string
23-
MoveServerSide []RcloneServerSide `mapstructure:"move_server_side"`
23+
MoveServerSide []RcloneServerSide `koanf:"move_server_side"`
2424
Dedupe []string
2525
}
2626

2727
type UploaderRcloneParams struct {
2828
Copy []string
29-
GlobalCopy string `mapstructure:"global_copy"`
29+
GlobalCopy string `koanf:"global_copy"`
3030
Move []string
31-
GlobalMove string `mapstructure:"global_move"`
32-
MoveServerSide []string `mapstructure:"move_server_side"`
33-
GlobalMoveServerSide string `mapstructure:"global_move_server_side"`
31+
GlobalMove string `koanf:"global_move"`
32+
MoveServerSide []string `koanf:"move_server_side"`
33+
GlobalMoveServerSide string `koanf:"global_move_server_side"`
3434
Dedupe []string
35-
GlobalDedupe string `mapstructure:"global_dedupe"`
35+
GlobalDedupe string `koanf:"global_dedupe"`
3636
}
3737

3838
type UploaderConfig struct {
3939
Name string
4040
Enabled bool
4141
Check UploaderCheck
4242
Hidden UploaderHidden
43-
LocalFolder string `mapstructure:"local_folder"`
43+
LocalFolder string `koanf:"local_folder"`
4444
Remotes UploaderRemotes
45-
RcloneParams UploaderRcloneParams `mapstructure:"rclone_params"`
45+
RcloneParams UploaderRcloneParams `koanf:"rclone_params"`
4646
}

go.mod

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ require (
88
github.com/blang/semver v3.5.1+incompatible
99
github.com/dgraph-io/badger/v2 v2.0.3 // indirect
1010
github.com/dustin/go-humanize v1.0.0
11-
github.com/fsnotify/fsnotify v1.4.9 // indirect
1211
github.com/go-cmd/cmd v1.2.0
1312
github.com/gofiber/fiber v1.11.1
1413
github.com/gofiber/recover v0.1.0
1514
github.com/golang/protobuf v1.4.2 // indirect
1615
github.com/klauspost/compress v1.10.8 // indirect
16+
github.com/knadh/koanf v0.10.0
1717
github.com/mattn/go-colorable v0.1.6 // indirect
1818
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
1919
github.com/mitchellh/mapstructure v1.3.2 // indirect
@@ -27,22 +27,16 @@ require (
2727
github.com/rhysd/go-github-selfupdate v1.2.2
2828
github.com/sirupsen/logrus v1.6.0
2929
github.com/sony/sonyflake v1.0.0 // indirect
30-
github.com/spf13/afero v1.2.2 // indirect
31-
github.com/spf13/cast v1.3.1 // indirect
3230
github.com/spf13/cobra v1.0.0
33-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
34-
github.com/spf13/pflag v1.0.5 // indirect
35-
github.com/spf13/viper v1.7.0
3631
github.com/ulikunitz/xz v0.5.7 // indirect
3732
github.com/x-cray/logrus-prefixed-formatter v0.5.2
3833
github.com/yale8848/gorpool v0.1.0
3934
github.com/zippoxer/bow v0.0.0-20200229231453-bf1012ae7ab9
4035
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 // indirect
4136
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
4237
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
43-
golang.org/x/sys v0.0.0-20200610111108-226ff32320da
38+
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1
4439
google.golang.org/appengine v1.6.6 // indirect
4540
google.golang.org/protobuf v1.24.0 // indirect
46-
gopkg.in/ini.v1 v1.57.0 // indirect
4741
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
4842
)

0 commit comments

Comments
 (0)