command
This commit is contained in:
117
internal/values.go
Normal file
117
internal/values.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"oc-k8s/utils"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Help_Values() {
|
||||
fmt.Println(`
|
||||
Values commands: oc-k8s <action> values
|
||||
create - Create a new values release yaml
|
||||
help - Show this help message
|
||||
|
||||
Usage:
|
||||
oc-k8s create values [release] [env_folder] [env_file]
|
||||
release - Release values name (required)
|
||||
env_folder - Helm config folder (optionnal, default: .)
|
||||
env_file - env to map (optionnal)
|
||||
oc-k8s help values `)
|
||||
}
|
||||
|
||||
var varPattern = regexp.MustCompile(`\$\{([A-Za-z_][A-Za-z0-9_]*)[:-]([^}]+)\}`)
|
||||
|
||||
func Create_Values(args ...string) error {
|
||||
folder := "."
|
||||
release := "dev"
|
||||
conf := ""
|
||||
|
||||
if len(args) > 0 {
|
||||
release = args[0]
|
||||
}
|
||||
if len(args) > 1 {
|
||||
folder = args[1]
|
||||
}
|
||||
if len(args) > 2 {
|
||||
conf = args[2]
|
||||
}
|
||||
return generateConfig(conf, release, folder)
|
||||
}
|
||||
|
||||
func generateConfig(confFile string, release string, output_folder string) error {
|
||||
// Load env file if provided
|
||||
var err error
|
||||
confs := map[string]string{}
|
||||
if confFile != "" {
|
||||
if confs, err = loadEnvFile(confFile); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
confFile = "no configuration file"
|
||||
}
|
||||
// Read template
|
||||
content, err := utils.ReadFS("assets/values.yaml.template")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rendered := renderTemplate(string(content), confs)
|
||||
|
||||
// Ensure output directory exists
|
||||
if err := os.MkdirAll(output_folder, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write output
|
||||
if err := os.WriteFile(output_folder+"/"+release+"-values.yaml", []byte(rendered), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Rendered %s from %s using %s\n", output_folder+"/"+release+"-values.yaml", "assets/values.yaml.template", confFile)
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadEnvFile(path string) (map[string]string, error) {
|
||||
envs := map[string]string{}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return envs, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(parts[0])
|
||||
val := strings.Trim(strings.TrimSpace(parts[1]), `"'`)
|
||||
|
||||
envs[key] = val
|
||||
}
|
||||
return envs, scanner.Err()
|
||||
}
|
||||
|
||||
func renderTemplate(input string, envs map[string]string) string {
|
||||
return varPattern.ReplaceAllStringFunc(input, func(match string) string {
|
||||
sub := varPattern.FindStringSubmatch(match)
|
||||
varName := sub[1]
|
||||
defaultVal := sub[2][1:]
|
||||
|
||||
if val, ok := envs[varName]; ok && val != "" {
|
||||
return val
|
||||
}
|
||||
return defaultVal
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user