Skip to content
This repository has been archived by the owner on Nov 9, 2019. It is now read-only.

backends: Use defaults even if config option was provided but empty #191

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions backends/file/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions backends/s3/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down