52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
/*
|
|
Copyright © 2025 Jake jake.young.dev@gmail.com
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"code.jakeyoungdev.com/jake/mctl/database"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// 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
|
|
fmt.Printf("Command: ")
|
|
sc := bufio.NewScanner(os.Stdin)
|
|
if sc.Scan() {
|
|
txt := sc.Text()
|
|
if txt != "" {
|
|
db, err := database.New()
|
|
cobra.CheckErr(err)
|
|
defer db.Close()
|
|
|
|
err = db.SaveCmd(args[0], txt)
|
|
cobra.CheckErr(err)
|
|
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)
|
|
}
|