Skip to content

Commit

Permalink
feat: support logger opts to merge (#186)
Browse files Browse the repository at this point in the history
feat: support logger opts to mergs

Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Nov 27, 2023
1 parent 17b297a commit 0b7e384
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion pkg/kcl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ func run(pathList []string, opts ...Option) (*KCLResultList, error) {
// Output log message
logger := args.GetLogger()
if logger != nil && resp.LogMessage != "" {
logger.Info(resp.LogMessage)
_, err := logger.Write([]byte(resp.LogMessage))
if err != nil {
return nil, err
}
}
if resp.ErrMessage != "" {
return nil, errors.New(resp.ErrMessage)
Expand Down
9 changes: 5 additions & 4 deletions pkg/kcl/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ package kcl
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"kcl-lang.io/kcl-go/pkg/logger"
"kcl-lang.io/kcl-go/pkg/settings"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
"kcl-lang.io/kcl-go/pkg/tools/override"
)

type Option struct {
*gpyrpc.ExecProgram_Args
logger logger.Logger
logger io.Writer
Err error
}

Expand All @@ -36,7 +36,7 @@ func (p *Option) JSONString() string {
return string(x)
}

func (p *Option) GetLogger() logger.Logger {
func (p *Option) GetLogger() io.Writer {
return p.logger
}

Expand Down Expand Up @@ -79,7 +79,7 @@ func ParseArgs(pathList []string, opts ...Option) (Option, error) {
return *args, nil
}

func WithLogger(l logger.Logger) Option {
func WithLogger(l io.Writer) Option {
var opt = NewOption()
opt.logger = l
return *opt
Expand Down Expand Up @@ -279,6 +279,7 @@ func (p *Option) Merge(opts ...Option) *Option {
if opt.ExternalPkgs != nil {
p.ExternalPkgs = append(p.ExternalPkgs, opt.ExternalPkgs...)
}
p.logger = opt.logger
}
return p
}

0 comments on commit 0b7e384

Please sign in to comment.