build: minor fixes and build optimization

This commit is contained in:
2025-09-26 11:30:16 +05:30
parent e5a002e4b4
commit df2b446907
6 changed files with 121 additions and 20 deletions

View File

@@ -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
}