Skip to content

Commit

Permalink
Merge pull request #120 from Kuchteq/main
Browse files Browse the repository at this point in the history
Add connecting to database by connectionString given as an argument to cli
  • Loading branch information
jorgerojas26 authored Dec 7, 2024
2 parents e080d02 + ac8969d commit d4cd01f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
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

0 comments on commit d4cd01f

Please sign in to comment.