diff --git a/pkg/api/kpm_run.go b/pkg/api/kpm_run.go index aa556285..8e954462 100644 --- a/pkg/api/kpm_run.go +++ b/pkg/api/kpm_run.go @@ -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) { if len(opts.Entries()) > 0 { for _, entry := range opts.Entries() { @@ -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, @@ -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()) @@ -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() diff --git a/pkg/api/kpm_run_test.go b/pkg/api/kpm_run_test.go index 36899f29..91b14be3 100644 --- a/pkg/api/kpm_run_test.go +++ b/pkg/api/kpm_run_test.go @@ -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))) +} diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index f7375ba7..b039d37f 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -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{