Skip to content

Commit

Permalink
tmp-commit: impl dyn-commands in tiny-app, just a temp demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 13, 2024
1 parent 7c9ffcb commit 8fe95f2
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 4 deletions.
154 changes: 154 additions & 0 deletions tiny/litecmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package main

import (
"context"
"os"
"path"

"github.com/hedzr/cmdr/v2"
"github.com/hedzr/cmdr/v2/cli"
"github.com/hedzr/cmdr/v2/pkg/dir"
"github.com/hedzr/cmdr/v2/pkg/exec"
"github.com/hedzr/is/term/color"
)

func onEvalJumpSubCommands(ctx context.Context, c cli.BaseOptI) (it cli.EvalIterator, err error) {
files := make(map[string]*liteCmdS)
pos := 0
var keys []string

baseDir := path.Join("ci", "pkg", "usr.local.lib.tiny-app", "ext")
err = dir.ForFile(baseDir, func(depth int, dirName string, fi os.DirEntry) (stop bool, err error) {
if fi.Name()[0] == '.' {
return
}
key := path.Join(dirName, fi.Name())
files[key] = &liteCmdS{dirName: dirName, fi: fi, depth: depth, owner: c}
keys = append(keys, key)
return
})

it = func(ctx context.Context) (bo cli.BaseOptI, hasNext bool, err error) {
if pos < len(keys) {
key := keys[pos]
bo = files[key]
pos++
hasNext = pos < len(keys)
}
return
}
return
}

type liteCmdS struct {
dirName string
fi os.DirEntry
depth int
owner cli.BaseOptI
group string

hitTitle string
hitTimes int
}

func (s *liteCmdS) name() string { return s.fi.Name() }

func (s *liteCmdS) OwnerOrParent() cli.BacktraceableMin { return s.owner.(cli.Backtraceable) }
func (s *liteCmdS) OwnerIsNil() bool { return s.owner == nil }
func (s *liteCmdS) GetDottedPath() string { return cli.DottedPath(s) }
func (s *liteCmdS) GetTitleName() string { return s.name() }
func (s *liteCmdS) GetTitleNamesArray() []string { return []string{s.name()} }
func (s *liteCmdS) GetTitleNames() string { return s.name() }
func (s *liteCmdS) App() cli.App { return nil }

func (s *liteCmdS) String() string { return path.Join(s.dirName, s.name()) }

func (s *liteCmdS) FindSubCommand(ctx context.Context, longName string, wide bool) (res cli.BaseOptI) {
return
}
func (s *liteCmdS) Walk(ctx context.Context, cb cli.WalkCB) {
return
}
func (s *liteCmdS) WalkGrouped(ctx context.Context, cb cli.WalkGroupedCB) {
return
}
func (s *liteCmdS) WalkBackwardsCtx(ctx context.Context, cb cli.WalkBackwardsCB, pc *cli.WalkBackwardsCtx) {
return
}

func (s *liteCmdS) OwnerCmd() cli.BaseOptI { return s.owner }
func (s *liteCmdS) Root() *cli.RootCommand { return s.owner.Root() }
func (s *liteCmdS) Name() string { return s.name() }
func (s *liteCmdS) ShortName() string { return s.name() }
func (s *liteCmdS) ShortNames() []string { return []string{s.name()} }
func (s *liteCmdS) AliasNames() []string { return nil }
func (s *liteCmdS) Desc() string { return s.String() }
func (s *liteCmdS) DescLong() string { return "" }
func (s *liteCmdS) Examples() string { return "" }
func (s *liteCmdS) TailPlaceHolder() string { return "" }
func (s *liteCmdS) GetCommandTitles() string { return s.name() }

func (s *liteCmdS) GroupTitle() string { return cmdr.RemoveOrderedPrefix(s.SafeGroup()) }
func (s *liteCmdS) GroupHelpTitle() string { return s.GroupTitle() }
func (s *liteCmdS) SafeGroup() string {
if s.group == "" {
return cli.UnsortedGroup
}
return s.group
}
func (s *liteCmdS) AllGroupKeys(chooseFlag, sort bool) []string { return nil }
func (s *liteCmdS) Hidden() bool { return false }
func (s *liteCmdS) VendorHidden() bool { return false }
func (s *liteCmdS) Deprecated() string { return "" }
func (s *liteCmdS) CountOfCommands() int { return 0 }
func (s *liteCmdS) CommandsInGroup(groupTitle string) (list []cli.BaseOptI) { return nil }
func (s *liteCmdS) FlagsInGroup(groupTitle string) (list []*cli.Flag) { return nil }
func (s *liteCmdS) Flags() []*cli.Flag { return nil }
func (s *liteCmdS) SubCommands() []*cli.Command { return nil }
func (s *liteCmdS) Invoke(ctx context.Context, args []string) (err error) {
err = exec.Run("sh", "-c", s.fi.Name())
return
}
func (s *liteCmdS) OnEvalSubcommands() cli.OnEvaluateSubCommands {
return nil
}
func (s *liteCmdS) OnEvalSubcommandsOnce() cli.OnEvaluateSubCommands {
return nil
}
func (s *liteCmdS) OnEvalSubcommandsOnceInvoked() bool {
return false
}
func (s *liteCmdS) OnEvalSubcommandsOnceCache() []cli.BaseOptI {
return nil
}
func (s *liteCmdS) OnEvalSubcommandsOnceSetCache(list []cli.BaseOptI) {
}

func (s *liteCmdS) OnEvalFlags() cli.OnEvaluateFlags {
return nil
}
func (s *liteCmdS) OnEvalFlagsOnce() cli.OnEvaluateFlags {
return nil
}
func (s *liteCmdS) OnEvalFlagsOnceInvoked() bool {
return false
}
func (s *liteCmdS) OnEvalFlagsOnceCache() []*cli.Flag {
return nil
}
func (s *liteCmdS) OnEvalFlagsOnceSetCache(list []*cli.Flag) {
}

func (s *liteCmdS) DeprecatedHelpString(trans func(ss string, clr color.Color) string, clr, clrDefault color.Color) (hs, plain string) {
return
}

func (s *liteCmdS) SetHitTitle(title string) {
s.hitTitle = title
s.hitTimes++
}
func (s *liteCmdS) HitTitle() string { return s.hitTitle }
func (s *liteCmdS) HitTimes() int { return s.hitTimes }
func (s *liteCmdS) ForeachFlags(func(f *cli.Flag) (stop bool)) (stop bool) {
return
}
4 changes: 0 additions & 4 deletions tiny/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func main() {
}
}

func onEvalJumpSubCommands(ctx context.Context, c *cli.Command) (it cli.EvalIterator, err error) {
return
}

func prepareApp(opts ...cli.Opt) (app cli.App) {
app = cmdr.New(opts...).
Info("tiny-app", "0.3.1").
Expand Down

0 comments on commit 8fe95f2

Please sign in to comment.