-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dee8f37
commit 7321b69
Showing
3 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package model | ||
|
||
import "gorm.io/gorm" | ||
|
||
type Database struct { | ||
gorm.Model | ||
Host string | ||
Username string | ||
Password string | ||
} |