Skip to content

Commit

Permalink
feat: add 'WithLogWriter' for api 'RunWithOpts'
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Jan 29, 2024
1 parent 194168a commit e0a1a4c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func RunWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
}

// RunWithOpts will compile the kcl package with the compile options.
// Note: When combining multiple options, the 'WithLogWriter' should be set at the end.
func RunWithOpts(opts ...opt.CompileOptions) (*kcl.KCLResultList, error) {
mergedOpts := opt.MergeOptions(opts...)
return runPkgWithOpt(&mergedOpts)
Expand Down
29 changes: 29 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,31 @@ 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.WithEntries([]string{pathMainK}),
opt.WithKclOption(kcl.WithWorkDir(pkgPath)),
opt.WithLogWriter(nil),
)

assert.Equal(t, err, nil)
os.Stdout = old
w.Close()
var buf bytes.Buffer
buf.ReadFrom(r)

Check failure on line 242 in pkg/api/kpm_run_test.go

View workflow job for this annotation

GitHub Actions / Lint checks

Error return value of `buf.ReadFrom` is not checked (errcheck)

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"
}
}
14 changes: 11 additions & 3 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ type CompileOptions struct {
}

// MergeOptions will merge the input options.
// When combining multiple options, the 'WithLogWriter' should be set at the end.
func MergeOptions(opts ...CompileOptions) CompileOptions {
var opt = DefaultCompileOptions()
for _, o := range opts {
opt.Merge(*o.Option)
if o.writer != nil {
opt.writer = o.writer
}
opt.writer = o.writer

if o.isVendor {
opt.isVendor = o.isVendor
Expand Down Expand Up @@ -79,6 +78,15 @@ func WithNoSumCheck(is bool) CompileOptions {
return *opt
}

// WithLogWriter will set the log writer of the compiler.
// When multiple log writers are set, the compiler will write to the last log writer.
// When combining multiple options, the log should be set at the end.
func WithLogWriter(writer io.Writer) CompileOptions {
var opt = DefaultCompileOptions()
opt.writer = writer
return *opt
}

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

0 comments on commit e0a1a4c

Please sign in to comment.