/* Copyright © 2025 Jake jake.young.dev@gmail.com */ package cmd import ( "bufio" "errors" "fmt" "os" "github.com/spf13/cobra" "github.com/spf13/viper" ) // saveCmd represents the save command var saveCmd = &cobra.Command{ Use: "save ", Example: "mctl save newcmd", Short: "Saves a server command under an alias for quick execution", Long: `Saves supplied command using alias to allow the command to be executed using the run command. The %s placeholder can be used as a wildcard to be injected when running the command`, Run: func(cmd *cobra.Command, args []string) { fmt.Printf("Use %s as a wildcard in your command\n", "%s") //this is ugly, have to use printf to stop issues with compiler fmt.Printf("Command: ") sc := bufio.NewScanner(os.Stdin) if sc.Scan() { txt := sc.Text() if txt != "" { viper.Set(fmt.Sprintf("customCmd-%s", args[0]), txt) viper.WriteConfig() fmt.Println("\nSaved!") return } } }, PreRunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { return errors.New("name argument is required") } return nil }, } func init() { rootCmd.AddCommand(saveCmd) }