2025-04-17 15:40:54 +00:00
|
|
|
/*
|
|
|
|
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 <name>",
|
|
|
|
Example: "mctl save newcmd",
|
|
|
|
Short: "Saves a server command under an alias for quick execution",
|
|
|
|
Long: `Saves supplied command using alias <name> 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 != "" {
|
2025-04-17 17:24:09 -04:00
|
|
|
var cmdMap map[string]any
|
|
|
|
cm := viper.Get("customcmd")
|
|
|
|
if cmdMap == nil {
|
|
|
|
cmdMap = make(map[string]any, 0)
|
|
|
|
} else {
|
|
|
|
cmdMap = cm.(map[string]any)
|
|
|
|
}
|
|
|
|
cmdMap[args[0]] = txt
|
|
|
|
viper.Set("customcmd", cmdMap)
|
2025-04-17 15:40:54 +00:00
|
|
|
viper.WriteConfig()
|
|
|
|
fmt.Println("\nSaved!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|