mctl/cmd/clear.go
jake 57fc5e0807
All checks were successful
code scans / scans (push) Successful in 1m28s
code scans / scans (pull_request) Successful in 1m30s
long description update
2025-05-11 00:22:07 -04:00

44 lines
887 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: `Clears all configuration values for mctl`,
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)
}