Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable read env map from KCL option("env") function #30

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
Peefy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", errors.Wrap(err)
}

envMapOptionKCLValue, err := ToKCLValueString(v, "{}")
Peefy marked this conversation as resolved.
Show resolved Hide resolved
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
Loading