delete added, more details below (#3)

- readme updates
- delete command added
- commands now saved in a map vs fields
- login and run now ensure config has been run to prevent errors

Reviewed-on: #3
Co-authored-by: jake <jake.young.dev@gmail.com>
Co-committed-by: jake <jake.young.dev@gmail.com>
This commit was merged in pull request #3.
This commit is contained in:
2025-04-17 21:25:54 +00:00
committed by jake
parent a9c6400761
commit f8a9528e0f
8 changed files with 121 additions and 17 deletions

View File

@@ -15,14 +15,28 @@ import (
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run <name> args...",
Example: "mctl run savedcmd 63 jake",
Short: "Runs a previously saved command with supplied arguments on remote server",
Use: "run <name> args...",
Example: "mctl run savedcmd 63 jake",
SilenceUsage: true,
Short: "Runs a previously saved command with supplied arguments on remote server",
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) {
//grab saved command
cmdName := viper.Get(fmt.Sprintf("customCmd-%s", args[0])).(string)
//check for command map
var cm map[string]any
cmdMap := viper.Get("customcmd")
if cmdMap == nil {
cm = make(map[string]any, 0)
} else {
cm = cmdMap.(map[string]any)
}
//is this an existing command
cmdRun, ok := cm[args[0]]
if !ok {
fmt.Printf("command %s not found", args[0])
return
}
//convert arguments to interface
var nargs []any
for _, a := range args[1:] {
@@ -30,7 +44,7 @@ var runCmd = &cobra.Command{
}
//inject arguments
fixed := fmt.Sprintf(cmdName, nargs...)
fixed := fmt.Sprintf(cmdRun.(string), nargs...)
fmt.Printf("Running saved command %s\n", fixed)
//create client and send command
@@ -44,13 +58,20 @@ var runCmd = &cobra.Command{
fmt.Println(res)
},
PreRunE: func(cmd *cobra.Command, args []string) error {
//ensure configuration has been setup
if viper.Get("server") == "" || viper.Get("password") == "" || viper.Get("port") == 0 {
return errors.New("the 'config' command must be run before you can interact with servers")
}
//ensure we have a command name
al := len(args)
if al == 0 {
return errors.New("name argument is required")
}
cmdCheck := viper.Get(fmt.Sprintf("customCmd-%s", args[0]))
count := strings.Count(cmdCheck.(string), "%s")
cmdMap := viper.Get("customcmd").(map[string]any)
count := strings.Count(cmdMap[args[0]].(string), "%s")
//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)