Skip to content

Commit

Permalink
Merge pull request #256 from zong-zhe/add-with-logger
Browse files Browse the repository at this point in the history
feat: add 'WithLogWriter' for api 'RunWithOpts'
  • Loading branch information
Peefy authored Jan 29, 2024
2 parents 194168a + 7552f2b commit 5e4483a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 80 deletions.
9 changes: 6 additions & 3 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down
30 changes: 30 additions & 0 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -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(), "")
}
Original file line number Diff line number Diff line change
@@ -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" }
45 changes: 45 additions & 0 deletions pkg/api/test_data/test_run_pkg_in_path/test_run_with_no_log/main.k
Original file line number Diff line number Diff line change
@@ -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"
}
}
65 changes: 24 additions & 41 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 0 additions & 36 deletions pkg/opt/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 5e4483a

Please sign in to comment.