diff --git a/cmd/command/add.go b/cmd/command/add.go index 63975ad..fe85d7d 100644 --- a/cmd/command/add.go +++ b/cmd/command/add.go @@ -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") }, } diff --git a/cmd/command/command.go b/cmd/command/command.go index 600466b..f9f58dc 100644 --- a/cmd/command/command.go +++ b/cmd/command/command.go @@ -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", diff --git a/cmd/command/delete.go b/cmd/command/delete.go index 499c8f0..ddd4577 100644 --- a/cmd/command/delete.go +++ b/cmd/command/delete.go @@ -12,18 +12,23 @@ import ( ) var deleteCmd = &cobra.Command{ - Use: "delete", - Example: "mctl command delete ", - Short: "Deletes a command from the database", + Use: "delete", + Example: "mctl command delete ", + 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 { diff --git a/cmd/command/run.go b/cmd/command/run.go index 8ff9a2b..c739180 100644 --- a/cmd/command/run.go +++ b/cmd/command/run.go @@ -18,10 +18,11 @@ var ( ) var runCmd = &cobra.Command{ - Use: "run", - Example: "mctl command run -s 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 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 } diff --git a/cmd/command/view.go b/cmd/command/view.go index ec9006c..a615eb3 100644 --- a/cmd/command/view.go +++ b/cmd/command/view.go @@ -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 { diff --git a/cmd/destroy.go b/cmd/destroy.go index c793b9e..4e892c3 100644 --- a/cmd/destroy.go +++ b/cmd/destroy.go @@ -9,6 +9,7 @@ import ( "os" "strings" + "code.jakeyoungdev.com/jake/mctl/cmd/command" "code.jakeyoungdev.com/jake/mctl/database" "github.com/spf13/cobra" ) @@ -19,6 +20,7 @@ var destroyCmd = &cobra.Command{ Short: "clears all configuration", Long: `clear all data and drop database tables, this will delete all previously saved data and the 'inti' command must be run again before use`, + SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { scanner := bufio.NewScanner(os.Stdin) fmt.Printf("Are you sure you want to destroy your config? (yes|no): ") @@ -29,6 +31,10 @@ var destroyCmd = &cobra.Command{ defer db.Close() err = db.Destroy() + if err.Error() == command.ErrInit { + fmt.Println(command.ErrInitRsp) + return + } cobra.CheckErr(err) fmt.Println("Configuration is cleared, the 'init' command must be run again.") diff --git a/cmd/init.go b/cmd/init.go index b9b144d..49046d5 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -9,9 +9,10 @@ import ( ) var initCmd = &cobra.Command{ - Use: "init", - Example: "mctl init", - Short: "initializes the database tables, must be run before mctl can be used", + Use: "init", + Example: "mctl init", + Short: "initializes the database tables, must be run before mctl can be used", + SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { db, err := database.New() cobra.CheckErr(err) diff --git a/cmd/login.go b/cmd/login.go index 57105b1..298712a 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -9,7 +9,6 @@ import ( "os" "code.jakeyoungdev.com/jake/mctl/client" - "code.jakeyoungdev.com/jake/mctl/database" "github.com/spf13/cobra" ) @@ -26,10 +25,6 @@ var loginCmd = &cobra.Command{ Long: `Login to default server and enter command loop. The default server is used unless the server flag is set`, Run: func(cmd *cobra.Command, args []string) { - db, err := database.New() - cobra.CheckErr(err) - defer db.Close() - cli, err := client.New(server) cobra.CheckErr(err) defer cli.Close() diff --git a/cmd/server/active.go b/cmd/server/active.go index 9d780da..65922f1 100644 --- a/cmd/server/active.go +++ b/cmd/server/active.go @@ -12,15 +12,20 @@ import ( ) var activeCmd = &cobra.Command{ - Use: "active", - Example: "mctl server active ", - Short: "sets the active server to run commands on", + Use: "active", + Example: "mctl server active ", + Short: "sets the active server to run commands on", + SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { db, err := database.New() cobra.CheckErr(err) defer db.Close() err = db.SetActiveServer(args[0]) + if err.Error() == ErrInit { + fmt.Println(ErrInitRsp) + return + } cobra.CheckErr(err) fmt.Println("Active server updated") diff --git a/cmd/server/add.go b/cmd/server/add.go index 7d63f83..9d67339 100644 --- a/cmd/server/add.go +++ b/cmd/server/add.go @@ -17,10 +17,11 @@ import ( ) var addCmd = &cobra.Command{ - Use: "add", - Example: "mctl server add", - Short: "Saves a new server configuration", - Long: `Saves server address, alias, port, and password to the database.`, + Use: "add", + Example: "mctl server add", + Short: "Saves a new server configuration", + Long: `Saves server address, alias, port, and password to the database.`, + SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { scanner := bufio.NewScanner(os.Stdin) @@ -62,6 +63,10 @@ var addCmd = &cobra.Command{ Port: fp, Password: base64.StdEncoding.EncodeToString(ps), }) + if err.Error() == ErrInit { + fmt.Println(ErrInitRsp) + return + } cobra.CheckErr(err) fmt.Println("Server saved") diff --git a/cmd/server/delete.go b/cmd/server/delete.go index 0fac751..13bab0e 100644 --- a/cmd/server/delete.go +++ b/cmd/server/delete.go @@ -5,22 +5,30 @@ package server import ( "errors" + "fmt" "code.jakeyoungdev.com/jake/mctl/database" "github.com/spf13/cobra" ) var deleteCmd = &cobra.Command{ - Use: "delete", - Example: "mctl server delete ", - Short: "deletes a server from the database", + Use: "delete", + Example: "mctl server delete ", + Short: "deletes a server from the database", + SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { db, err := database.New() cobra.CheckErr(err) defer db.Close() err = db.DeleteServer(args[0]) + if err.Error() == ErrInit { + fmt.Println(ErrInitRsp) + return + } cobra.CheckErr(err) + + fmt.Println("Server deleted") }, PreRunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { diff --git a/cmd/server/server.go b/cmd/server/server.go index d2bc87e..64352da 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -7,6 +7,11 @@ import ( "github.com/spf13/cobra" ) +const ( + ErrInit = "sqlite3: SQL logic error: no such table: servers" + ErrInitRsp = "The 'init' command must be run before mctl can be used" +) + var ServerCmd = &cobra.Command{ Use: "server", Example: "mctl server ", diff --git a/cmd/server/update.go b/cmd/server/update.go index f2782c8..f5368c1 100644 --- a/cmd/server/update.go +++ b/cmd/server/update.go @@ -15,9 +15,10 @@ import ( ) var updateCmd = &cobra.Command{ - Use: "update", - Example: "mctl server update ", - Short: "updates a saved servers password in the database", + Use: "update", + Example: "mctl server update ", + Short: "updates a saved servers password in the database", + SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { //read in password using term to keep it secure/hidden from bash history fmt.Printf("Password: ") @@ -29,6 +30,10 @@ var updateCmd = &cobra.Command{ defer db.Close() err = db.UpdateServer(args[0], base64.StdEncoding.EncodeToString(ps)) + if err.Error() == ErrInit { + fmt.Println(ErrInitRsp) + return + } cobra.CheckErr(err) fmt.Printf("%s password updated!", args[0]) diff --git a/cmd/server/view.go b/cmd/server/view.go index 630a5d3..e52fb46 100644 --- a/cmd/server/view.go +++ b/cmd/server/view.go @@ -11,15 +11,20 @@ import ( ) var viewCmd = &cobra.Command{ - Use: "view", - Example: "mctl server view", - Short: "view all saved servers", + Use: "view", + Example: "mctl server view", + Short: "view all saved servers", + SilenceUsage: true, Run: func(cmd *cobra.Command, args []string) { db, err := database.New() cobra.CheckErr(err) defer db.Close() ts, err := db.GetAllServers() + if err.Error() == ErrInit { + fmt.Println(ErrInitRsp) + return + } cobra.CheckErr(err) for _, s := range ts { diff --git a/database/sqlx.go b/database/sqlx.go index c5f0700..499175a 100644 --- a/database/sqlx.go +++ b/database/sqlx.go @@ -2,6 +2,8 @@ package database import ( "context" + "database/sql" + "errors" "fmt" "os" "time" @@ -13,8 +15,7 @@ import ( ) /* -all sqlx methods for CRUD functionalities of commands and servers. All database methods should use the internal -methods query, exec, queryrow -- these wrapper functions handle timeouts and other configuration +all sqlx methods for CRUD functionalities of commands and servers. */ const ( @@ -123,6 +124,9 @@ func (d *database) GetCmd(name string) (string, error) { ctx, cl := d.timeout() defer cl() err := d.QueryRowxContext(ctx, query, name).Scan(&cmd) + if errors.Is(err, sql.ErrNoRows) { + return "", errors.New("Command not found") + } if err != nil { return "", err } @@ -222,6 +226,9 @@ func (d *database) GetServer(name string) (model.Server, error) { defer cancel() var s model.Server err := d.QueryRowxContext(ctx, query, name).StructScan(&s) + if errors.Is(err, sql.ErrNoRows) { + return model.Server{}, errors.New("Server not found") + } if err != nil { return model.Server{}, err } @@ -246,6 +253,9 @@ func (d *database) GetActiveServer() (model.Server, error) { defer cancel() var s model.Server err := d.QueryRowxContext(ctx, query).StructScan(&s) + if errors.Is(err, sql.ErrNoRows) { + return s, errors.New("No active server set") + } return s, err }