appcmd

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AppCheckCmd = &gcli.Command{
	Name: "check",
	Desc: "check the kite app runtime env information",
	Func: func(c *gcli.Command, args []string) error {

		baseDir := app.App().BaseDir
		confDir := app.App().ConfigDir
		dotenvFile := fsutil.JoinPaths(baseDir, appconst.DotEnvFileName)

		infos := []struct {
			title  string
			answer string
		}{
			{
				"Kite base data dir: " + baseDir,
				fmt.Sprintf("Exists: %v", fsutil.IsDir(baseDir)),
			},
			{
				"Kite dotenv file: " + dotenvFile,
				fmt.Sprintf("Exists: %v", fsutil.IsFile(dotenvFile)),
			},
			{
				"Kite config directory: " + confDir,
				fmt.Sprintf("Exists: %v", fsutil.IsDir(confDir)),
			},
		}

		for _, info := range infos {
			c.Println("-", info.title)
			c.Infoln(">", info.answer)
		}

		return nil
	},
}

AppCheckCmd command

View Source
var (

	// AppExtAddCmd 添加应用扩展添加命令
	AppExtAddCmd = &gcli.Command{
		Name: "add",
		Desc: "Add a new kite extension",
		Config: func(c *gcli.Command) {
			c.MustFromStruct(&extSetOpts)
			c.AddArg("name", "extension name", true)
		},
		Func: func(c *gcli.Command, args []string) error {
			name := c.Arg("name").String()
			ext := kiteext.NewExt(name, extSetOpts.Desc, extSetOpts.Path)
			ext.SetBinName(extSetOpts.BinName)
			ext.BeforeSave = func(ext *kiteext.KiteExt) bool {
				show.AList("Extension Info", ext)
				return interact.Confirm("Do you want to save this extension?")
			}

			return app.Exts.Add(ext)
		},
	}
)

add new extension

View Source
var (
	AppExtListCmd = &gcli.Command{
		Name:    "list",
		Desc:    "list all installed extensions",
		Aliases: []string{"ls"},
		Config: func(c *gcli.Command) {
			c.AddArg("keywords", "keywords for search extensions", false, true)
		},
		Func: func(c *gcli.Command, args []string) error {
			ccolor.Cyanln("Extensions metafile: ", app.Exts.Metafile)
			ccolor.Cyanln("Installed extensions:")
			keywords := c.Arg("keywords").Array()

			for _, ext := range app.Exts.Exts() {
				if len(keywords) > 0 {
					if !strutil.ContainsOne(ext.Name, keywords) && !strutil.ContainsOne(ext.Desc, keywords) {
						continue
					}
				}

				version := strutil.OrCond(ext.Version != "", " (v"+ext.Version+")", "")
				ccolor.Printf("  <green>%s</>  %s%s\n", ext.Name, ext.Desc, version)
				fmt.Printf("    - path: %s\n", ext.OsPath())
			}

			return nil
		},
	}
)
View Source
var AppExtRunCmd = &gcli.Command{
	Name: "run",
	Desc: "run extension commands with args",
	Help: `Example: kite app ext run [options] <ext> [args...]`,
	Config: func(c *gcli.Command) {
		c.MustFromStruct(&extRunOpts)
		c.AddArg("name", "extension name for run", true)
	},
	Func: func(c *gcli.Command, args []string) error {
		name := c.Arg("name").String()
		return handleExtRun(name, &kiteext.RunCtx{
			Dir:  extRunOpts.Workdir,
			Dry:  extRunOpts.DryRun,
			Env:  extRunOpts.EnvMap.Data(),
			Args: args,
		})
	},
}

AppExtRunCmd run ext commands

