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

@@ -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)
@@ -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,8 +2,11 @@ package utils
import (
"errors"
"fmt"
"os"
"path"
"regexp"
"strings"
"github.com/pelletier/go-toml"
"gorm.io/gorm"
@@ -35,13 +38,12 @@ func GetEnvy() (*toml.Tree, error) {
// file does not exist
if os.IsNotExist(err) {
return nil, errors.New(".envy file doesn't exist in the current directory.")
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) {
InitDb()
@@ -50,7 +52,7 @@ func GetProject(name string) (Project, error) {
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.")
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())
}
@@ -58,7 +60,6 @@ func GetProject(name string) (Project, error) {
return dbProject, nil
}
func GetEnvironments(project uint) ([]Environment, error) {
InitDb()
@@ -71,7 +72,6 @@ func GetEnvironments(project uint) ([]Environment, error) {
return dbEnvs, nil
}
func GetEnvironment(project uint, environment string) (Environment, error) {
InitDb()
@@ -80,10 +80,37 @@ func GetEnvironment(project uint, environment string) (Environment, error) {
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.")
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
}