-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: add code runner for configuration code execution
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package run | ||
|
||
import ( | ||
kcl "kcl-lang.io/kcl-go" | ||
kclpkg "kcl-lang.io/kcl-go/pkg/kcl" | ||
"kcl-lang.io/kpm/pkg/api" | ||
"kcl-lang.io/kpm/pkg/opt" | ||
) | ||
|
||
// CodeRunner compiles and runs the target DSL based configuration code | ||
// and returns configuration data in plain format. | ||
type CodeRunner interface { | ||
Run(workingDir string, arguments map[string]string) ([]byte, error) | ||
} | ||
|
||
// KPMRunner should implement the CodeRunner interface. | ||
var _ CodeRunner = &KPMRunner{} | ||
|
||
// KPMRunner implements the CodeRunner interface. | ||
type KPMRunner struct{} | ||
|
||
// Run calls KPM api to compile and run KCL based configuration code. | ||
func (r *KPMRunner) Run(workingDir string, arguments map[string]string) ([]byte, error) { | ||
optList, err := buildKCLOptions(workingDir, arguments) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result, err := api.RunWithOpts( | ||
opt.WithKclOption(*kclpkg.NewOption().Merge(optList...)), | ||
opt.WithNoSumCheck(true), | ||
opt.WithLogWriter(nil), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []byte(result.GetRawYamlResult()), nil | ||
} | ||
|
||
// buildKCLOptions returns list of KCL options. | ||
func buildKCLOptions(workingDir string, arguments map[string]string) ([]kcl.Option, error) { | ||
optList := make([]kcl.Option, 0) | ||
|
||
// build arguments option | ||
for k, v := range arguments { | ||
argStr := k + "=" + v | ||
withOpt := kcl.WithOptions(argStr) | ||
if withOpt.Err != nil { | ||
return nil, withOpt.Err | ||
} | ||
|
||
optList = append(optList, withOpt) | ||
} | ||
|
||
// build workDir option | ||
withOpt := kcl.WithWorkDir(workingDir) | ||
if withOpt.Err != nil { | ||
return nil, withOpt.Err | ||
} | ||
optList = append(optList, withOpt) | ||
|
||
// eliminate null values in the result | ||
withOpt = kcl.WithDisableNone(true) | ||
if withOpt.Err != nil { | ||
return nil, withOpt.Err | ||
} | ||
optList = append(optList, withOpt) | ||
|
||
return optList, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package run |