Skip to content

Commit

Permalink
change(config): switch viper to koanf
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz committed Jun 15, 2020
1 parent 56ae791 commit 1fd3ed6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 295 deletions.
113 changes: 13 additions & 100 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package config

import (
"fmt"
"os"

"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/l3uddz/crop/logger"
"github.com/l3uddz/crop/stringutils"

"github.com/pkg/errors"
"github.com/spf13/viper"
)

// BuildVars build details
type BuildVars struct {
// Version
Version string
// Timestamp
Timestamp string
// Git commit
GitCommit string
}

type Configuration struct {
Rclone RcloneConfig
Uploader []UploaderConfig
Expand All @@ -36,8 +23,10 @@ var (
Config *Configuration

// Internal
log = logger.GetLogger("cfg")
newOptionLen = 0
delimiter = "."
k = koanf.New(delimiter)

log = logger.GetLogger("cfg")
)

/* Public */
Expand All @@ -46,46 +35,14 @@ func Init(configFilePath string) error {
// set package variables
cfgPath = configFilePath

/* Initialize Configuration */
viper.SetConfigType("yaml")
viper.SetConfigFile(configFilePath)

// read matching env vars
viper.AutomaticEnv()

// Load config
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok || os.IsNotExist(err) {
// set the default config to be written
if err := setConfigDefaults(false); err != nil {
log.WithError(err).Error("Failed to add config defaults")
return errors.Wrap(err, "failed adding config defaults")
}

// write default config
if err := viper.WriteConfig(); err != nil {
log.WithError(err).Fatalf("Failed dumping default configuration to %q", configFilePath)
}

log.Infof("Dumped default configuration to %q. Please edit before running again!",
viper.ConfigFileUsed())
log.Logger.Exit(0)
}

log.WithError(err).Error("Configuration read error")
return errors.Wrap(err, "failed reading config")
// load config file
if err := k.Load(file.Provider(configFilePath), yaml.Parser()); err != nil {
return err
}

// Set defaults (checking whether new options were added)
if err := setConfigDefaults(true); err != nil {
log.WithError(err).Error("Failed to add new config defaults")
return errors.Wrap(err, "failed adding new config defaults")
}

// Unmarshal into Config struct
if err := viper.Unmarshal(&Config); err != nil {
log.WithError(err).Error("Configuration decode error")
return errors.Wrap(err, "failed decoding config")
// unmarshal into struct
if err := k.Unmarshal("", &Config); err != nil {
return err
}

return nil
Expand All @@ -94,47 +51,3 @@ func Init(configFilePath string) error {
func ShowUsing() {
log.Infof("Using %s = %q", stringutils.LeftJust("CONFIG", " ", 10), cfgPath)
}

/* Private */

func setConfigDefault(key string, value interface{}, check bool) int {
if check {
if viper.IsSet(key) {
return 0
}

// determine padding to use for new key
if keyLen := len(key); (keyLen + 2) > newOptionLen {
newOptionLen = keyLen + 2
}

log.Warnf("New config option: %s = %v", stringutils.LeftJust(fmt.Sprintf("%q", key),
" ", newOptionLen), value)
}

viper.SetDefault(key, value)

return 1
}

func setConfigDefaults(check bool) error {
added := 0

// rclone settings
added += setConfigDefault("rclone.path", "/usr/bin/rclone", check)
added += setConfigDefault("rclone.config", "/Users/l3uddz/.config/rclone/rclone.conf", check)
added += setConfigDefault("rclone.live_rotate", false, check)

// were new settings added?
if check && added > 0 {
if err := viper.WriteConfig(); err != nil {
log.WithError(err).Error("Failed saving configuration with new options...")
return errors.Wrap(err, "failed saving updated configuration")
}

log.Info("Configuration was saved with new options!")
log.Logger.Exit(0)
}

return nil
}
16 changes: 8 additions & 8 deletions config/rclone.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package config

type RcloneConfig struct {
Path string
Config string
Stats string
LiveRotate bool `mapstructure:"live_rotate"`
DryRun bool `mapstructure:"dry_run"`
ServiceAccountRemotes map[string][]string `mapstructure:"service_account_remotes"`
GlobalParams map[string]RcloneParams `mapstructure:"global_params"`
Path string `koanf:"path"`
Config string `koanf:"config"`
Stats string `koanf:"stats"`
LiveRotate bool `koanf:"live_rotate"`
DryRun bool `koanf:"dry_run"`
ServiceAccountRemotes map[string][]string `koanf:"service_account_remotes"`
GlobalParams map[string]RcloneParams `koanf:"global_params"`
}

type RcloneServerSide struct {
Expand All @@ -18,7 +18,7 @@ type RcloneServerSide struct {
type RcloneParams struct {
Copy []string
Move []string
MoveServerSide []string `mapstructure:"move_server_side"`
MoveServerSide []string `koanf:"move_server_side"`
Sync []string
Dedupe []string
}
16 changes: 8 additions & 8 deletions config/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package config
type SyncerRemotes struct {
Copy []string
Sync []string
MoveServerSide []RcloneServerSide `mapstructure:"move_server_side"`
MoveServerSide []RcloneServerSide `koanf:"move_server_side"`
Dedupe []string
}

type SyncerRcloneParams struct {
Copy []string
GlobalCopy string `mapstructure:"global_copy"`
GlobalCopy string `koanf:"global_copy"`
Sync []string
GlobalSync string `mapstructure:"global_sync"`
MoveServerSide []string `mapstructure:"move_server_side"`
GlobalMoveServerSide string `mapstructure:"global_move_server_side"`
GlobalSync string `koanf:"global_sync"`
MoveServerSide []string `koanf:"move_server_side"`
GlobalMoveServerSide string `koanf:"global_move_server_side"`
Dedupe []string
GlobalDedupe string `mapstructure:"global_dedupe"`
GlobalDedupe string `koanf:"global_dedupe"`
}

type SyncerConfig struct {
Name string
Enabled bool
SourceRemote string `mapstructure:"source_remote"`
SourceRemote string `koanf:"source_remote"`
Remotes SyncerRemotes
RcloneParams SyncerRcloneParams `mapstructure:"rclone_params"`
RcloneParams SyncerRcloneParams `koanf:"rclone_params"`
}
16 changes: 8 additions & 8 deletions config/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ type UploaderRemotes struct {
Clean []string
Copy []string
Move string
MoveServerSide []RcloneServerSide `mapstructure:"move_server_side"`
MoveServerSide []RcloneServerSide `koanf:"move_server_side"`
Dedupe []string
}

type UploaderRcloneParams struct {
Copy []string
GlobalCopy string `mapstructure:"global_copy"`
GlobalCopy string `koanf:"global_copy"`
Move []string
GlobalMove string `mapstructure:"global_move"`
MoveServerSide []string `mapstructure:"move_server_side"`
GlobalMoveServerSide string `mapstructure:"global_move_server_side"`
GlobalMove string `koanf:"global_move"`
MoveServerSide []string `koanf:"move_server_side"`
GlobalMoveServerSide string `koanf:"global_move_server_side"`
Dedupe []string
GlobalDedupe string `mapstructure:"global_dedupe"`
GlobalDedupe string `koanf:"global_dedupe"`
}

type UploaderConfig struct {
Name string
Enabled bool
Check UploaderCheck
Hidden UploaderHidden
LocalFolder string `mapstructure:"local_folder"`
LocalFolder string `koanf:"local_folder"`
Remotes UploaderRemotes
RcloneParams UploaderRcloneParams `mapstructure:"rclone_params"`
RcloneParams UploaderRcloneParams `koanf:"rclone_params"`
}
10 changes: 2 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/dgraph-io/badger/v2 v2.0.3 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-cmd/cmd v1.2.0
github.com/gofiber/fiber v1.11.1
github.com/gofiber/recover v0.1.0
github.com/golang/protobuf v1.4.2 // indirect
github.com/klauspost/compress v1.10.8 // indirect
github.com/knadh/koanf v0.10.0
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/mapstructure v1.3.2 // indirect
Expand All @@ -27,22 +27,16 @@ require (
github.com/rhysd/go-github-selfupdate v1.2.2
github.com/sirupsen/logrus v1.6.0
github.com/sony/sonyflake v1.0.0 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.0
github.com/ulikunitz/xz v0.5.7 // indirect
github.com/x-cray/logrus-prefixed-formatter v0.5.2
github.com/yale8848/gorpool v0.1.0
github.com/zippoxer/bow v0.0.0-20200229231453-bf1012ae7ab9
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 // indirect
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20200610111108-226ff32320da
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/ini.v1 v1.57.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
)
Loading

0 comments on commit 1fd3ed6

Please sign in to comment.