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:
71
cmd/init.go
Normal file
71
cmd/init.go
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"envy/cmd/utils"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// initCmd represents the init command
|
||||
var initCmd = &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "Initialize envy on your system",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
config, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
fmt.Println(utils.ErrStyle.Render("Config directory not found for your OS. Please run `envy init <path>`."))
|
||||
return
|
||||
}
|
||||
|
||||
envyPath := path.Join(config, "envy")
|
||||
|
||||
_, err = os.Stat(envyPath)
|
||||
|
||||
if os.IsExist(err) {
|
||||
fmt.Println(utils.ErrStyle.Render("Config directory already exists."))
|
||||
return
|
||||
}
|
||||
|
||||
err = os.Mkdir(envyPath, 0755)
|
||||
if err != nil {
|
||||
fmt.Println(utils.ErrStyle.Render("Failed to create config directory:", err.Error()))
|
||||
}
|
||||
|
||||
configFile, err := os.Create(path.Join(envyPath, "config.toml"))
|
||||
if err != nil {
|
||||
fmt.Println(utils.ErrStyle.Render("Failed to create config file:", err.Error()))
|
||||
}
|
||||
defer configFile.Close()
|
||||
|
||||
_, err = os.Create(path.Join(envyPath, "envy.db"))
|
||||
if err != nil {
|
||||
fmt.Println(utils.ErrStyle.Render("Failed to create database file:", err.Error()))
|
||||
}
|
||||
|
||||
utils.Migrate()
|
||||
|
||||
configFile.Write([]byte("[config]\nmode = \"local\"\n"))
|
||||
|
||||
fmt.Println("envy initialized on your system.")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(initCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// initCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
15
cmd/root.go
15
cmd/root.go
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
||||
|
||||
Copyright © 2025 Suraj B M <silicoflare@gmail.com>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
@@ -10,18 +9,10 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "envy",
|
||||
Short: "A brief description of your application",
|
||||
Long: `A longer description that spans multiple lines and likely contains
|
||||
examples and usage of using your application. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Short: "A CLI tool to manage .env files for your projects",
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
@@ -47,5 +38,3 @@ func init() {
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
|
||||
|
||||
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