Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connecting to database by connectionString given as an argument to cli #120

Merged
merged 13 commits into from
Dec 7, 2024
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,17 @@ makepkg -si

## Usage

```bash
```console
$ lazysql
```

To launch lazysql with the ability to pick from the saved connections.
```console
$ lazysql [connection_url]
```

To launch lazysql and connect to database at [connection_url].

<p align="right">(<a href="#readme-top">back to top</a>)</p>

## Support
Expand Down
45 changes: 45 additions & 0 deletions components/ArgConnection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package components

import (
"fmt"
"strings"

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

func InitFromArg(connectionString string) error {
parsed, err := helpers.ParseConnectionString(connectionString)
if err != nil {
return fmt.Errorf("Could not parse connection string: %s", err)
}
DBName := strings.Split(parsed.Normalize(",", "NULL", 0), ",")[3]

if DBName == "NULL" {
DBName = ""
}

connection := models.Connection{
Name: "",
Provider: parsed.Driver,
DBName: DBName,
URL: connectionString,
}
var newDbDriver drivers.Driver
switch connection.Provider {
case drivers.DriverMySQL:
newDbDriver = &drivers.MySQL{}
case drivers.DriverPostgres:
newDbDriver = &drivers.Postgres{}
case drivers.DriverSqlite:
newDbDriver = &drivers.SQLite{}
}
err = newDbDriver.Connect(connection.URL)

if err != nil {
return fmt.Errorf("Could not connect to database %s: %s", connectionString, err)
}
MainPages.AddAndSwitchToPage(connection.URL, NewHomePage(connection, newDbDriver).Flex, true)
return nil
}
34 changes: 27 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@ import (
var version = "dev"

func main() {
flag.Usage = func() {
f := flag.CommandLine.Output()
fmt.Fprintln(f, "lazysql")
fmt.Fprintln(f, "")
fmt.Fprintf(f, "Usage: %s [options] [connection_url]\n\n", os.Args[0])
fmt.Fprintln(f, " connection_url")
fmt.Fprintln(f, " database URL to connect to. Omit to start in picker mode")
fmt.Fprintln(f, "")
fmt.Fprintln(f, "Options:")
flag.PrintDefaults()
}
printVersion := flag.Bool("version", false, "Show version")
logLevel := flag.String("loglevel", "info", "Log level")
logFile := flag.String("logfile", "", "Log file")
flag.Parse()

if *printVersion {
println("LazySQL version: ", version)
os.Exit(0)
}

slogLevel, err := logger.ParseLogLevel(*logLevel)
if err != nil {
log.Fatalf("Error parsing log level: %v", err)
Expand All @@ -39,15 +56,18 @@ func main() {
log.Fatalf("Error setting MySQL logger: %v", err)
}

// Check if "version" arg is passed.
argsWithProg := os.Args
args := flag.Args()

if len(argsWithProg) > 1 {
switch argsWithProg[1] {
case "version":
fmt.Println("LazySQL version: ", version)
os.Exit(0)
switch len(args) {
case 0:
// nothing to do. Launch into the connection picker.
case 1:
err := components.InitFromArg(args[0])
if err != nil {
log.Fatal(err)
}
default:
log.Fatal("Only a single connection is allowed")
}

if err = app.App.Run(components.MainPages); err != nil {
Expand Down
Loading