moving run command
All checks were successful
code scans / scans (push) Successful in 26s
code scans / scans (pull_request) Successful in 24s

- run command is no longer attached to the 'command' subcmd
- run is standalone
This commit is contained in:
2025-11-26 13:04:14 -05:00
parent 210e574f51
commit b41b5e2be8
7 changed files with 26 additions and 21 deletions

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"os"
"code.jakeyoungdev.com/jake/mctl/constants"
"code.jakeyoungdev.com/jake/mctl/database"
"github.com/spf13/cobra"
)
@@ -42,8 +43,8 @@ var addCmd = &cobra.Command{
err = db.SaveCmd(cfgname, cfgcmd)
if err != nil {
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
if err.Error() == constants.ErrInit {
fmt.Println(constants.ErrInitRsp)
return
}
}

View File

@@ -7,13 +7,6 @@ import (
"github.com/spf13/cobra"
)
const (
//sqlite doesn't have an error type for this error, but we want to catch when this error is thrown
//and provide the proper response. It should not be treated as an error if the db isn't setup.
ErrInit = "sqlite3: SQL logic error: no such table: commands"
ErrInitRsp = "The 'init' command must be run before mctl can be used"
)
// CommandCmd is such a cool name lol
var CommandCmd = &cobra.Command{
Use: "command",

View File

@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"code.jakeyoungdev.com/jake/mctl/constants"
"code.jakeyoungdev.com/jake/mctl/database"
"github.com/spf13/cobra"
)
@@ -23,8 +24,8 @@ var deleteCmd = &cobra.Command{
err = db.DeleteCmd(args[0])
if err != nil {
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
if err.Error() == constants.ErrInit {
fmt.Println(constants.ErrInitRsp)
return
}
}

View File

@@ -1,80 +0,0 @@
/*
Copyright © 2025 Jake jake.young.dev@gmail.com
*/
package command
import (
"errors"
"fmt"
"strings"
"code.jakeyoungdev.com/jake/mctl/client"
"code.jakeyoungdev.com/jake/mctl/database"
"github.com/spf13/cobra"
)
var (
cfgserver string
)
var runCmd = &cobra.Command{
Use: "run",
Example: "mctl command run -s <server> <command> args...",
Short: "Runs a saved command on a server",
Long: `Runs the named command with the provided args on the default/active server unless -s is specified`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
cname := args[0]
db, err := database.New()
if err != nil {
return err
}
defer db.Close()
crun, err := db.Cmd(cname)
if err != nil {
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
return nil
}
return err
}
count := strings.Count(crun, "%s")
l := len(args)
if l-1 != count {
return fmt.Errorf("not enough arguments to fill all command placeholders, required: %d - have: %d", count, l-1)
}
cli, err := client.New(cfgserver)
cobra.CheckErr(err)
defer cli.Close()
var fargs []any
for _, x := range args[1:] {
fargs = append(fargs, x)
}
res, err := cli.Command(fmt.Sprintf(crun, fargs...))
if err != nil {
return err
}
fmt.Println(res)
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("name parameter required")
}
return nil
},
}
func init() {
runCmd.Flags().StringVarP(&cfgserver, "server", "s", "", "server name")
CommandCmd.AddCommand(runCmd)
}

View File

@@ -6,6 +6,7 @@ package command
import (
"fmt"
"code.jakeyoungdev.com/jake/mctl/constants"
"code.jakeyoungdev.com/jake/mctl/database"
"github.com/spf13/cobra"
)
@@ -22,8 +23,8 @@ var viewCmd = &cobra.Command{
ts, err := db.AllCmds()
if err != nil {
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
if err.Error() == constants.ErrInit {
fmt.Println(constants.ErrInitRsp)
return
}
}