44 lines
926 B
Go
44 lines
926 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, all server configuration will be lost`,
|
||
|
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)
|
||
|
}
|