View Source
var (
	// AppExtUpdateCmd update kite extension
	AppExtUpdateCmd = &gcli.Command{
		Name: "update",
		Desc: "Update kite extension",
		Config: func(c *gcli.Command) {
			c.MustFromStruct(&extSetOpts)
			c.AddArg("name", "extension name", true)
		},
		Func: func(c *gcli.Command, args []string) error {
			name := c.Arg("name").String()
			ext, ok := app.Exts.Ext(name)
			if !ok {
				return fmt.Errorf("extension %s not found", name)
			}

			ext.SetPath(extSetOpts.Path)
			ext.SetBinName(extSetOpts.BinName)
			ext.SetAliases(extSetOpts.Aliases)
			if extSetOpts.Desc != "" {
				ext.Desc = extSetOpts.Desc
			}
			if extSetOpts.Author != "" {
				ext.Author = extSetOpts.Author
			}
			if extSetOpts.Homepage != "" {
				ext.Homepage = extSetOpts.Homepage
			}
			if extSetOpts.Version != "" {
				ext.Version = extSetOpts.Version
			}

			ext.BeforeSave = func(ext *kiteext.KiteExt) bool {
				show.AList("Extension Info", ext)
				return interact.Confirm("Do you want to save this extension?")
			}
			return app.Exts.Update(ext)
		},
	}
)

update kite extension

View Source
var BackendServeCmd = &gcli.Command{
	Name:    "serve",
	Aliases: []string{"be-serve", "server"},
	Desc:    "kite backend serve application",
	Func: func(c *gcli.Command, args []string) error {
		return errorx.New("todo")
	},
}

BackendServeCmd kite backend background server

View Source
var CommandMapCmd = &gcli.Command{
	Name:    "cmd-map",
	Aliases: []string{"cmdmap"},
	Desc:    "export all console commands to JSON/YAML for smart search and AI matching",
	Examples: `
# export both flat and tree to current dir
{$fullCmd} -o .

# export only flat structure to stdout
{$fullCmd} --struct flat

# export tree as YAML to file
{$fullCmd} --struct tree --format yaml -o /tmp/commands.tree.yaml

# export with flags/args info
{$fullCmd} --flags -o .
`,
	Config: func(c *gcli.Command) {
		c.BoolOpt2(&cmOpts.withFlag, "flags,flag", "also export command flags(options) and arguments")
		c.VarOpt(&cmOpts.format, "format", "fmt", "output format, allow: json, yaml", gflag.WithDefault("yaml"))
		c.VarOpt(&cmOpts.strukt, "struct", "s", "export structure type, allow: flat, tree, both", gflag.WithDefault("tree"))
		c.StrOpt2(&cmOpts.output, "output,o", "output file path or directory when --struct=both. default is stdout", gflag.WithDefault(""))
	},
	Func: func(c *gcli.Command, _ []string) error {
		cliApp := app.Cli
		format := cmOpts.format.String()
		strukt := cmOpts.strukt.String()
		outPath := cmOpts.output

		switch strukt {
		case "flat":
			items := buildFlatCmds(cliApp, cmOpts.withFlag)
			return writeExportData(items, format, outPath)

		case "tree":
			items := buildTreeCmds(cliApp, cmOpts.withFlag)
			return writeExportData(items, format, outPath)

		default:
			flat := buildFlatCmds(cliApp, cmOpts.withFlag)
			tree := buildTreeCmds(cliApp, cmOpts.withFlag)

			if outPath == "" {

				return writeExportData(map[string]any{
					"flat": flat,
					"tree": tree,
				}, format, "")
			}

			ext := fmtExt(format)
			flatFile := filepath.Join(outPath, "commands.flat"+ext)
			treeFile := filepath.Join(outPath, "commands.tree"+ext)

			if err := writeExportData(flat, format, flatFile); err != nil {
				return err
			}
			c.Successf("Written flat: %s\n", flatFile)

			if err := writeExportData(tree, format, treeFile); err != nil {
				return err
			}
			c.Successf("Written tree: %s\n", treeFile)
		}
		return nil
	},
}

CommandMapCmd export all console commands info to JSON/YAML for smart search and matching

View Source
var KiteAliasCmd = &gcli.Command{
	Name:    "alias",
	Aliases: []string{"aliases", "cmd-alias"},
	Desc:    "show custom command aliases in app(config:aliases)",
	Config: func(c *gcli.Command) {
		goutil.MustOK(c.UseSimpleRule().FromStruct(kaOpts))
		c.AddArg("name", "get real-name of the input alias").WithAfterFn(func(a *gflag.CliArg) error {
			kaOpts.Name = a.String()
			return nil
		})
	},
	Func: func(c *gcli.Command, _ []string) error {
		if kaOpts.List || kaOpts.Name == "" {
			show.AList("Command Aliases", app.Kas)
			return nil
		}

		fmt.Println(app.Kas.ResolveAlias(kaOpts.Name))
		return nil
	},
}

