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

Commit

Permalink
add config set cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
martinnirtl committed Feb 7, 2020
1 parent e9b24d0 commit c7ae9fe
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions internal/commands/configcmd/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package configcmd

import (
"fmt"
"strings"

"github.com/martinnirtl/dockma/internal/config"
"github.com/martinnirtl/dockma/internal/survey"
"github.com/martinnirtl/dockma/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var setCmd = &cobra.Command{
Use: "set",
Short: "Set dockma config vars in an interactive walkthrough.",
Long: `-`,
Example: "dockma config set",
Run: func(cmd *cobra.Command, args []string) {
options := []string{
fmt.Sprintf("hidesubcommandoutput: %t", viper.GetBool("hidesubcommandoutput")),
fmt.Sprintf("logfile: %s", viper.GetString("logfile")),
fmt.Sprintf("username: %s", viper.GetString("username")),
}

selected, err := survey.MultiSelect("Select config items to change", options, nil)

if err != nil {
utils.Abort()
}

for _, varnameRaw := range selected {
varname := strings.Split(varnameRaw, ":")

setConfigVar(varname[0])
}
},
}

func init() {
ConfigCommand.AddCommand(setCmd)
}

func setConfigVar(varname string) error {
switch varname {
case "hidesubcommandoutput":
hide, err := survey.Confirm("Hide output of external commands [true: show output only on error, false: always pipe output]", viper.GetBool("hidesubcommandoutput"))

if err != nil {
utils.Abort()
}

viper.Set("hidesubcommandoutput", hide)

case "logfile":
logfile, err := survey.Input("Enter name of logfile [stored in dockma home dir]", viper.GetString("logfile"))

if err != nil {
utils.Abort()
}

viper.Set("logfile", logfile)

case "username":
username, err := survey.Input("Enter new username", viper.GetString("username"))

if err != nil {
utils.Abort()
}

viper.Set("username", username)
}

err := config.Save()

if err != nil {
utils.Error(err)
}

return nil
}

0 comments on commit c7ae9fe

Please sign in to comment.