diff --git a/cmd/add.go b/cmd/add.go index f1f44c6..dfed21a 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -3,7 +3,7 @@ package cmd import ( "errors" - "github.com/gookit/goutil/dump" + db "github.com/lewissteele/dbat/internal" "github.com/manifoldco/promptui" "github.com/spf13/cobra" ) @@ -40,7 +40,9 @@ func run(cmd *cobra.Command, args []string) { results = append(results, result) } - dump.P(results) + host, username, password := results[0], results[1], results[2] + + db.SaveConnection(host, username, password) } func isNotBlank(val string) error { diff --git a/internal/db.go b/internal/db.go new file mode 100644 index 0000000..2968a83 --- /dev/null +++ b/internal/db.go @@ -0,0 +1,38 @@ +package db + +import ( + "os" + "path/filepath" + + "github.com/lewissteele/dbat/internal/model" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +var localDB *gorm.DB + +func SaveConnection(host string, username string, password string) { + localDB.Create(&model.Database{Host: "localhost", Username: "root", Password: ""}) +} + +func init() { + configHome := os.Getenv("XDG_CONFIG_HOME") + + if len(configHome) == 0 { + configHome = filepath.Join(os.Getenv("HOME"), ".config") + } + + path := filepath.Join(configHome, "dbat/dbat.db") + conn, err := gorm.Open(sqlite.Open(path), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Silent), + }) + + if err != nil { + panic("could not connect to sqlite db") + } + + conn.AutoMigrate(&model.Database{}) + + localDB = conn +} diff --git a/internal/model/database.go b/internal/model/database.go new file mode 100644 index 0000000..79b8df5 --- /dev/null +++ b/internal/model/database.go @@ -0,0 +1,10 @@ +package model + +import "gorm.io/gorm" + +type Database struct { + gorm.Model + Host string + Username string + Password string +}