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

feat: add api whose input option with style 'WithXXX' #230

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 18 additions & 1 deletion pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func RunPkgInPath(opts *opt.CompileOptions) (string, error) {
return compileResult.GetRawYamlResult(), nil
}

// CompileWithOpts will compile the kcl program without kcl package.
// CompileWithOpt will compile the kcl program without kcl package.
// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead.
func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
if len(opts.Entries()) > 0 {
for _, entry := range opts.Entries() {
Expand All @@ -84,6 +85,12 @@ func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
return kcl.RunWithOpts(*opts.Option)
}

// RunWithOpts will compile the kcl package with the compile options.
func RunWithOpts(opts ...opt.CompileOptions) (*kcl.KCLResultList, error) {
mergedOpts := opt.MergeOptions(opts...)
return runPkgWithOpt(&mergedOpts)
}

// getAbsInputPath will return the abs path of the file path described by '--input'.
// If the path exists after 'inputPath' is computed as a full path, it will be returned.
// If not, the kpm checks whether the full path of 'pkgPath/inputPath' exists,
Expand All @@ -103,6 +110,7 @@ func getAbsInputPath(pkgPath string, inputPath string) (string, error) {
}

// RunPkgWithOpt will compile the kcl package with the compile options.
// Deprecated: This method will not be maintained in the future. Use RunWithOpts instead.
func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
kpmcli, err := client.NewKpmClient()
kpmcli.SetNoSumCheck(opts.NoSumCheck())
Expand All @@ -112,6 +120,15 @@ func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
return run(kpmcli, opts)
}

func runPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
kpmcli, err := client.NewKpmClient()
kpmcli.SetNoSumCheck(opts.NoSumCheck())
if err != nil {
return nil, err
}
return run(kpmcli, opts)
}

// RunCurrentPkg will compile the current kcl package.
func RunCurrentPkg(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
pwd, err := os.Getwd()
Expand Down
16 changes: 16 additions & 0 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,19 @@ func TestRunWithNoSumCheck(t *testing.T) {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()
}

func TestRunPkgWithOpts(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")

result, err := RunWithOpts(
opt.WithNoSumCheck(false),
opt.WithEntries([]string{filepath.Join(pkgPath, "test_kcl", "main.k")}),
opt.WithKclOption(kcl.WithWorkDir(filepath.Join(pkgPath, "test_kcl"))),
)

assert.Equal(t, err, nil)
expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
assert.Equal(t, utils.RmNewline(string(result.GetRawYamlResult())), utils.RmNewline(string(expected)))
expectedJson, _ := os.ReadFile(filepath.Join(pkgPath, "expected.json"))
assert.Equal(t, utils.RmNewline(string(result.GetRawJsonResult())), utils.RmNewline(string(expectedJson)))
}
41 changes: 41 additions & 0 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,47 @@ type CompileOptions struct {
*kcl.Option
}

// MergeOptions will merge the input options.
func MergeOptions(opts ...CompileOptions) CompileOptions {
var opt = DefaultCompileOptions()
for _, o := range opts {
opt.Merge(*o.Option)
opt.isVendor = o.isVendor
opt.hasSettingsYaml = o.hasSettingsYaml
opt.entries = append(opt.entries, o.entries...)
opt.noSumCheck = o.noSumCheck
}
return *opt
}

// WithKclOption will add a kcl option to the compiler.
func WithKclOption(opt kcl.Option) CompileOptions {
var opts = DefaultCompileOptions()
opts.Merge(opt)
return *opts
}

// WithEntries will add entries to the compiler.
func WithEntries(entries []string) CompileOptions {
var opt = DefaultCompileOptions()
opt.entries = entries
return *opt
}

// WithEntry will add an entry to the compiler.
func WithVendor(isVendor bool) CompileOptions {
var opt = DefaultCompileOptions()
opt.isVendor = isVendor
return *opt
}

// WithNoSumCheck will set the 'no_sum_check' flag.
func WithNoSumCheck(is bool) CompileOptions {
var opt = DefaultCompileOptions()
opt.noSumCheck = is
return *opt
}

// DefaultCompileOptions returns a default CompileOptions.
func DefaultCompileOptions() *CompileOptions {
return &CompileOptions{
Expand Down
Loading