null pointer fix

- need to nil check before pulling value
This commit is contained in:
jake 2025-06-19 17:28:56 -04:00
parent 7ff43c82c2
commit 71ace969d3
12 changed files with 53 additions and 31 deletions

View File

@ -41,10 +41,12 @@ var addCmd = &cobra.Command{
cobra.CheckErr(err) cobra.CheckErr(err)
err = db.SaveCmd(cfgname, cfgcmd) err = db.SaveCmd(cfgname, cfgcmd)
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
fmt.Println("Command saved") fmt.Println("Command saved")

View File

@ -8,6 +8,8 @@ import (
) )
const ( 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" ErrInit = "sqlite3: SQL logic error: no such table: commands"
ErrInitRsp = "The 'init' command must be run before mctl can be used" ErrInitRsp = "The 'init' command must be run before mctl can be used"
) )

View File

@ -22,10 +22,12 @@ var deleteCmd = &cobra.Command{
defer db.Close() defer db.Close()
err = db.DeleteCmd(args[0]) err = db.DeleteCmd(args[0])
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
fmt.Println("Command deleted") fmt.Println("Command deleted")

View File

@ -33,11 +33,11 @@ var runCmd = &cobra.Command{
defer db.Close() defer db.Close()
crun, err := db.GetCmd(cname) crun, err := db.GetCmd(cname)
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return nil return nil
} }
if err != nil {
return err return err
} }

View File

@ -21,10 +21,12 @@ var viewCmd = &cobra.Command{
defer db.Close() defer db.Close()
ts, err := db.GetAllCmds() ts, err := db.GetAllCmds()
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
for _, s := range ts { for _, s := range ts {

View File

@ -31,10 +31,12 @@ var destroyCmd = &cobra.Command{
defer db.Close() defer db.Close()
err = db.Destroy() err = db.Destroy()
if err != nil {
if err.Error() == command.ErrInit { if err.Error() == command.ErrInit {
fmt.Println(command.ErrInitRsp) fmt.Println(command.ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
fmt.Println("Configuration is cleared, the 'init' command must be run again.") fmt.Println("Configuration is cleared, the 'init' command must be run again.")

View File

@ -22,10 +22,12 @@ var activeCmd = &cobra.Command{
defer db.Close() defer db.Close()
err = db.SetActiveServer(args[0]) err = db.SetActiveServer(args[0])
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
fmt.Println("Active server updated") fmt.Println("Active server updated")

View File

@ -63,10 +63,12 @@ var addCmd = &cobra.Command{
Port: fp, Port: fp,
Password: base64.StdEncoding.EncodeToString(ps), Password: base64.StdEncoding.EncodeToString(ps),
}) })
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
fmt.Println("Server saved") fmt.Println("Server saved")

View File

@ -22,10 +22,12 @@ var deleteCmd = &cobra.Command{
defer db.Close() defer db.Close()
err = db.DeleteServer(args[0]) err = db.DeleteServer(args[0])
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
fmt.Println("Server deleted") fmt.Println("Server deleted")

View File

@ -8,6 +8,8 @@ import (
) )
const ( 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: servers" ErrInit = "sqlite3: SQL logic error: no such table: servers"
ErrInitRsp = "The 'init' command must be run before mctl can be used" ErrInitRsp = "The 'init' command must be run before mctl can be used"
) )

View File

@ -30,10 +30,12 @@ var updateCmd = &cobra.Command{
defer db.Close() defer db.Close()
err = db.UpdateServer(args[0], base64.StdEncoding.EncodeToString(ps)) err = db.UpdateServer(args[0], base64.StdEncoding.EncodeToString(ps))
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
fmt.Printf("%s password updated!", args[0]) fmt.Printf("%s password updated!", args[0])

View File

@ -21,10 +21,12 @@ var viewCmd = &cobra.Command{
defer db.Close() defer db.Close()
ts, err := db.GetAllServers() ts, err := db.GetAllServers()
if err != nil {
if err.Error() == ErrInit { if err.Error() == ErrInit {
fmt.Println(ErrInitRsp) fmt.Println(ErrInitRsp)
return return
} }
}
cobra.CheckErr(err) cobra.CheckErr(err)
for _, s := range ts { for _, s := range ts {