added clear command
All checks were successful
code scans / scans (push) Successful in 1m19s

- clear command setup
- clear command added to docs
- config fields saved in model layer
- config command code cleanup
This commit is contained in:
2025-05-10 15:22:33 -04:00
parent 386a766185
commit 83fec73076
4 changed files with 65 additions and 3 deletions

44
cmd/clear.go Normal file
View File

@@ -0,0 +1,44 @@
/*
Copyright © 2025 Jake jake.young.dev@gmail.com
*/
package cmd
import (
"fmt"
"os"
"code.jakeyoungdev.com/jake/mctl/models"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// clearCmd represents the clear command
var clearCmd = &cobra.Command{
Use: "clear",
Short: "Clear config file",
Long: `Removes the configuration file from the system. The
config command must be run before use after clearing.`,
Run: func(cmd *cobra.Command, args []string) {
home, err := os.UserHomeDir()
cobra.CheckErr(err)
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".mctl")
err = viper.ReadInConfig()
if err == nil {
//clear values if file exists
for _, v := range models.ConfigFields {
viper.Set(v, "")
}
err := viper.WriteConfig()
cobra.CheckErr(err)
fmt.Println("Config file cleared, use 'config' command to re-populate it")
}
},
}
func init() {
rootCmd.AddCommand(clearCmd)
}

View File

@@ -76,9 +76,8 @@ func initConfig() {
viper.SetConfigName(".mctl")
viper.AutomaticEnv()
err = viper.ReadInConfig()
cobra.CheckErr(err)
if err := viper.ReadInConfig(); err != nil {
if err != nil {
//file does not exist, create it
viper.Set("server", cfgserver)
viper.Set("password", "")