Skip to content

Commit

Permalink
Enhancement] Read env map from KCL option(env) function in KRM KCL Spec
Browse files Browse the repository at this point in the history
Signed-off-by: satyazzz123 <[email protected]>
  • Loading branch information
satyazzz123 committed Jan 24, 2024
1 parent 2e59400 commit 2279bb9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/edit/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -139,6 +146,23 @@ 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) {
v, err := resourceList.Pipe(yaml.Lookup("option", "env"))
if err != nil {
return "", errors.Wrap(err)
}

envMapOptionKCLValue, err := ToKCLValueString(v, "{}")
if err != nil {
return "", errors.Wrap(err)
}

return envMapOptionKCLValue, nil
}
3 changes: 3 additions & 0 deletions pkg/options/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down

0 comments on commit 2279bb9

Please sign in to comment.