Skip to content

Commit

Permalink
refactor(root): refactor the root command to call the objects instead…
Browse files Browse the repository at this point in the history
… manage the application
  • Loading branch information
danvergara committed Apr 28, 2021
1 parent cd520e9 commit 18c18bf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
29 changes: 16 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ limitations under the License.

import (
"fmt"
"log"
"os"

"github.com/danvergara/dblab/pkg/app"
"github.com/danvergara/dblab/pkg/client"
"github.com/danvergara/dblab/pkg/command"
"github.com/danvergara/dblab/pkg/connection"
"github.com/danvergara/dblab/pkg/gui"
"github.com/jroimartin/gocui"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -50,7 +52,6 @@ func NewRootCmd() *cobra.Command {
Short: "Interactive databse client",
Long: `dblab is a terminal UI based interactive database client for Postgres, MySQL and SQLite.`,
RunE: func(cmd *cobra.Command, args []string) error {

opts := command.Options{
Driver: driver,
URL: url,
Expand All @@ -62,24 +63,26 @@ func NewRootCmd() *cobra.Command {
SSL: ssl,
}

if opts.Host == "" && opts.Port == "" && opts.User == "" && opts.Pass == "" && opts.DBName == "" && opts.Driver == "" && opts.URL == "" {
return fmt.Errorf("empty values required to open a session in database")
if err := connection.ValidateOpts(opts); err != nil {
return err
}

g, err := gocui.NewGui(gocui.OutputNormal)
c, err := client.New(opts)
if err != nil {
log.Panicln(err)
return err
}
defer g.Close()

g.SetManagerFunc(gui.Layout)

if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, gui.Quit); err != nil {
log.Panicln(err)
gcui, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
return err
}

if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
g := gui.New(gcui)

app := app.New(c, g)

if err := app.Run(); err != nil {
return err
}

return nil
Expand Down
15 changes: 15 additions & 0 deletions pkg/connection/validateopts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package connection

import (
"fmt"

"github.com/danvergara/dblab/pkg/command"
)

// ValidateOpts make sure the important fields used to open a connection aren't empty.
func ValidateOpts(opts command.Options) error {
if opts.Host == "" && opts.Port == "" && opts.User == "" && opts.Pass == "" && opts.DBName == "" && opts.Driver == "" && opts.URL == "" {
return fmt.Errorf("empty values required to open a session in database")
}
return nil
}

0 comments on commit 18c18bf

Please sign in to comment.