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..ce3952b 100644 --- a/config/config.go +++ b/config/config.go @@ -49,12 +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 - 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 + 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 { if err := viper.Unmarshal(&config); err != nil { return fmt.Errorf("failed to unmarshal into config: %v", err) }