Skip to content

Commit

Permalink
Add error to function signature and handle exiting in main func
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Kuchta committed Dec 5, 2024
1 parent ce4bbdd commit 0277c94
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 5 additions & 6 deletions components/ArgConnection.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package components

import (
"errors"
"fmt"
"os"
"strings"

"github.com/jorgerojas26/lazysql/drivers"
"github.com/jorgerojas26/lazysql/helpers"
"github.com/jorgerojas26/lazysql/models"
)

func InitFromArg(connectionString string) {
func InitFromArg(connectionString string) error {
parsed, err := helpers.ParseConnectionString(connectionString)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not parse connection string: %s\n", err)
os.Exit(1)
return errors.New(fmt.Sprintf("Could not parse connection string: %s", err))
}
DBName := strings.Split(parsed.Normalize(",", "NULL", 0), ",")[3]

Expand All @@ -40,8 +39,8 @@ func InitFromArg(connectionString string) {
err = newDbDriver.Connect(connection.URL)

if err != nil {
fmt.Fprintf(os.Stderr, "Could not connect to database %s: %s\n", connectionString, err)
os.Exit(1)
return errors.New(fmt.Sprintf("Could not connect to database %s: %s", connectionString, err))
}
MainPages.AddAndSwitchToPage(connection.URL, NewHomePage(connection, newDbDriver).Flex, true)
return nil
}
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"io"
"log"
"os"
Expand Down Expand Up @@ -46,7 +47,11 @@ func main() {
fmt.Println("LazySQL version: ", version)
os.Exit(0)
default:
components.InitFromArg(argsWithProg[1])
err := components.InitFromArg(argsWithProg[1])
if err != nil {
fmt.Println(os.Stderr, err)
os.Exit(1)
}
}
}

Expand Down

0 comments on commit 0277c94

Please sign in to comment.