mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
feat: add env parsing before creating environment
This commit is contained in:
@@ -68,7 +68,7 @@ var createCmd = &cobra.Command{
|
|||||||
return
|
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)
|
utils.DB.Create(&env)
|
||||||
|
|
||||||
fmt.Printf("Created environment %s for project %s.\n", environment, project)
|
fmt.Printf("Created environment %s for project %s.\n", environment, project)
|
||||||
@@ -122,7 +122,7 @@ var createCmd = &cobra.Command{
|
|||||||
proj := utils.Project{Name: project}
|
proj := utils.Project{Name: project}
|
||||||
utils.DB.Create(&proj)
|
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)
|
utils.DB.Create(&env)
|
||||||
|
|
||||||
filePath := path.Join(cwd, ".envy")
|
filePath := path.Join(cwd, ".envy")
|
||||||
|
|||||||
@@ -2,8 +2,11 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/pelletier/go-toml"
|
"github.com/pelletier/go-toml"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -35,13 +38,12 @@ func GetEnvy() (*toml.Tree, error) {
|
|||||||
|
|
||||||
// file does not exist
|
// file does not exist
|
||||||
if os.IsNotExist(err) {
|
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 {
|
} else {
|
||||||
return nil, errors.New("Some error occured:" + err.Error())
|
return nil, errors.New("Some error occured:" + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func GetProject(name string) (Project, error) {
|
func GetProject(name string) (Project, error) {
|
||||||
InitDb()
|
InitDb()
|
||||||
|
|
||||||
@@ -50,7 +52,7 @@ func GetProject(name string) (Project, error) {
|
|||||||
|
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
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 {
|
} else {
|
||||||
return Project{}, errors.New("Some error occured: " + result.Error.Error())
|
return Project{}, errors.New("Some error occured: " + result.Error.Error())
|
||||||
}
|
}
|
||||||
@@ -58,7 +60,6 @@ func GetProject(name string) (Project, error) {
|
|||||||
return dbProject, nil
|
return dbProject, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func GetEnvironments(project uint) ([]Environment, error) {
|
func GetEnvironments(project uint) ([]Environment, error) {
|
||||||
InitDb()
|
InitDb()
|
||||||
|
|
||||||
@@ -71,7 +72,6 @@ func GetEnvironments(project uint) ([]Environment, error) {
|
|||||||
return dbEnvs, nil
|
return dbEnvs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func GetEnvironment(project uint, environment string) (Environment, error) {
|
func GetEnvironment(project uint, environment string) (Environment, error) {
|
||||||
InitDb()
|
InitDb()
|
||||||
|
|
||||||
@@ -80,10 +80,37 @@ func GetEnvironment(project uint, environment string) (Environment, error) {
|
|||||||
|
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
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 {
|
} else {
|
||||||
return Environment{}, errors.New("Some error occured: " + result.Error.Error())
|
return Environment{}, errors.New("Some error occured: " + result.Error.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dbEnv, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user