KiteAliasCmd command

View Source
var KiteConfCmd = &gcli.Command{
	Name:    "config",
	Aliases: []string{"conf", "cfg"},
	Desc:    "display kite config information",
	Config: func(c *gcli.Command) {
		c.BoolOpt2(&confOpts.all, "all,a", "display all config data")
		c.BoolOpt2(&confOpts.keys, "keys", "display raw config data")
		c.StrOpt2(&confOpts.search, "search,s", "search top key by input keywords")

		c.AddArg("key", "show config for the input key")
	},
	Func: func(c *gcli.Command, args []string) error {
		if confOpts.keys {
			names := make([]string, 16)
			for name := range app.Cfg().Data() {
				names = append(names, name)
			}

			c.Infoln("All Config Keys:")
			dump.Clear(names)
			return nil
		}

		if confOpts.all {
			c.Infoln("All Config Data:")
			dump.Clear(app.Cfg().Data())
			return nil
		}

		key := c.Arg("key").String()
		if key == "" {
			return errorx.Raw("please input key for show configuration")
		}

		switch key {
		case "git":
			key = app.ObjGit
		case "glab":
			key = app.ObjGlab
		case "hub", "ghub":
			key = app.ObjGhub
		}

		data, ok := app.Cfg().GetValue(key)
		if !ok {
			return c.NewErrf("not found config for key: %s", key)
		}

		c.Infoln("Config for key:", key)
		dump.Clear(data)
		return nil
	},
}

KiteConfCmd command

View Source
var KiteInfoCmd = &gcli.Command{
	Name: "info",
	Desc: "show the kite tool information",
	Func: func(c *gcli.Command, args []string) error {
		show.AList("information", map[string]any{
			"user home dir": sysutil.UserHomeDir(),
			"app bin dir":   c.Ctx.BinDir(),
			"app bin file":  c.Ctx.BinFile(),
			"user data dir": app.App().BaseDir,
			"work dir":      c.Ctx.WorkDir(),
			"dotenv file":   app.App().DotenvFile(),
			"config files":  app.Cfg().LoadedFiles(),
			"language":      "TODO",
			"version":       kite.Version,
			"build date":    kite.BuildDate,
			"go version":    kite.GoVersion,
			"github repo":   kite.GithubRepo,
		}, nil)

		return nil
	},
}

KiteInfoCmd instance

