diff --git a/pkg/api/kpm_run.go b/pkg/api/kpm_run.go index 8e954462..3450f2cc 100644 --- a/pkg/api/kpm_run.go +++ b/pkg/api/kpm_run.go @@ -86,9 +86,12 @@ func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) { } // 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) +func RunWithOpts(opts ...opt.Option) (*kcl.KCLResultList, error) { + mergedOpts := opt.DefaultCompileOptions() + for _, opt := range opts { + opt(mergedOpts) + } + return runPkgWithOpt(mergedOpts) } // getAbsInputPath will return the abs path of the file path described by '--input'. diff --git a/pkg/api/kpm_run_test.go b/pkg/api/kpm_run_test.go index abf95598..d94723fe 100644 --- a/pkg/api/kpm_run_test.go +++ b/pkg/api/kpm_run_test.go @@ -1,6 +1,7 @@ package api import ( + "bytes" "fmt" "os" "path/filepath" @@ -214,3 +215,32 @@ func TestRunWithOptsAndNoSumCheck(t *testing.T) { assert.Equal(t, err, nil) } } + +func TestRunWithOptsWithNoLog(t *testing.T) { + pkgPath := filepath.Join(getTestDir("test_run_pkg_in_path"), "test_run_with_no_log") + + defer func() { + _ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock")) + }() + + old := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + + pathMainK := filepath.Join(pkgPath, "main.k") + + _, err := RunWithOpts( + opt.WithLogWriter(nil), + opt.WithEntries([]string{pathMainK}), + opt.WithKclOption(kcl.WithWorkDir(pkgPath)), + ) + + assert.Equal(t, err, nil) + os.Stdout = old + w.Close() + var buf bytes.Buffer + _, err = buf.ReadFrom(r) + assert.Equal(t, err, nil) + + assert.Equal(t, buf.String(), "") +} diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_with_no_log/kcl.mod b/pkg/api/test_data/test_run_pkg_in_path/test_run_with_no_log/kcl.mod new file mode 100644 index 00000000..8f27a88e --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_with_no_log/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "dep_git_commit" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +catalog = { git = "https://github.com/KusionStack/catalog.git", commit = "a29e3db" } diff --git a/pkg/api/test_data/test_run_pkg_in_path/test_run_with_no_log/main.k b/pkg/api/test_data/test_run_pkg_in_path/test_run_with_no_log/main.k new file mode 100644 index 00000000..320a5761 --- /dev/null +++ b/pkg/api/test_data/test_run_pkg_in_path/test_run_with_no_log/main.k @@ -0,0 +1,45 @@ +import catalog.models.schema.v1 as ac +import catalog.models.schema.v1.workload as wl +import catalog.models.schema.v1.workload.container as c +import catalog.models.schema.v1.workload.container.probe as p +import catalog.models.schema.v1.monitoring as m + +ac.AppConfiguration { + workload: wl.Service { + containers: { + "nginx": c.Container { + image: "nginx:v1" + # Run the following command as defined + command: ["/bin/sh", "-c", "echo hi"] + # Extra arguments append to command defined above + args: ["/bin/sh", "-c", "echo hi"] + env: { + # An environment variable of name "env1" and value "VALUE" will be set + "env1": "VALUE" + # An environment variable of name "env2" and value of the key "key" in the + # secret named "sec-name" will be set. + "env2": "secret://sec-name/key" + } + # Run the command "/bin/sh -c echo hi", as defined above, in the directory "/tmp" + workingDir: "/tmp" + # Configure a HTTP readiness probe + readinessProbe: p.Probe { + probeHandler: p.Http { + url: "http://localhost:80" + } + initialDelaySeconds: 10 + } + } + } + # Set the replicas + replicas: 2 + } + # Add the monitoring configuration backed by Prometheus + monitoring: m.Prometheus{ + interval: "30s" + timeout: "15s" + path: "/metrics" + port: "web" + scheme: "http" + } +} \ No newline at end of file diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index 1780e976..57e2a0ec 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -25,58 +25,41 @@ 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) - if o.writer != nil { - opt.writer = o.writer - } - - if o.isVendor { - opt.isVendor = o.isVendor - } - - if o.hasSettingsYaml { - opt.hasSettingsYaml = o.hasSettingsYaml - } - - if o.noSumCheck { - opt.noSumCheck = o.noSumCheck - } - - opt.entries = append(opt.entries, o.entries...) - } - return *opt -} +type Option func(*CompileOptions) // WithKclOption will add a kcl option to the compiler. -func WithKclOption(opt kcl.Option) CompileOptions { - var opts = DefaultCompileOptions() - opts.Merge(opt) - return *opts +func WithKclOption(opt kcl.Option) Option { + return func(opts *CompileOptions) { + opts.Merge(opt) + } } // WithEntries will add entries to the compiler. -func WithEntries(entries []string) CompileOptions { - var opt = DefaultCompileOptions() - opt.entries = entries - return *opt +func WithEntries(entries []string) Option { + return func(opts *CompileOptions) { + opts.entries = append(opts.entries, entries...) + } } // WithEntry will add an entry to the compiler. -func WithVendor(isVendor bool) CompileOptions { - var opt = DefaultCompileOptions() - opt.isVendor = isVendor - return *opt +func WithVendor(isVendor bool) Option { + return func(opts *CompileOptions) { + opts.isVendor = isVendor + } } // WithNoSumCheck will set the 'no_sum_check' flag. -func WithNoSumCheck(is bool) CompileOptions { - var opt = DefaultCompileOptions() - opt.noSumCheck = is - return *opt +func WithNoSumCheck(is bool) Option { + return func(opts *CompileOptions) { + opts.noSumCheck = is + } +} + +// WithLogWriter will set the log writer of the compiler. +func WithLogWriter(writer io.Writer) Option { + return func(opts *CompileOptions) { + opts.writer = writer + } } // DefaultCompileOptions returns a default CompileOptions. diff --git a/pkg/opt/opt_test.go b/pkg/opt/opt_test.go index 54dfeb88..a04ea6a0 100644 --- a/pkg/opt/opt_test.go +++ b/pkg/opt/opt_test.go @@ -20,39 +20,3 @@ func TestWorkDirAsPkgPath(t *testing.T) { opts.SetEntries([]string{"override.k"}) assert.Equal(t, opts.Entries(), []string{"override.k"}) } - -func TestCompilationMerge(t *testing.T) { - opts := MergeOptions( - WithEntries([]string{"test.k"}), - ) - assert.Equal(t, opts.Entries(), []string{"test.k"}) - assert.Equal(t, opts.NoSumCheck(), false) - assert.Equal(t, opts.IsVendor(), false) - - opts = MergeOptions( - opts, - WithNoSumCheck(true), - ) - - assert.Equal(t, opts.Entries(), []string{"test.k"}) - assert.Equal(t, opts.NoSumCheck(), true) - assert.Equal(t, opts.IsVendor(), false) - - opts = MergeOptions( - opts, - WithVendor(true), - ) - - assert.Equal(t, opts.Entries(), []string{"test.k"}) - assert.Equal(t, opts.NoSumCheck(), true) - assert.Equal(t, opts.IsVendor(), true) - - opts = MergeOptions( - opts, - WithEntries([]string{"test1.k"}), - ) - - assert.Equal(t, opts.Entries(), []string{"test.k", "test1.k"}) - assert.Equal(t, opts.NoSumCheck(), true) - assert.Equal(t, opts.IsVendor(), true) -}