null pointer fix

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

View File

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

View File

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

View File

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

View File

@@ -8,6 +8,8 @@ import (
)
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"
ErrInitRsp = "The 'init' command must be run before mctl can be used"
)

View File

@@ -30,9 +30,11 @@ var updateCmd = &cobra.Command{
defer db.Close()
err = db.UpdateServer(args[0], base64.StdEncoding.EncodeToString(ps))
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
return
if err != nil {
if err.Error() == ErrInit {
fmt.Println(ErrInitRsp)
return
}
}
cobra.CheckErr(err)

View File

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