View Source
var KiteInitCmd = &gcli.Command{
	Name: "init",
	Desc: "init kite .env, config to the user home dir",
	Config: func(c *gcli.Command) {
		c.BoolOpt2(&ikOpts.dryRun, "dry-run, dry", "run workflow, dont real execute")
		c.BoolOpt2(&ikOpts.force, "force, f", "force re-init kite app config")
	},
	Func: func(c *gcli.Command, args []string) error {
		c.Warnln("Pre-check:")
		uHome := sysutil.UserHomeDir()
		c.Infoln("Found user home dir:", uHome)

		baseDir := app.App().BaseDir
		dotenvFile := fsutil.JoinPaths(baseDir, appconst.DotEnvFileName)
		confFile := fsutil.JoinPaths(baseDir, appconst.KiteConfigName)
		c.Infoln("Will init kite .env and config and more...")

		if ikOpts.dryRun {
			c.Warnln("TIP: on DRY-RUN mode, will not be execute any operation")
		}

		qr := goutil.NewQuickRun()
		qr.Add(func(ctx *structs.Data) error {
			idx := ctx.IntVal("index") + 1
			c.Infof("%d. Make the kite base dir: %s\n", idx, baseDir)
			if ikOpts.dryRun {
				return nil
			}

			if !ikOpts.force && fsutil.IsFile(baseDir) {
				c.Warnln("   Exists, skip write!")
				return nil
			}

			return fsutil.Mkdir(baseDir, fsutil.DefaultDirPerm)
		}, func(ctx *structs.Data) error {
			idx := ctx.IntVal("index") + 1
			c.Infof("%d. Init the .env file: %s\n", idx, dotenvFile)
			if ikOpts.dryRun {
				return nil
			}

			if !ikOpts.force && fsutil.IsFile(dotenvFile) {
				c.Warnln("   Exists, skip write!")
				return nil
			}

			text := byteutil.SafeString(kite.EmbedFs.ReadFile(".example.env"))
			_, err := fsutil.PutContents(dotenvFile, text, fsutil.FsCWTFlags)
			return err
		}, func(ctx *structs.Data) error {
			idx := ctx.IntVal("index") + 1
			c.Infof("%d. Init the main config file: %s\n", idx, confFile)
			if ikOpts.dryRun {
				return nil
			}

			if !ikOpts.force && fsutil.IsFile(confFile) {
				c.Warnln("   Exists, skip write!")
				return nil
			}

			text := byteutil.SafeString(kite.EmbedFs.ReadFile("kite.example.yml"))
			_, err := fsutil.PutContents(confFile, text, fsutil.FsCWTFlags)
			return err
		}, func(ctx *structs.Data) error {
			subDirs := []string{"config", "data", "scripts", "plugins", "tmp"}
			idx := ctx.IntVal("index") + 1
			c.Infof("%d. Init subdir in %s: %v\n", idx, baseDir, subDirs)
			if ikOpts.dryRun {
				return nil
			}

			return fsutil.MkSubDirs(fsutil.DefaultDirPerm, baseDir, subDirs...)
		}, func(ctx *structs.Data) error {
			idx := ctx.IntVal("index") + 1
			c.Infof("%d. Init kite config files to %s/config\n", idx, baseDir)
			if ikOpts.dryRun {
				return nil
			}

			return exportEmbedDir(kite.EmbedFs, "config", baseDir+"/config", true)
		})

		cliutil.Warnln("\nStarting init:")
		err := qr.Run()
		if err == nil {
			cliutil.Successln("✅  Init Completed")
		}
		return err
	},
}

KiteInitCmd command

View Source
var KiteObjectCmd = &gcli.Command{
	Name:    "object",
	Aliases: []string{"obj"},
	Desc:    "display service object config struct on kite",
	Config: func(c *gcli.Command) {
		c.AddArg("name", "show info for the object")
	},
	Func: func(c *gcli.Command, args []string) error {
		var data any
		key := c.Arg("name").String()
		switch key {
		case "app", "kite":
			data = app.App().Config
		case "git", "gitx":
			data = app.Gitx()
		case "glab", "gitlab":
			data = app.Glab()
		default:
			if app.Has(key) {
				data = app.GetAny(key)
			}
		}

		if data == nil {
			return c.NewErrf("not found object for %q", key)
		}

		c.Warnln("Object info for", key)
		dump.Clear(data)
		return nil
	},
}

KiteObjectCmd command

View Source
var KitePathCmd = &gcli.Command{
	Name:    "path",
	Aliases: []string{"paths"},
	Desc:    "show the kite system path information",
	Config: func(c *gcli.Command) {
		c.BoolOpt2(&kpOpts.list, "list, all, a, l", "display all paths for the kite")
		c.AddArg("path", "special alias path for resolve. prefix: base, config, tmp")
	},
	Func: func(c *gcli.Command, args []string) error {
		if kpOpts.list {
			show.AList("Kite paths", app.App().Config)
			return nil
		}

		path := c.Arg("path").String()
		if path == "" {
			return errorx.Raw("please input path for resolve")
		}

		path = app.App().ResolvePath(path)
		if path == "" {
			return errorx.Rawf("not found path for %q", path)
		}

		fmt.Println(path)
		return nil
	},
}

KitePathCmd command

View Source
var KiteReadmeCmd = &gcli.Command{
	Name:    "readme",
	Aliases: []string{"doc", "docs"},
	Desc:    "show readme docs for kite app",
	Config: func(c *gcli.Command) {

	},
	Func: func(c *gcli.Command, _ []string) error {
		str := byteutil.SafeString(kite.EmbedFs.ReadFile("README.md"))

		return apputil.RenderContents(str, "markdown", "github")
	},
}

KiteReadmeCmd instance

