mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
build: minor fixes and build optimization
This commit is contained in:
@@ -141,3 +141,47 @@ func ParseEnv(env string) string {
|
||||
|
||||
return envs
|
||||
}
|
||||
|
||||
|
||||
type Env struct {
|
||||
Enabled bool
|
||||
Key string
|
||||
Value string
|
||||
}
|
||||
|
||||
func ParseEnvToStruct(env string) []Env {
|
||||
re := regexp.MustCompile(`^\s*(#?)\s*([A-Z0-9_]+)\s*=\s*(?:"([^"\n]+)"|([^"\n]+))$`)
|
||||
|
||||
var envs []Env
|
||||
|
||||
for _, line := range strings.Split(string(env), "\n") {
|
||||
matches := re.FindStringSubmatch(line)
|
||||
var env Env
|
||||
if matches != nil {
|
||||
env.Enabled = matches[1] == ""
|
||||
env.Key = matches[2] // the key
|
||||
env.Value = matches[3] // quoted value
|
||||
if env.Value == "" {
|
||||
env.Value = matches[4] // unquoted value
|
||||
}
|
||||
envs = append(envs, env)
|
||||
}
|
||||
}
|
||||
|
||||
return envs
|
||||
}
|
||||
|
||||
|
||||
func ParseEnvStruct(envs []Env) string {
|
||||
var env string
|
||||
|
||||
for _, en := range envs {
|
||||
comment := ""
|
||||
if !en.Enabled {
|
||||
comment = "#"
|
||||
}
|
||||
env += fmt.Sprintf("%v%v=%v\n", comment, en.Key, en.Value)
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user