mctl/cmd/clear.go
jake 83fec73076
All checks were successful
code scans / scans (push) Successful in 1m19s
added clear command
- clear command setup
- clear command added to docs
- config fields saved in model layer
- config command code cleanup
2025-05-10 15:22:33 -04:00

45 lines
955 B
Go

/*
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)
}