Skip to content

Commit

Permalink
Merge pull request #30 from satyazzz123/satyazzz123/MAP
Browse files Browse the repository at this point in the history
enable read env map from KCL option("env") function
  • Loading branch information
Peefy authored Jan 24, 2024
2 parents 2e59400 + 90383b8 commit 9cfe827
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
38 changes: 38 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,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
}
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 9cfe827

Please sign in to comment.