Skip to content

Commit

Permalink
refactor(app): add the client as a field of the gui, instead of being…
Browse files Browse the repository at this point in the history
… a field of the app
  • Loading branch information
danvergara committed May 2, 2021
1 parent bf478d0 commit a1a2617
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func NewRootCmd() *cobra.Command {
return err
}

g := gui.New(gcui)
g := gui.New(gcui, c)

app := app.New(c, g)
app := app.New(g)

if err := app.Run(); err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package app

import (
"github.com/danvergara/dblab/pkg/client"
"github.com/danvergara/dblab/pkg/gui"
)

// App Struct.
type App struct {
c *client.Client
g *gui.Gui
}

// New bootstrap a new application.
func New(c *client.Client, g *gui.Gui) *App {
func New(g *gui.Gui) *App {
return &App{
c: c,
g: g,
}
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (

// Client is used to store the pool of db connection.
type Client struct {
db *sqlx.DB
db *sqlx.DB
driver string
}

// New return an instance of the client.
Expand All @@ -22,14 +23,14 @@ func New(opts command.Options) (*Client, error) {
if err != nil {
return nil, err
}

db, err := sqlx.Open(opts.Driver, conn)
if err != nil {
return nil, err
}

c := Client{
db: db,
db: db,
driver: opts.Driver,
}

return &c, nil
Expand All @@ -39,3 +40,8 @@ func New(opts command.Options) (*Client, error) {
func (c *Client) DB() *sqlx.DB {
return c.db
}

// Driver returns the driver of the database.
func (c *Client) Driver() string {
return c.driver
}
9 changes: 7 additions & 2 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package gui

import "github.com/jroimartin/gocui"
import (
"github.com/danvergara/dblab/pkg/client"
"github.com/jroimartin/gocui"
)

// Gui wraps the gocui Gui object which handles rendering and events.
type Gui struct {
g *gocui.Gui
c *client.Client
}

// New builds a new gui handler.
func New(g *gocui.Gui) *Gui {
func New(g *gocui.Gui, c *client.Client) *Gui {
return &Gui{
g: g,
c: c,
}
}

Expand Down

0 comments on commit a1a2617

Please sign in to comment.