(isPromptEnabled bool, ask question.Asker, key string, value string)
| 36 | } |
| 37 | |
| 38 | func setRun(isPromptEnabled bool, ask question.Asker, key string, value string) error { |
| 39 | // have to make new viper so it only contains file value, no ENVs or Flags |
| 40 | configPath, err := config.EnsureConfigPath() |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | |
| 45 | localViper := viper.New() |
| 46 | config.SetupConfigFile(localViper, configPath) |
| 47 | |
| 48 | if err := localViper.ReadInConfig(); err != nil { |
| 49 | if _, ok := err.(viper.ConfigFileNotFoundError); ok { |
| 50 | // config file not found, we create it here and recover |
| 51 | if err = localViper.SafeWriteConfig(); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | } else { |
| 55 | return err // any other error is unrecoverable; abort |
| 56 | } |
| 57 | } |
| 58 | if key != "" && !config.IsValidKey(key) { |
| 59 | return fmt.Errorf("the key '%s' is not a valid", key) |
| 60 | } |
| 61 | if isPromptEnabled && value == "" { |
| 62 | k, v, err := promptMissing(ask, key) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | value = v |
| 67 | key = k |
| 68 | } |
| 69 | key = strings.ToLower(key) |
| 70 | if key == strings.ToLower(constants.ConfigNoPrompt) { |
| 71 | boolValue, err := strconv.ParseBool(value) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("the provided value %s is not valid for NoPrompt, please use true of false", value) |
| 74 | } |
| 75 | localViper.Set(key, boolValue) |
| 76 | } else { |
| 77 | localViper.Set(key, value) |
| 78 | } |
| 79 | if err := localViper.WriteConfig(); err != nil { |
| 80 | return err |
| 81 | } |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func promptMissing(ask question.Asker, key string) (string, string, error) { |
| 86 | keys := []string{ |
no test coverage detected