From de47db0673e1326b0554d0e5f2aff42f8a97309e Mon Sep 17 00:00:00 2001 From: Leon Klingele Date: Sat, 1 Dec 2018 16:51:49 +0100 Subject: [PATCH 1/2] backends: Use defaults even if config option was provided but empty This is a workaround for #190 --- backends/file/new.go | 9 +++++---- backends/s3/new.go | 10 ++++++---- config/config.go | 5 +---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/backends/file/new.go b/backends/file/new.go index 37e9b63..3748748 100644 --- a/backends/file/new.go +++ b/backends/file/new.go @@ -22,14 +22,15 @@ func _new(config *backends.Config) (backends.Client, error) { return nil, err } - safePath, ok := config.Settings["path"].(string) - if ok { - safePath = formatHomeDir(safePath, homeDir) - } else { + safePath, _ := config.Settings["path"].(string) + // TODO: Should only check the `ok` variable! Current implementation is a workaround for https://github.com/spf13/viper/issues/472 + if safePath == "" { safePath, err = defaultSafePath(homeDir) if err != nil { return nil, err } + } else { + safePath = formatHomeDir(safePath, homeDir) } // Adding support for paths relative to the default pick dir diff --git a/backends/s3/new.go b/backends/s3/new.go index 8306cea..66adfef 100644 --- a/backends/s3/new.go +++ b/backends/s3/new.go @@ -22,13 +22,15 @@ const ( // key: AWS S3 Key name for storing the safe. Defaults to `defaultS3Key` func _new(config *backends.Config) (backends.Client, error) { // AWS S3 Bucket overrides - bucket, ok := config.Settings["bucket"].(string) - if !ok { + bucket, _ := config.Settings["bucket"].(string) + // TODO: Should only check the `ok` variable! Current implementation is a workaround for https://github.com/spf13/viper/issues/472 + if bucket == "" { bucket = defaultS3Bucket } - key, ok := config.Settings["key"].(string) - if !ok { + key, _ := config.Settings["key"].(string) + // TODO: Should only check the `ok` variable! Current implementation is a workaround for https://github.com/spf13/viper/issues/472 + if key == "" { key = defaultS3Key } key = path.TrimModPrefix(key) diff --git a/config/config.go b/config/config.go index 3cb1671..c82dc16 100644 --- a/config/config.go +++ b/config/config.go @@ -51,10 +51,7 @@ func Load(rootCmd *cobra.Command, version string) (*Config, error) { // Ugly, I know. See https://github.com/spf13/viper/issues/472 rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { - if err := viper.ReadInConfig(); err != nil { - // Return `nil` to avoid printing an unneccesary error message - return nil - } + _ = viper.ReadInConfig() if err := viper.Unmarshal(&config); err != nil { return fmt.Errorf("failed to unmarshal into config: %v", err) } From 7e43a48532b76bc43eb780245cde4e72c7695a8e Mon Sep 17 00:00:00 2001 From: Leon Klingele Date: Sat, 1 Dec 2018 17:44:12 +0100 Subject: [PATCH 2/2] config: Output error message when processing config file fails --- config/config.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index c82dc16..ce3952b 100644 --- a/config/config.go +++ b/config/config.go @@ -49,9 +49,13 @@ func Load(rootCmd *cobra.Command, version string) (*Config, error) { viper.BindPFlag("storage.settings.path", rootCmd.PersistentFlags().Lookup("safe")) // file backend viper.BindPFlag("storage.settings.key", rootCmd.PersistentFlags().Lookup("safe")) // s3 backend - // Ugly, I know. See https://github.com/spf13/viper/issues/472 + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); !ok { + return nil, fmt.Errorf("failed to read config file: %v", err) + } + } + // TODO: https://github.com/spf13/viper/issues/472 rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { - _ = viper.ReadInConfig() if err := viper.Unmarshal(&config); err != nil { return fmt.Errorf("failed to unmarshal into config: %v", err) }