mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
feat: add init command and setup db
This commit is contained in:
44
cmd/utils/db.go
Normal file
44
cmd/utils/db.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Project struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Name string
|
||||
Environments []Environment
|
||||
}
|
||||
|
||||
type Environment struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Name string
|
||||
Data string
|
||||
Active bool
|
||||
ProjectID uint
|
||||
Project Project `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
|
||||
}
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func Init() {
|
||||
config, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
fmt.Println(ErrStyle.Render("Error accessing config directory:", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
DB, err = gorm.Open(sqlite.Open(path.Join(config, "envy.db")), &gorm.Config{})
|
||||
if err != nil {
|
||||
ErrPrint("Error while opening database:" + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func Migrate() {
|
||||
DB.AutoMigrate(&Project{}, &Environment{})
|
||||
}
|
||||
13
cmd/utils/tools.go
Normal file
13
cmd/utils/tools.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
)
|
||||
|
||||
var ErrStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#F00"))
|
||||
|
||||
func ErrPrint(data string) {
|
||||
fmt.Println(ErrStyle.Render(data))
|
||||
}
|
||||
Reference in New Issue
Block a user