kscript

package
v0.0.0-...-9c55465 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllowExt = []string{".sh", ".zsh", ".bash", ".php", ".go", ".gop", ".kts", ".java", ".gry", ".groovy"}

AllowExt list

View Source
var AllowTypes = []string{"sh", "zsh", "bash"}

AllowTypes shell wrapper for run script

View Source
var ErrNotFound = errors.New("script not found")

ErrNotFound error

View Source
var ExtToBinMap = map[string]string{
	".sh":     "sh",
	".zsh":    "zsh",
	".bash":   "bash",
	".php":    "php",
	".gry":    "groovy",
	".groovy": "groovy",
	".go":     "go run",
}

ExtToBinMap data

eg:

'#!/usr/bin/env bash'
'#!/usr/bin/env -S go run'

Functions

func IsNoNotFound

func IsNoNotFound(err error) bool

IsNoNotFound error

Types

type RunCtx

type RunCtx struct {
	// Name for script run
	Name string
	Type string

	// Verbose show more info on run
	Verbose bool
	// DryRun script
	DryRun bool
	// Workdir for run script
	Workdir string
	// Env setting for run
	Env map[string]string

	// BeforeFn hook
	BeforeFn func(si *ScriptInfo, ctx *RunCtx)
}

RunCtx definition

func EnsureCtx

func EnsureCtx(ctx *RunCtx) *RunCtx

EnsureCtx to

func (*RunCtx) MergeEnv

func (c *RunCtx) MergeEnv(mp map[string]string) map[string]string

MergeEnv and returns

func (*RunCtx) WithName

func (c *RunCtx) WithName(name string) *RunCtx

WithName to ctx

type Runner

type Runner struct {
	// Scripts config and loaded from DefineFiles.
	//
	// format: {name: info, name2: info2, ...}
	Scripts map[string]any `json:"scripts"`
	// DefineFiles scripts 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"`
	// TypeShell wrapper for run each script.
	//
	// value like: bash, sh, zsh or empty for direct run command
	TypeShell string `json:"type_shell"`
	// ParseEnv var on script command
	ParseEnv bool `json:"parse_env"`

	// ScriptDirs script file dirs, allow multi
	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
	ExtToBinMap map[string]string `json:"ext_to_bin_map"`
	// PathResolver handler
	PathResolver func(path string) string
	// contains filtered or unexported fields
}

Runner struct

func NewRunner

func NewRunner(fns ...func(sr *Runner)) *Runner

NewRunner instance

func (*Runner) DefinedScripts

func (r *Runner) DefinedScripts() map[string]any

DefinedScripts map

func (*Runner) InitLoad

func (r *Runner) InitLoad() error

InitLoad define scripts and script files.

func (*Runner) IsDefinedScript

func (r *Runner) IsDefinedScript(name string) bool

IsDefinedScript name

func (*Runner) LoadDefineScripts

func (r *Runner) LoadDefineScripts() (err error)

LoadDefineScripts from DefineFiles

func (*Runner) LoadScriptFiles

func (r *Runner) LoadScriptFiles() error

LoadScriptFiles from the ScriptDirs

func (*Runner) RawDefinedScript

func (r *Runner) RawDefinedScript(name string) (any, bool)

RawDefinedScript raw info get

func (*Runner) Run

func (r *Runner) Run(name string, args []string, ctx *RunCtx) error

Run script or script-file by name and with args

func (*Runner) RunDefinedScript

func (r *Runner) RunDefinedScript(name string, args []string, ctx *RunCtx) error

RunDefinedScript by input name and with arguments

func (*Runner) RunScriptFile

func (r *Runner) RunScriptFile(name string, args []string, ctx *RunCtx) error

RunScriptFile by input name and with arguments

func (*Runner) RunScriptInfo

func (r *Runner) RunScriptInfo(si *ScriptInfo, inArgs []string, ctx *RunCtx) error

RunScriptInfo by args and context

func (*Runner) ScriptDefineInfo

func (r *Runner) ScriptDefineInfo(name string) (*ScriptInfo, error)

ScriptDefineInfo get script info as ScriptInfo

func (*Runner) ScriptFileInfo

func (r *Runner) ScriptFileInfo(name string) (*ScriptInfo, error)

ScriptFileInfo info get

func (*Runner) ScriptFiles

func (r *Runner) ScriptFiles() map[string]string

ScriptFiles file map

func (*Runner) Search

func (r *Runner) Search(name string, args []string, limit int) map[string]string

Search by name

func (*Runner) TryRun

func (r *Runner) TryRun(name string, args []string, ctx *RunCtx) (found bool, err error)

TryRun script or script-file by name and with args

type ScriptInfo

type ScriptInfo struct {

	// Type wrap for run script. allow: sh, bash, zsh
	Type string

	// Workdir for run script
	Workdir string
	// Platform limit. allow: windows, linux, darwin
	Platform []string
	// Output target. default is stdout
	Output string
	// Vars for run script. allow exec a command line TODO
	Vars map[string]string
	// Ext enable ext: proxy, clip
	Ext string

	// Name for script
	Name string
	// Desc message
	Desc string
	// Env setting for run
	Env map[string]string
	// Args script args definition.
	Args, ArgNames []string
	// Cmds commands list
	Cmds []string

	// File script file path in Runner.ScriptDirs
	File    string
	BinName string
	FileExt string // eg: .go
}

ScriptInfo struct

func (*ScriptInfo) IsDefined

func (si *ScriptInfo) IsDefined() bool

IsDefined script

func (*ScriptInfo) IsFile

func (si *ScriptInfo) IsFile() bool

IsFile script

func (*ScriptInfo) ParseArgs

func (si *ScriptInfo) ParseArgs() (args []string)

ParseArgs on commands

func (*ScriptInfo) WithFallbackType

func (si *ScriptInfo) WithFallbackType(typ string) *ScriptInfo

WithFallbackType on not set

Jump to

Keyboard shortcuts

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