Documentation
¶
Index ¶
- Variables
- func IsNoNotFound(err error) bool
- type CmdResult
- type RunCtx
- func (c *RunCtx) AppendArgsToVars(vars map[string]any)
- func (c *RunCtx) FullEnv() map[string]string
- func (c *RunCtx) MergeEnv(mps ...map[string]string)
- func (c *RunCtx) ParseVarInEnv(envPaths []string, vars map[string]any) map[string]string
- func (c *RunCtx) WithName(name string) *RunCtx
- func (c *RunCtx) WithNameArgs(name string, args []string) *RunCtx
- type Runner
- func (r *Runner) InitLoad() error
- func (r *Runner) IsScriptTask(name string) bool
- func (r *Runner) LoadScriptApps()
- func (r *Runner) LoadScriptFileInfo(name string) (*ScriptFile, error)
- func (r *Runner) LoadScriptFiles() error
- func (r *Runner) LoadScriptTaskInfo(name string) (*ScriptTask, error)
- func (r *Runner) LoadScriptTasks() (err error)
- func (r *Runner) RawScriptTask(name string) (any, bool)
- func (r *Runner) RawScriptTasks() map[string]any
- func (r *Runner) Run(name string, args []string, ctx *RunCtx) error
- func (r *Runner) RunScriptFile(name string, args []string, ctx *RunCtx) error
- func (r *Runner) RunScriptTask(name string, args []string, ctx *RunCtx) error
- func (r *Runner) ScriptFiles() map[string]string
- func (r *Runner) Search(name string, args []string, limit int) map[string]string
- func (r *Runner) Search1ByName(name string, limit int) string
- func (r *Runner) SearchByKeywords(parts []string, limit int) map[string]string
- func (r *Runner) SearchByName(name string, limit int) map[string]string
- func (r *Runner) TryRun(name string, args []string, ctx *RunCtx) (found bool, err error)
- type RunnerMeta
- type ScriptApp
- type ScriptApps
- type ScriptFile
- type ScriptFiles
- type ScriptInfo
- type ScriptItem
- type ScriptMeta
- type ScriptTask
- type ScriptTasks
- type ScriptType
- type TaskCmd
- type TaskFor
- type TaskSettings
- type Variable
Constants ¶
This section is empty.
Variables ¶
var ( // AllowTypes shell wrapper for run a script. eg: bash, sh, zsh, cmd, pwsh AllowTypes = []string{"sh", "zsh", "bash", "cmd", "pwsh"} // AllowExt list. allowed script file ext. AllowExt = []string{".sh", ".zsh", ".bash", ".php", ".go", ".gop", ".kts", ".java", ".gry", ".groovy", ".py"} )
var ( // DefaultTaskFiles 默认自动查找的task文件名称 eg "kite.task[s].yml", "kite.script[s].yml" DefaultTaskFiles = []string{".kite.task", ".kite.tasks", ".kite.script", ".kite.scripts"} // DefaultDefineExts 默认允许的 scriptApp, scriptTask 定义文件后缀 DefaultDefineExts = []string{".yml", ".yaml", ".toml", ".json"} )
var ErrNotFound = errors.New("script not found")
ErrNotFound error
var ExtToBinMap = map[string]string{
".sh": "sh",
".zsh": "zsh",
".bash": "bash",
".php": "php",
".py": "python",
".gry": "groovy",
".groovy": "groovy",
".go": "go run",
}
ExtToBinMap data
eg:
'#!/usr/bin/env bash' '#!/usr/bin/env -S go run'
Functions ¶
Types ¶
type CmdResult ¶
type CmdResult struct {
// Type for the command result, default is text.
// - Allow: text, json, xml, html, csv, yaml, toml, jsonl, json5, jsonc
Type string
// contains filtered or unexported fields
}
type RunCtx ¶
type RunCtx struct {
// Name for script run
Name string
Type string // shell type
// ScriptType name
ScriptType ScriptType
// Verbose show more info on run
Verbose bool
// DryRun script
DryRun bool
// Workdir for run a script
Workdir string
// Vars for run cmd. access: $ctx.var_name
Vars map[string]string
// Env setting for run
// - 支持使用 ${var_name} 引用变量
Env map[string]string
// Silent mode, dont print exec command line.
Silent bool `json:"silent"`
// Args for run script task
Args []string
// BeforeFn hook. si: ScriptTask | ScriptApp | ScriptFile
BeforeFn func(si any, ctx *RunCtx)
// AppendVarsFn hook for run task. eg: gvs, paths, kite
AppendVarsFn func(data map[string]any) map[string]any
// contains filtered or unexported fields
}
RunCtx for run a task/file/app
func (*RunCtx) AppendArgsToVars ¶
AppendArgsToVars set args to vars. like shell $1 .. $N
func (*RunCtx) ParseVarInEnv ¶
type Runner ¶
type Runner struct {
RunnerMeta
// PathResolver handler. 用于查找脚本文件
PathResolver func(path string) string
// ScriptAppDirs 独立的 script app 定义文件目录. 每个定义文件是一个独立的cli app
// - default will load from `$base/script-app`
ScriptAppDirs []string `json:"script_app_dirs"`
// ScriptAppExts script app file define extensions. eg: .yml, .yaml
ScriptAppExts []string `json:"script_app_exts"`
// DefineFiles script tasks define files, will read and add to Scripts
//
// Allow vars: $user, $os
//
// eg:
// - config/module/scripts.yml
// - ?config/module/scripts.$os.yml // start withs '?' - an optional file, load on exists.
DefineFiles []string `json:"define_files"`
// 自动加载的task文件名称列表,无需设置扩展
// - 将自动从当前目录或父级目录中寻找 script task 定义文件
// - 找到第一个匹配的就停止
AutoTaskFiles []string `json:"auto_task_files"`
// 自动加载的task文件扩展名
AutoTaskExts []string `json:"auto_task_exts"`
// auto 向上搜索目录最大深度,默认为 6. 找到第一个匹配的就停止
AutoMaxDepth int `json:"auto_max_depth"`
// Scripts 通过配置定义的各种简单的任务命令。tasks config and loaded from DefineFiles.
//
// Format: {name: info, name2: info2, ...}
//
// - special settings key: __settings, will read and merge to Settings
Scripts map[string]any `json:"scripts"`
// ParseEnv var on script command expr. eg: $SHELL
ParseEnv bool `json:"parse_env"`
// TypeShell wrapper for run each script.
//
// value like: bash, sh, zsh, cmd, pwsh or empty for direct run command
TypeShell string `json:"type_shell"`
// ScriptDirs 独立的 script file 文件查找目录。例如 bash, python, php 等脚本文件
ScriptDirs []string `json:"script_dirs"`
// AllowedExt allowed script file extensions. eg: .go, .sh
AllowedExt []string `json:"allowed_ext"`
// FindBinByExt on run a script file
FindBinByExt bool `json:"find_bin_by_ext"`
// ExtToBinMap settings. key: ext, value: bin name or path
ExtToBinMap map[string]string `json:"ext_to_bin_map"`
// BinPathMap settings. key: bin name, value: bin path
BinPathMap map[string]string `json:"bin_path_map"`
// contains filtered or unexported fields
}
Runner struct. TODO KRunner, ScriptRunner or ScriptManager
实现扩展的kite run命令,可以执行任何的 script-file, script-task, script-app 等等
func (*Runner) LoadScriptApps ¶
func (r *Runner) LoadScriptApps()
LoadScriptApps from Runner.ScriptApps
func (*Runner) LoadScriptFileInfo ¶
func (r *Runner) LoadScriptFileInfo(name string) (*ScriptFile, error)
LoadScriptFileInfo info get
func (*Runner) LoadScriptFiles ¶
LoadScriptFiles from the ScriptDirs
func (*Runner) LoadScriptTaskInfo ¶
func (r *Runner) LoadScriptTaskInfo(name string) (*ScriptTask, error)
LoadScriptTaskInfo get script info as ScriptTask
func (*Runner) LoadScriptTasks ¶
LoadScriptTasks from Runner.DefineFiles
func (*Runner) RawScriptTask ¶
RawScriptTask raw info get
func (*Runner) RunScriptFile ¶
RunScriptFile by input name and with arguments
func (*Runner) RunScriptTask ¶
RunScriptTask by input name and with arguments
func (*Runner) Search1ByName ¶
Search1ByName search one script task/file by name
func (*Runner) SearchByKeywords ¶
SearchByKeywords search script task/file by keywords
func (*Runner) SearchByName ¶
SearchByName search script task/file by name
type RunnerMeta ¶
type RunnerMeta struct {
}
type ScriptApp ¶
type ScriptApp struct {
ScriptMeta
// script app name, use file name
Name string
// File script app file path in Runner.ScriptApps
File string
}
type ScriptApps ¶
type ScriptApps struct {
// contains filtered or unexported fields
}
type ScriptFile ¶
type ScriptFile struct {
ScriptMeta
// script name, default uses file name. eg: demo.sh
Name string
// File script file path in Runner.ScriptDirs
File string
// BinName script file bin name. 默认从 ext 解析 e.g.: .php => php
BinName string
// file ext. eg: .go
FileExt string
// ShellBang script file shell bang line.
// always at first line and start with: #!
ShellBang string
// contains filtered or unexported fields
}
type ScriptFiles ¶
type ScriptFiles struct {
// contains filtered or unexported fields
}
type ScriptItem ¶
type ScriptItem interface {
ScriptTask | ScriptApp | ScriptFile
}
type ScriptMeta ¶
type ScriptMeta struct {
// ScriptType name
ScriptType ScriptType
// Workdir for run script, default is current dir.
Workdir string
// CleanEnv for run script, default is false.
CleanEnv bool
// Env setting for run the script file/app/task
// - 支持使用 ${var_name} 引用变量
Env map[string]string
// EnvPaths custom prepend set ENV PATH.
// - 支持使用 ${var_name} 引用变量
EnvPaths []string
// Timeout for run a script, default is 0.
Timeout time.Duration
// contains filtered or unexported fields
}
type ScriptTask ¶
type ScriptTask struct {
ScriptMeta
// Name for the script task
Name string
// Desc message
Desc string
Usage string // usage message
Help string // long help message
// Type shell wrap for run the task. allow: sh, bash, zsh
Type string
// Alias names for the script task
Alias []string
// Silent mode, dont print exec command line and output.
Silent bool `json:"silent"`
// Output target. default is stdout
Output string
// Vars map[string]Variable // TODO
// Vars for run script task.
// - task配置中访问: $name
// - allow dynamic var: "@sh: git log -1" TODO
Vars map[string]string `json:"vars"`
// Deps task name list. 当前任务依赖的任务名称列表
Deps []string `json:"deps"`
// Cmds exec commands list.
Cmds []*TaskCmd
// Args for exec task commands.
Args []string
// CmdTimeout for run each command, default is 0.
CmdTimeout time.Duration
// Platform limit exec. allow: windows, linux, darwin
Platform []string
// PlatformSet 当前系统平台的设置,可以覆盖设置 Type, Cmds
PlatformSet map[string]any
// Ext enable extensions: proxy, clip
Ext string
// If condition check for run command. eg: sh:test -f .env
// or see github.com/expr-lang/expr
If string
// For loop for run command. eg: for i in 1 2 3; do echo $i; done
// - var: 可以引用变量
For *TaskFor
}
ScriptTask for one script task.
func (*ScriptTask) CmdsToString ¶
func (st *ScriptTask) CmdsToString(sep ...string) string
CmdsToString build.
func (*ScriptTask) LoadFromMap ¶
func (st *ScriptTask) LoadFromMap(mp map[string]any) error
func (*ScriptTask) ParseArgs ¶
func (st *ScriptTask) ParseArgs() (args []string)
ParseArgs on commands
func (*ScriptTask) WithFallbackType ¶
func (st *ScriptTask) WithFallbackType(typ string) *ScriptTask
WithFallbackType on not setting.
type ScriptTasks ¶
type ScriptTasks struct {
// contains filtered or unexported fields
}
type ScriptType ¶
type ScriptType string
ScriptType definition
const ( TypeFile ScriptType = "file" TypeTask ScriptType = "task" // task define. TypeApp ScriptType = "app" // app define. 独立的cli app配置文件 )
type TaskCmd ¶
type TaskCmd struct {
// Name for the command. if not set, use the command index: "cmd{index}"
Name string
// Workdir for run command
Workdir string
// Vars for run cmd.
// - task配置中访问: $name
// - allow dynamic var: "@sh: git log -1"
Vars map[string]string
// Env append ENV setting for run
Env map[string]string
// Run command line expr for run. eg: go run main.go
Run string
// Task refer task name.
Task string
// Type wrap for run. Allow: sh, bash, zsh
Type string
// If condition expr for run, return true or false
If string
// FailMsg custom message on run fail
FailMsg string
// Silent mode, dont print exec command line.
Silent bool `json:"silent"`
// Timeout for run the command, default is 0.
Timeout time.Duration
// IgnoreErr safed run command. ignore run error, continue next command.
IgnoreErr bool `json:"ignore_err"`
// Output for run the command, default is stdout.
// - allow: stdout, stderr, both, discard, collect
// - collect: collect command output to Result. and can use Result.Text() or Result.Bytes() to get output.
Output string
// ResultType for run the command, default is text. see CmdResult.Type
//
// Allow: text, json, xml, html, csv, yaml, toml, jsonl, json5, jsonc
ResultType string
// Result for run the command, default is nil.
Result CmdResult
// contains filtered or unexported fields
}
TaskCmd of the task TODO
type TaskSettings ¶
type TaskSettings struct {
// Env append set ENV for all script tasks
Env map[string]string `json:"env"`
// EnvPaths prepend set ENV PATHs for all script tasks
EnvPaths []string `json:"env_paths"`
// DefaultGroup default group name for use Groups. will merge default group data to Vars
DefaultGroup string `json:"default_group"`
// Vars built in vars map. group name: vars
// - usage in a task: ${vars.key}
Vars map[string]string `json:"vars"`
// Grouped vars map.
// - group name => map[string]string grouped var map.
Groups comdef.L2StrMap `json:"groups"`
}
TaskSettings 可以通过 script task 文件中的 "__settings" 调整设置