Skip to content

Commit

Permalink
cmdr: add pkg/logz
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 18, 2024
1 parent a266cab commit 939a1fc
Show file tree
Hide file tree
Showing 20 changed files with 280 additions and 99 deletions.
4 changes: 2 additions & 2 deletions builder/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/conf"
logz "github.com/hedzr/logg/slog"
"github.com/hedzr/cmdr/v2/pkg/logz"
)

type appS struct {
Expand Down Expand Up @@ -64,7 +64,7 @@ func (s *appS) Args() []string { return s.args }
func (s *appS) Build() {
if sr, ok := s.Runner.(setRoot); ok {
ctx := context.Background()
logz.DebugContext(ctx, "[cmdr] builder.appS.Build() - setRoot")
logz.DebugContext(ctx, "builder.appS.Build() - setRoot")
if cx, ok := s.root.Cmd.(*cli.CmdS); ok {
cx.EnsureTree(ctx, s, s.root)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/atoa/r1.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"

"github.com/hedzr/evendeep/ref"
logz "github.com/hedzr/logg/slog"

"github.com/hedzr/cmdr/v2/pkg/exec"
"github.com/hedzr/cmdr/v2/pkg/logz"
)

//
Expand Down
6 changes: 3 additions & 3 deletions cli/atoa/stringtoany.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"time"

"github.com/hedzr/evendeep/ref"
logz "github.com/hedzr/logg/slog"

"github.com/hedzr/cmdr/v2/internal/tool"
"github.com/hedzr/cmdr/v2/pkg/exec"
"github.com/hedzr/cmdr/v2/pkg/logz"
)

// Parse convert a string to any type following a given meme.
Expand Down Expand Up @@ -107,7 +107,7 @@ func (s *toS) parseImpl(str string, rt reflect.Type, meme any) (v any, err error
rt = rt.Elem() //nolint:revive
rv := reflect.New(rt)
ctx := context.Background()
logz.DebugContext(ctx, "[cmdr] toS.parseImpl - rv", "rv", ref.Valfmt(&rv))
logz.DebugContext(ctx, "toS.parseImpl - rv", "rv", ref.Valfmt(&rv))
err = rv.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(str))
if err == nil {
v = rv.Interface()
Expand Down Expand Up @@ -204,7 +204,7 @@ func (s *toS) parseArray(preferKind reflect.Kind, typArray reflect.Type, str str
position, v, err = s.stepComplexObject(preferKind, typArray, runes, pos, meme)
if position == pos || position < len(runes) {
ctx := context.Background()
logz.WarnContext(ctx, "[cmdr] the given string is empty or has too much data?", "str", str, "posAfterParsed", position)
logz.WarnContext(ctx, "the given string is empty or has too much data?", "str", str, "posAfterParsed", position)
}
return
}
Expand Down
42 changes: 21 additions & 21 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

"github.com/hedzr/is"
"github.com/hedzr/is/states"
logz "github.com/hedzr/logg/slog"

"github.com/hedzr/cmdr/v2/internal/tool"
"github.com/hedzr/cmdr/v2/pkg/dir"
"github.com/hedzr/cmdr/v2/pkg/exec"
"github.com/hedzr/cmdr/v2/pkg/logz"

"gopkg.in/hedzr/errors.v3"
)
Expand Down Expand Up @@ -310,12 +310,12 @@ func (c *CmdS) SetAction(fn OnInvokeHandler) {
func (c *CmdS) Invoke(ctx context.Context, args []string) (err error) {
var deferActions func(errInvoked error)
if deferActions, err = c.RunPreActions(ctx, c, args); err != nil {
logz.VerboseContext(ctx, "[cmdr] cmd.RunPreActions failed", "err", err)
logz.VerboseContext(ctx, "cmd.RunPreActions failed", "err", err)
return
}
defer func() { deferActions(err) }() // err must be delayed caught here

logz.VerboseContext(ctx, "[cmdr] cmd.Invoke()", "onInvoke", c.onInvoke)
logz.VerboseContext(ctx, "cmd.Invoke()", "onInvoke", c.onInvoke)
if c.onInvoke != nil {
err = c.onInvoke(ctx, c, args)
}
Expand Down Expand Up @@ -370,7 +370,7 @@ func (c *CmdS) getDeferAction(ctx context.Context, cmd Cmd, args []string) func(
}

if !ecp.IsEmpty() {
logz.Panic("[cmdr] Error(s) occurred when running post-actions:", "error", ecp.Error())
logz.Panic("Error(s) occurred when running post-actions:", "error", ecp.Error())
}
}
}
Expand All @@ -388,7 +388,7 @@ func (c *CmdS) getDeferAction(ctx context.Context, cmd Cmd, args []string) func(
// ecp.Attach(c.postInvoke(cmd, args, errInvoked))
// }
// if !ecp.IsEmpty() {
// logz.Fatalf("[cmdr] Error(s) occurred when running post-actions: %v", ecp)
// logz.Fatalf("Error(s) occurred when running post-actions: %v", ecp)
// }
// return
// }
Expand All @@ -405,7 +405,7 @@ func (c *CmdS) getDeferAction(ctx context.Context, cmd Cmd, args []string) func(
// This function will be called for running time once (see also cmdr.Run()).
func (c *CmdS) EnsureTree(ctx context.Context, app App, root *RootCommand) {
if atomic.CompareAndSwapInt32(&root.linked, 0, 1) {
logz.DebugContext(ctx, "[cmdr] cmd.EnsureTree (Once) -> linking to root and owner", "root", root)
logz.DebugContext(ctx, "cmd.EnsureTree (Once) -> linking to root and owner", "root", root)
c.ensureTreeAlways(ctx, app, root)
}
}
Expand All @@ -415,7 +415,7 @@ func (c *CmdS) EnsureTree(ctx context.Context, app App, root *RootCommand) {
//
// This func is called only in building command system (see also builder.postBuild).
func (c *CmdS) EnsureTreeAlways(ctx context.Context, app App, root *RootCommand) {
logz.DebugContext(ctx, "[cmdr] cmd.EnsureTreeAlways -> linking to root and owner", "root", root)
logz.DebugContext(ctx, "cmd.EnsureTreeAlways -> linking to root and owner", "root", root)
c.ensureTreeAlways(ctx, app, root)
}

Expand Down Expand Up @@ -514,7 +514,7 @@ func (c *CmdS) ensureXrefFlags(ctx context.Context) { //nolint:revive
ff.ensureXref()
if ff.headLike {
if ff.owner.HeadLikeFlag() != nil && ff.owner.HeadLikeFlag() != ff {
logz.WarnContext(ctx, "[cmdr] too much head-like flags", "last-head-like-flag", ff.owner.HeadLikeFlag(), "this-one", ff)
logz.WarnContext(ctx, "too much head-like flags", "last-head-like-flag", ff.owner.HeadLikeFlag(), "this-one", ff)
}
ff.owner.SetHeadLikeFlag(ff)
}
Expand All @@ -530,7 +530,7 @@ func (c *CmdS) ensureXrefFlags(ctx context.Context) { //nolint:revive
ff.ensureXref()
if ff.headLike {
if ff.owner.HeadLikeFlag() != nil && ff.owner.HeadLikeFlag() != ff {
logz.WarnContext(ctx, "[cmdr] too much head-like flags", "last-head-like-flag", ff.owner.HeadLikeFlag(), "this-one", ff)
logz.WarnContext(ctx, "too much head-like flags", "last-head-like-flag", ff.owner.HeadLikeFlag(), "this-one", ff)
}
ff.owner.SetHeadLikeFlag(ff)
}
Expand Down Expand Up @@ -599,68 +599,68 @@ func (c *CmdS) invokeExternalEditor(ctx context.Context, vp *FlagValuePkg, ff *F
}
}

logz.DebugContext(ctx, "[cmdr] external editor", "ex-editor", ff.externalEditor)
logz.DebugContext(ctx, "external editor", "ex-editor", ff.externalEditor)
if cmd := os.Getenv(ff.externalEditor); cmd != "" {
file := tool.TempFileName("message*.tmp", "message001.tmp", c.App().Name())
cmdS := tool.SplitCommandString(cmd)
cmdS = append(cmdS, file)
defer func(dst string) {
if err := dir.DeleteFile(dst); err != nil {
logz.ErrorContext(ctx, "[cmdr] cannot delete temporary file for flag", "flag", ff)
logz.ErrorContext(ctx, "cannot delete temporary file for flag", "flag", ff)
}
}(file)

logz.DebugContext(ctx, "[cmdr] invoke external editor", "ex-editor", ff.externalEditor, "cmd", cmdS)
logz.DebugContext(ctx, "invoke external editor", "ex-editor", ff.externalEditor, "cmd", cmdS)
if is.DebuggerAttached() {
vp.ValueOK, vp.Value = true, "<<stdoutTextForDebugging>>"
logz.WarnContext(ctx, "[cmdr] use debug text", "flag", ff, "text", vp.Value)
logz.WarnContext(ctx, "use debug text", "flag", ff, "text", vp.Value)
return ff
}

if err := exec.CallSliceQuiet([]string{"which", cmdS[0]}, func(retCode int, stdoutText string) {
if retCode == 0 {
cmdS[0] = strings.TrimSpace(strings.TrimSuffix(stdoutText, "\n"))
logz.DebugContext(ctx, "[cmdr] got external editor real-path", "cmd", cmdS)
logz.DebugContext(ctx, "got external editor real-path", "cmd", cmdS)
}
}); err != nil {
logz.ErrorContext(ctx, "[cmdr] cannot invoke which CmdS", "flag", ff, "cmd", cmdS)
logz.ErrorContext(ctx, "cannot invoke which CmdS", "flag", ff, "cmd", cmdS)
return nil
}

var content []byte
var err error
content, err = tool.LaunchEditorWithGetter(cmdS[0], func() string { return cmdS[1] }, false)
if err != nil {
logz.ErrorContext(ctx, "[cmdr] Error on launching cmd", "err", err, "cmd", cmdS)
logz.ErrorContext(ctx, "Error on launching cmd", "err", err, "cmd", cmdS)
return nil
}

// content, err = tool.LaunchEditorWith(cmdS[0], cmdS[1])
// if err != nil {
// logz.ErrorContext(ctx, "[cmdr] Error on launching cmd", "err", err, "cmd", cmdS)
// logz.ErrorContext(ctx, "Error on launching cmd", "err", err, "cmd", cmdS)
// return nil
// }
//
// content, err = tool.LaunchEditor(cmdS[0])
// if err != nil {
// logz.ErrorContext(ctx, "[cmdr] Error on launching cmd", "err", err, "cmd", cmdS)
// logz.ErrorContext(ctx, "Error on launching cmd", "err", err, "cmd", cmdS)
// return nil
// }

// f, err = os.Open(file)
// if err != nil {
// logz.ErrorContext(ctx, "[cmdr] cannot open temporary file for reading content", "file", file, "flag", ff, "cmd", cmdS)
// logz.ErrorContext(ctx, "cannot open temporary file for reading content", "file", file, "flag", ff, "cmd", cmdS)
// return nil
// }
// defer f.Close()
// vp.ValueOK, vp.Value = true, dir.MustReadAll(f)

vp.ValueOK, vp.Value = true, string(content)
ff.defaultValue = string(content)
// logz.DebugContext(ctx, "[cmdr] invoked external editor", "ex-editor", ff.externalEditor, "text", string(content))
// logz.DebugContext(ctx, "invoked external editor", "ex-editor", ff.externalEditor, "text", string(content))
return ff
}
logz.WarnContext(ctx, "[cmdr] Unknown External Editor for flag.", "ex-editor", ff.externalEditor, "flag", ff)
logz.WarnContext(ctx, "Unknown External Editor for flag.", "ex-editor", ff.externalEditor, "flag", ff)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cli/examples/more.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/hedzr/is"
"github.com/hedzr/is/term"
logz "github.com/hedzr/logg/slog" //nolint:gci

"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/pkg/dir"
"github.com/hedzr/cmdr/v2/pkg/logz"
"github.com/hedzr/cmdr/v2/pkg/text"
)

Expand Down
2 changes: 2 additions & 0 deletions cli/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ type Cmd interface {
HitTitle() string
HitTimes() int

SetRedirectTo(dottedPath string)

CanInvoke() bool
Invoke(ctx context.Context, args []string) (err error)

Expand Down
5 changes: 3 additions & 2 deletions cli/worker/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"runtime"

"github.com/hedzr/is/states"
logz "github.com/hedzr/logg/slog"
logzorig "github.com/hedzr/logg/slog"

"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/pkg/logz"
)

func (w *workerS) addBuiltinCommands(root *cli.RootCommand) (err error) { //nolint:unparam //unified form
Expand Down Expand Up @@ -84,7 +85,7 @@ func (w *workerS) builtinHelps(app cli.App, p *cli.CmdS) { //nolint:revive
Hidden(true, false).
OnMatched(func(c cli.Cmd, position int, hitState *cli.MatchState) (err error) { //nolint:revive
w.inCompleting = true
logz.SetLevel(logz.ErrorLevel) // disable trace, debug, info, warn messages
logz.SetLevel(logzorig.ErrorLevel) // disable trace, debug, info, warn messages
return
}).
OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) {
Expand Down
9 changes: 4 additions & 5 deletions cli/worker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (

errorsv3 "gopkg.in/hedzr/errors.v3"

logz "github.com/hedzr/logg/slog"

"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/pkg/logz"
)

func (w *workerS) exec(ctx context.Context, pc *parseCtx) (err error) {
Expand All @@ -35,9 +34,9 @@ func (w *workerS) exec(ctx context.Context, pc *parseCtx) (err error) {
}()

if !forceDefaultAction && lastCmd.CanInvoke() {
logz.VerboseContext(ctx, "[cmdr] invoke action of cmd, with args", "cmd", lastCmd, "args", pc.positionalArgs)
logz.VerboseContext(ctx, "invoke action of cmd, with args", "cmd", lastCmd, "args", pc.positionalArgs)
err = lastCmd.Invoke(ctx, pc.positionalArgs)
logz.VerboseContext(ctx, "[cmdr] invoke action ends.", "err", err)
logz.VerboseContext(ctx, "invoke action ends.", "err", err)
if !w.errIsSignalFallback(err) {
return
}
Expand All @@ -47,7 +46,7 @@ func (w *workerS) exec(ctx context.Context, pc *parseCtx) (err error) {
handled, err1 := w.handleActions(ctx, pc)
// for k, action := range w.actions {
// if k&w.actionsMatched != 0 {
// logz.VerboseContext(ctx, "[cmdr] Invoking worker.actionsMatched", "hit-action", k, "actions", w.Actions())
// logz.VerboseContext(ctx, "Invoking worker.actionsMatched", "hit-action", k, "actions", w.Actions())
// err, handled = action(pc, lastCmd), true
// break
// }
Expand Down
16 changes: 8 additions & 8 deletions cli/worker/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

errorsv3 "gopkg.in/hedzr/errors.v3"

logz "github.com/hedzr/logg/slog"
"github.com/hedzr/cmdr/v2/pkg/logz"

"github.com/hedzr/cmdr/v2/cli"
)
Expand All @@ -29,7 +29,7 @@ func (w *workerS) parse(ctx context.Context, pc *parseCtx) (err error) { //nolin
}
}()

logz.VerboseContext(ctx, "[cmdr] parsing command line args ...", "args", w.args)
logz.VerboseContext(ctx, "parsing command line args ...", "args", w.args)

loopArgs:
for pc.i = 1; pc.i < len(w.args); pc.i++ {
Expand All @@ -39,11 +39,11 @@ loopArgs:

if atomic.LoadInt32(&pc.passThruMatched) > 0 || errorsv3.Is(err, cli.ErrShouldStop) || w.errIsUnmatchedArg(err) {
pc.positionalArgs = append(pc.positionalArgs, w.args[pc.i])
logz.VerboseContext(ctx, "[cmdr] positional args added", "i", pc.i, "args", pc.positionalArgs)
logz.VerboseContext(ctx, "positional args added", "i", pc.i, "args", pc.positionalArgs)
continue
}

logz.VerboseContext(ctx, "[cmdr] parsing command-line args", "i", pc.i, "arg", w.args[pc.i])
logz.VerboseContext(ctx, "parsing command-line args", "i", pc.i, "arg", w.args[pc.i])

pc.arg, pc.short, pc.pos = w.args[pc.i], false, 0
switch c1 := pc.arg[0]; c1 {
Expand Down Expand Up @@ -72,7 +72,7 @@ loopArgs:
}
// single '+': as a positional arg
pc.positionalArgs = append(pc.positionalArgs, pc.arg)
logz.VerboseContext(ctx, "[cmdr] positional args added", "i", pc.i, "args", pc.positionalArgs)
logz.VerboseContext(ctx, "positional args added", "i", pc.i, "args", pc.positionalArgs)
continue

case '-', '~':
Expand Down Expand Up @@ -116,7 +116,7 @@ loopArgs:
default:
if pc.NoCandidateChildCommands() {
pc.positionalArgs = append(pc.positionalArgs, pc.arg)
logz.VerboseContext(ctx, "[cmdr] positional args added", "i", pc.i, "args", pc.positionalArgs)
logz.VerboseContext(ctx, "positional args added", "i", pc.i, "args", pc.positionalArgs)
continue
}
if err = w.matchCommand(ctx, pc); !w.errIsSignalOrNil(err) {
Expand Down Expand Up @@ -161,7 +161,7 @@ func (w *workerS) matchCommand(ctx context.Context, pc *parseCtx) (err error) {
if err == nil {
pc.lastCommand, err = len(pc.matchedCommands)-1, nil
}
logz.VerboseContext(ctx, "[cmdr] command matched", "short", short, "cmd", pc.LastCmd(), "handled", handled)
logz.VerboseContext(ctx, "command matched", "short", short, "cmd", pc.LastCmd(), "handled", handled)
}
return
}
Expand All @@ -176,7 +176,7 @@ compactFlags:
if vp.Matched != "" && ff != nil && w.errIsSignalOrNil(err1) {
ms, handled := pc.addFlag(ff), false
handled, err1 = ff.TryOnMatched(0, ms)
logz.VerboseContext(ctx, "[cmdr] flag matched", "short", vp.Short, "flg", ff, "val-pkg-val", ff.DefaultValue(), "handled", handled)
logz.VerboseContext(ctx, "flag matched", "short", vp.Short, "flg", ff, "val-pkg-val", ff.DefaultValue(), "handled", handled)

pc.i += vp.AteArgs
vp.AteArgs = 0
Expand Down
2 changes: 1 addition & 1 deletion cli/worker/parse_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync/atomic"

"github.com/hedzr/cmdr/v2/cli"
logz "github.com/hedzr/logg/slog"
"github.com/hedzr/cmdr/v2/pkg/logz"
)

type parseCtx struct {
Expand Down
Loading

0 comments on commit 939a1fc

Please sign in to comment.