Skip to content

Commit

Permalink
Added the functionality around write add and update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adron committed Oct 3, 2019
1 parent c164c52 commit aab13b7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 54 deletions.
1 change: 1 addition & 0 deletions .cobra-cli-samples.yml
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
41 changes: 4 additions & 37 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ limitations under the License.
package cmd

import (
"fmt"
"github.com/Adron/cobra-cli-samples/helper"
"github.com/Adron/cobra-cli-samples/configMgmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// addCmd represents the add command
Expand All @@ -33,43 +31,12 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
var theKey string
theKey, _ = cmd.Flags().GetString("key")
var theValue string
theValue, _ = cmd.Flags().GetString("value")

if len(theKey) == 0 || len(theValue) == 0 {
fmt.Println("The key and value must both contain contents to write to the configuration file.")
return
}

// Determine if an existing key, if so notify.
if findExistingKey(theKey) {
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
}

writeKeyValuePair(theKey, theValue)
key, _ := cmd.Flags().GetString("key")
value, _ := cmd.Flags().GetString("value")
configMgmt.ConfigKeyValuePairAdd(key, value)
},
}

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
}

func init() {
configCmd.AddCommand(addCmd)
}
25 changes: 8 additions & 17 deletions cmd/create.go → cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ limitations under the License.
package cmd

import (
"fmt"

"github.com/Adron/cobra-cli-samples/configMgmt"
"github.com/spf13/cobra"
)

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Expand All @@ -32,20 +31,12 @@ Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("create called")
key, _ := cmd.Flags().GetString("key")
value, _ := cmd.Flags().GetString("value")
configMgmt.ConfigKeyValuePairUpdate(key, value)
},
}

func init() {
configCmd.AddCommand(createCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
configCmd.AddCommand(updateCmd)
}
50 changes: 50 additions & 0 deletions configMgmt/configMgmt.go
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
}

0 comments on commit aab13b7

Please sign in to comment.