diff --git a/pkg/edit/bootstrap.go b/pkg/edit/bootstrap.go index 90ff054..103367f 100644 --- a/pkg/edit/bootstrap.go +++ b/pkg/edit/bootstrap.go @@ -128,6 +128,13 @@ func constructOptions(resourceList *yaml.RNode) (*options.RunOptions, error) { } // 4. Read environment variables. pathOptionKCLValue := os.Getenv("PATH") + + // read map on env + envMapOptionKCLValue, err := getEnvMapOptionKCLValue(resourceList) + if err != nil { + return nil, errors.Wrap(err) + } + opts := options.NewRunOptions() opts.NoStyle = true opts.Arguments = []string{ @@ -139,6 +146,37 @@ func constructOptions(resourceList *yaml.RNode) (*options.RunOptions, error) { fmt.Sprintf("%s=%s", paramsOptionName, paramsOptionKCLValue), // environment variable example (PATH) fmt.Sprintf("PATH=%s", pathOptionKCLValue), + // environment map example (option("env")) + fmt.Sprintf("env=%s", envMapOptionKCLValue), } return opts, nil } + +// getEnvMapOptionKCLValue retrieves the environment map from the KCL 'option("env")' function. +func getEnvMapOptionKCLValue(resourceList *yaml.RNode) (string, error) { + + envMap := make(map[string]string) + env := os.Environ() + for _, e := range env { + pair := strings.SplitN(e, "=", 2) + envMap[pair[0]] = pair[1] + } + + envMapInterface := make(map[string]interface{}) + for k, v := range envMap { + envMapInterface[k] = v + } + + v, err := yaml.FromMap(envMapInterface) + if err != nil { + return "", errors.Wrap(err) + } + + // 4. Convert the YAML RNode to its KCL value string representation. + envMapOptionKCLValue, err := ToKCLValueString(v, emptyConfig) + if err != nil { + return "", errors.Wrap(err) + } + + return envMapOptionKCLValue, nil +} diff --git a/pkg/options/run.go b/pkg/options/run.go index c997381..e80b08c 100644 --- a/pkg/options/run.go +++ b/pkg/options/run.go @@ -16,12 +16,15 @@ type RunOptions struct { OutputPath string // Environment variable example (PATH) PathEnvVar string + // Environment map from KCL option("env") + EnvMap map[string]string } // RunOptions creates a new options for the run command. func NewRunOptions() *RunOptions { return &RunOptions{ PathEnvVar: os.Getenv("PATH"), + EnvMap: make(map[string]string), } }