2025-04-17 15:40:54 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2025 Jake jake.young.dev@gmail.com
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.jakeyoungdev.com/jake/mctl/client"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// runCmd represents the run command
|
|
|
|
var runCmd = &cobra.Command{
|
2025-04-17 21:25:54 +00:00
|
|
|
Use: "run <name> args...",
|
|
|
|
Example: "mctl run savedcmd 63 jake",
|
|
|
|
SilenceUsage: true,
|
|
|
|
Short: "Runs a previously saved command with supplied arguments on remote server",
|
2025-04-17 15:40:54 +00:00
|
|
|
Long: `Loads a saved command, injects the supplied arguments into the command, and sends the command to the remove server
|
|
|
|
printing the response`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2025-04-19 01:10:22 -04:00
|
|
|
cm := viper.Get("customcmd").(map[string]any)
|
2025-04-17 21:25:54 +00:00
|
|
|
//is this an existing command
|
|
|
|
cmdRun, ok := cm[args[0]]
|
|
|
|
if !ok {
|
|
|
|
fmt.Printf("command %s not found", args[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-04-17 15:40:54 +00:00
|
|
|
//convert arguments to interface
|
|
|
|
var nargs []any
|
|
|
|
for _, a := range args[1:] {
|
|
|
|
nargs = append(nargs, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
//inject arguments
|
2025-04-17 21:25:54 +00:00
|
|
|
fixed := fmt.Sprintf(cmdRun.(string), nargs...)
|
2025-04-17 15:40:54 +00:00
|
|
|
fmt.Printf("Running saved command %s\n", fixed)
|
|
|
|
|
|
|
|
//create client and send command
|
|
|
|
cli, err := client.New()
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
res, err := cli.Command(fixed)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
fmt.Println(res)
|
|
|
|
},
|
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
2025-04-19 01:10:22 -04:00
|
|
|
//ensure config command has been run
|
|
|
|
if !viper.IsSet("server") || !viper.IsSet("password") || !viper.IsSet("port") {
|
2025-04-17 21:25:54 +00:00
|
|
|
return errors.New("the 'config' command must be run before you can interact with servers")
|
|
|
|
}
|
|
|
|
|
2025-04-19 01:10:22 -04:00
|
|
|
if !viper.IsSet("customcmd") {
|
|
|
|
return errors.New("no saved commands to run")
|
|
|
|
}
|
|
|
|
|
2025-04-17 15:40:54 +00:00
|
|
|
//ensure we have a command name
|
|
|
|
al := len(args)
|
|
|
|
if al == 0 {
|
|
|
|
return errors.New("name argument is required")
|
|
|
|
}
|
2025-04-17 21:25:54 +00:00
|
|
|
|
|
|
|
cmdMap := viper.Get("customcmd").(map[string]any)
|
|
|
|
count := strings.Count(cmdMap[args[0]].(string), "%s")
|
|
|
|
|
2025-04-17 15:40:54 +00:00
|
|
|
//make sure enough arguments are sent to fill command placeholders
|
|
|
|
if al < count+1 {
|
|
|
|
return fmt.Errorf("not enough arguments to populate command. Supplied: %d, Needed: %d", al-1, count)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
}
|