View Source
var LogWriteCmd = &gcli.Command{
	Name:    "logw",
	Aliases: []string{"log"},
	Desc:    "write a log message to the kite app logs file",
	Config: func(c *gcli.Command) {
		c.MustFromStruct(&lwOpts, gflag.TagRuleSimple)
		c.AddArg("message", "the log message", true)
	},
	Func: func(c *gcli.Command, _ []string) error {
		app.L.Log(slog.LevelByName(lwOpts.Level), c.Arg("message").String())
		return nil
	},
}

LogWriteCmd instance

View Source
var SelfManageCmd = &gcli.Command{
	Name:    "self",
	Aliases: []string{"app", "mgr"},
	Desc:    "provide commands for manage kite self",
	Subs: []*gcli.Command{
		AppCheckCmd,
		KiteInitCmd,
		KiteInfoCmd,
		KiteObjectCmd,
		KiteConfCmd,
		KiteReadmeCmd,
		KitePathCmd,
		NewPathMapCmd(),
		NewAppExtCmd(),
		KiteAliasCmd,
		BackendServeCmd,
		CommandMapCmd,
		UpdateSelfCmd,
		LogWriteCmd,
	},
}

SelfManageCmd manage kite self

View Source
var UpdateSelfCmd = &gcli.Command{
	Name:    "update",
	Aliases: []string{"update-self", "up-self", "up"},
	Desc:    "update {$binName} to latest from github repository",
}

UpdateSelfCmd command

Functions

func NewAppExtCmd

func NewAppExtCmd() *gcli.Command

NewAppExtCmd 创建应用扩展管理命令

func NewPathMapCmd

func NewPathMapCmd() *gcli.Command

NewPathMapCmd command

func SubCmdNotFound

func SubCmdNotFound(ctx *gcli.HookCtx) (stop bool)

Types

type CmdArgInfo

type CmdArgInfo struct {
	Name     string `json:"name" yaml:"name"`
	Desc     string `json:"desc" yaml:"desc"`
	Required bool   `json:"required,omitempty" yaml:"required,omitempty"`
	Arrayed  bool   `json:"arrayed,omitempty" yaml:"arrayed,omitempty"`
}

CmdArgInfo argument info for export

type CmdFlagInfo

type CmdFlagInfo struct {
	Name     string   `json:"name" yaml:"name"`
	Shorts   []string `json:"shorts,omitempty" yaml:"shorts,omitempty"`
	Desc     string   `json:"desc" yaml:"desc"`
	Default  any      `json:"default,omitempty" yaml:"default,omitempty"`
	Required bool     `json:"required,omitempty" yaml:"required,omitempty"`
	Hidden   bool     `json:"hidden,omitempty" yaml:"hidden,omitempty"`
}

CmdFlagInfo flag/option info for export

type CmdFlatInfo

type CmdFlatInfo struct {
	// Path is the full command path, e.g. "fs find"
	Path string `json:"path" yaml:"path"`
	// Name is the command name, e.g. "find"
	Name string `json:"name" yaml:"name"`
	// Group is the parent command name, e.g. "fs"; empty for top-level
	Group   string        `json:"group,omitempty" yaml:"group,omitempty"`
	Desc    string        `json:"desc" yaml:"desc"`
	Aliases []string      `json:"aliases,omitempty" yaml:"aliases,omitempty"`
	Hidden  bool          `json:"hidden,omitempty" yaml:"hidden,omitempty"`
	Flags   []CmdFlagInfo `json:"flags,omitempty" yaml:"flags,omitempty"`
	Args    []CmdArgInfo  `json:"args,omitempty" yaml:"args,omitempty"`
}

CmdFlatInfo flat command info, suitable for search/AI matching

type CmdTreeInfo

type CmdTreeInfo struct {
	Name    string         `json:"name" yaml:"name"`
	Desc    string         `json:"desc" yaml:"desc"`
	Aliases []string       `json:"aliases,omitempty" yaml:"aliases,omitempty"`
	Hidden  bool           `json:"hidden,omitempty" yaml:"hidden,omitempty"`
	Flags   []CmdFlagInfo  `json:"flags,omitempty" yaml:"flags,omitempty"`
	Args    []CmdArgInfo   `json:"args,omitempty" yaml:"args,omitempty"`
	Subs    []*CmdTreeInfo `json:"subs,omitempty" yaml:"subs,omitempty"`
}

CmdTreeInfo tree command info, preserves hierarchy

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL