[fix] Testing fixes
- sqlite error handling and wrapping - more responsive commands - database updates
This commit is contained in:
@@ -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")
|
||||
},
|
||||
}
|
||||
|
||||
|
@@ -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",
|
||||
|
@@ -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 {
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user