This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9b24d0
commit c7ae9fe
Showing
1 changed file
with
81 additions
and
0 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 |
---|---|---|
@@ -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 | ||
} |