feat: add env parsing before creating environment

This commit is contained in:
2025-09-25 22:25:03 +05:30
parent 22895f0192
commit e1d64e5750
2 changed files with 55 additions and 28 deletions

View File

@@ -27,19 +27,19 @@ var createCmd = &cobra.Command{
// file exists, create environment
if err == nil {
envy, err := utils.GetEnvy()
if err != nil {
if err != nil {
utils.ErrPrint(err.Error())
return
}
project := envy.Get("envy.project").(string)
if project == "" {
if project == "" {
utils.ErrPrint("Project name is empty. .envy file might have been modified.")
return
}
dbProject, err := utils.GetProject(project)
if err != nil {
if err != nil {
utils.ErrPrint(err.Error())
return
}
@@ -68,7 +68,7 @@ var createCmd = &cobra.Command{
return
}
env := utils.Environment{Name: environment, ProjectID: dbProject.ID, Data: string(envFile)}
env := utils.Environment{Name: environment, ProjectID: dbProject.ID, Data: utils.ParseEnv(string(envFile))}
utils.DB.Create(&env)
fmt.Printf("Created environment %s for project %s.\n", environment, project)
@@ -107,7 +107,7 @@ var createCmd = &cobra.Command{
utils.ErrPrint("Some error occured:", err.Error())
}
if project == "" || environment == "" {
if project == "" || environment == "" {
return
}
@@ -122,7 +122,7 @@ var createCmd = &cobra.Command{
proj := utils.Project{Name: project}
utils.DB.Create(&proj)
env := utils.Environment{Name: environment, ProjectID: proj.ID, Data: string(envFile)}
env := utils.Environment{Name: environment, ProjectID: proj.ID, Data: utils.ParseEnv(string(envFile))}
utils.DB.Create(&env)
filePath := path.Join(cwd, ".envy")

View File

@@ -2,21 +2,24 @@ package utils
import (
"errors"
"fmt"
"os"
"path"
"regexp"
"strings"
"github.com/pelletier/go-toml"
"gorm.io/gorm"
)
func StopIfErr(err error) {
if err != nil {
func StopIfErr(err error) {
if err != nil {
ErrPrint(err.Error())
os.Exit(1)
}
}
func GetEnvy() (*toml.Tree, error) {
func GetEnvy() (*toml.Tree, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, errors.New("Not able to fetch working directory: " + err.Error())
@@ -27,63 +30,87 @@ func GetEnvy() (*toml.Tree, error) {
// file exists
if err == nil {
envyFile, err := toml.LoadFile(path.Join(cwd, ".envy"))
if err != nil {
if err != nil {
return nil, errors.New("Error occured while reading .envy file:" + err.Error())
}
return envyFile, nil
}
// file does not exist
if os.IsNotExist(err) {
return nil, errors.New(".envy file doesn't exist in the current directory.")
} else {
if os.IsNotExist(err) {
return nil, errors.New(".envy file doesn't exist in the current directory")
} else {
return nil, errors.New("Some error occured:" + err.Error())
}
}
func GetProject(name string) (Project, error) {
func GetProject(name string) (Project, error) {
InitDb()
var dbProject Project
result := DB.Where("name = ?", name).First(&dbProject)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return Project{}, errors.New("Project with the name " + name + " not found in the database.")
} else {
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return Project{}, errors.New("Project with the name " + name + " not found in the database")
} else {
return Project{}, errors.New("Some error occured: " + result.Error.Error())
}
}
return dbProject, nil
}
func GetEnvironments(project uint) ([]Environment, error) {
func GetEnvironments(project uint) ([]Environment, error) {
InitDb()
var dbEnvs []Environment
result := DB.Where("project_id = ?", project).Find(&dbEnvs)
if result.Error != nil {
if result.Error != nil {
return nil, errors.New("Some error occured: " + result.Error.Error())
}
return dbEnvs, nil
}
func GetEnvironment(project uint, environment string) (Environment, error) {
func GetEnvironment(project uint, environment string) (Environment, error) {
InitDb()
var dbEnv Environment
result := DB.Where("project_id = ? AND name = ?", project, environment).First(&dbEnv)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return Environment{}, errors.New("Environment with the name " + environment + " not found in the database.")
} else {
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return Environment{}, errors.New("Environment with the name " + environment + " not found in the database")
} else {
return Environment{}, errors.New("Some error occured: " + result.Error.Error())
}
}
return dbEnv, nil
}
func ParseEnv(env string) string {
re := regexp.MustCompile(`^\s*(#?)\s*([A-Z0-9_]+)\s*=\s*(?:"([^"\n]+)"|([^"\n]+))$`)
var envs string
for _, line := range strings.Split(string(env), "\n") {
matches := re.FindStringSubmatch(line)
var key, value string
var enabled bool
if matches != nil {
enabled = matches[1] == ""
key = matches[2] // the key
value = matches[3] // quoted value
if value == "" {
value = matches[4] // unquoted value
}
var comment = ""
if enabled {
comment = "# "
}
envs += fmt.Sprintf("%v%v=%v\n", comment, key, value)
}
}
return envs
}