[fix] Testing fixes

- sqlite error handling and wrapping
- more responsive commands
- database updates
This commit is contained in:
2025-06-19 17:23:17 -04:00
parent 0d7fbffaf6
commit 7ff43c82c2
15 changed files with 112 additions and 40 deletions

View File

@@ -13,9 +13,10 @@ import (
)
var addCmd = &cobra.Command{
Use: "add",
Example: "mctl command add",
Short: "Saves a new command to the database",
Use: "add",
Example: "mctl command add",
Short: "Saves a new command to the database",
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
scanner := bufio.NewScanner(os.Stdin)
@@ -40,7 +41,13 @@ var addCmd = &cobra.Command{
cobra.CheckErr(err)
err = db.SaveCmd(cfgname, cfgcmd)
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
return
}
cobra.CheckErr(err)
fmt.Println("Command saved")
},
}

View File

@@ -7,6 +7,11 @@ import (
"github.com/spf13/cobra"
)
const (
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

@@ -12,18 +12,23 @@ import (
)
var deleteCmd = &cobra.Command{
Use: "delete",
Example: "mctl command delete <name>",
Short: "Deletes a command from the database",
Use: "delete",
Example: "mctl command delete <name>",
Short: "Deletes a command from the database",
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)
defer db.Close()
err = db.DeleteCmd(args[0])
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
return
}
cobra.CheckErr(err)
fmt.Println("Command deleted!")
fmt.Println("Command deleted")
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {

View File

@@ -18,10 +18,11 @@ var (
)
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`,
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]
@@ -32,6 +33,10 @@ var runCmd = &cobra.Command{
defer db.Close()
crun, err := db.GetCmd(cname)
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
return nil
}
if err != nil {
return err
}

View File

@@ -11,15 +11,20 @@ import (
)
var viewCmd = &cobra.Command{
Use: "view",
Example: "mctl command view",
Short: "view all saved commands",
Use: "view",
Example: "mctl command view",
Short: "view all saved commands",
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
db, err := database.New()
cobra.CheckErr(err)
defer db.Close()
ts, err := db.GetAllCmds()
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
return
}
cobra.CheckErr(err)
for _, s := range ts {