-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the functionality around write add and update.
- Loading branch information
Showing
4 changed files
with
63 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
dallakjoined: heydallakfollowmeathttps://twitch.tv/adronhall/ | ||
description: This is a sentence about some dealio or what not. | ||
name: the value | ||
something: 1/23/2019 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package configMgmt | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Adron/cobra-cli-samples/helper" | ||
"github.com/spf13/viper" | ||
"log" | ||
) | ||
|
||
func ConfigKeyValuePairUpdate(key string, value string) { | ||
writeKeyValuePair(key, value) | ||
} | ||
|
||
func ConfigKeyValuePairAdd(key string, value string) { | ||
if validateKeyValuePair(key, value) { | ||
log.Printf("Validation not met for %s.", key) | ||
} else { | ||
writeKeyValuePair(key, value) | ||
} | ||
} | ||
|
||
func validateKeyValuePair(key string, value string) bool { | ||
if len(key) == 0 || len(value) == 0 { | ||
fmt.Println("The key and value must both contain contents to write to the configuration file.") | ||
return true | ||
} | ||
// Determine if an existing key, if so notify. | ||
if findExistingKey(key) { | ||
fmt.Println("This key already exists. Create a key value pair with a different key, or if this is an update use the update command.") | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func writeKeyValuePair(key string, value string) { | ||
viper.Set(key, value) | ||
err := viper.WriteConfig() | ||
helper.Check(err) | ||
fmt.Printf("Wrote the %s pair.\n", key) | ||
} | ||
|
||
func findExistingKey(theKey string) bool { | ||
existingKey := false | ||
for i := 0; i < len(viper.AllKeys()); i++ { | ||
if viper.AllKeys()[i] == theKey { | ||
existingKey = true | ||
} | ||
} | ||
return existingKey | ||
} |