module

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrefixFlag = "__flag_"
)

Variables

View Source
var (
	ErrConflict = errors.New("runtime conflict")
)
View Source
var (
	ErrEmptyDir = errors.New("empty directory")
)

Functions

This section is empty.

Types

type Command

type Command struct {
	Name     string     `yaml:"name"`
	Long     string     `yaml:"long"`
	Script   string     `yaml:"script"`
	Authors  []string   `yaml:"authors"`
	Version  string     `yaml:"version"`
	Example  string     `yaml:"example"`
	Params   []*Schema  `yaml:"params"`
	Returns  []*Schema  `yaml:"returns"`
	Commands []*Command `yaml:"commands"`
	Root     string     `yaml:"root"`

	PreRun  RunE `yaml:"-"`
	Run     RunE `yaml:"-"`
	PostRun RunE `yaml:"-"`
	// contains filtered or unexported fields
}

func (*Command) Flags

func (c *Command) Flags() *pflag.FlagSet

func (*Command) NewContext

func (c *Command) NewContext(ctx context.Context, lg *zap.Logger, conn client.IClient, variables *StableMap) *RunContext

func (*Command) ParseCmd

func (c *Command) ParseCmd() *cobra.Command

func (*Command) Runnable

func (c *Command) Runnable() bool

type CommandErr added in v0.2.0

type CommandErr struct {
	Err    error
	Stderr []byte
}

func (*CommandErr) Error added in v0.2.0

func (e *CommandErr) Error() string

type Module

type Module struct {
	*Command `yaml:",inline"`
	Dir      string `yaml:"dir"`
}

func LoadDir

func LoadDir(name string) (*Module, error)

func (*Module) Execute

func (m *Module) Execute(args ...string) (*Command, error)

type Repl

type Repl string

Repl the kind of interpreter

const (
	Unknown    Repl = ""
	Tengo      Repl = "tengo"
	Bash       Repl = "bash"
	Powershell Repl = "powershell"
)

func KnownExt

func KnownExt(ext string) (Repl, bool)

type RunContext

type RunContext struct {
	context.Context

	Logger    *zap.Logger
	Cmd       *Command
	Conn      client.IClient
	Variables *StableMap
}

type RunE

type RunE func(ctx *RunContext, options ...client.ExecOption) ([]byte, error)
var DefaultRunCommand RunE = func(ctx *RunContext, opts ...client.ExecOption) ([]byte, error) {
	command := ctx.Cmd
	lg := ctx.Logger
	conn := ctx.Conn
	execOptions := client.NewExecOptions()
	for _, opt := range opts {
		opt(execOptions)
	}

	command.Flags().VisitAll(func(flag *pflag.Flag) {
		value := ctx.Variables.GetDefault(PrefixFlag+flag.Name, flag.Value.String())
		arg := "--" + flag.Name + "=" + value
		execOptions.Args = append(execOptions.Args, arg)
	})

	options := make([]client.ExecOption, 0)
	ext, ok := KnownExt(path.Ext(command.Script))
	if !ok {
		ext = Tengo
	}

	home := ctx.Variables.GetDefault(vars.BeeHome, ".bee")
	goos := ctx.Variables.GetDefault(vars.BeePlatformVars, "linux")

	var repl string
	var err error
	if repl, err = checkRepl(goos, ext); err != nil {
		return nil, err
	}

	script := path.Join(home, "modules", command.Root, command.Script)
	if goos == "windows" {
		script = strings.ReplaceAll(script, "/", "\\")
	}

	switch ext {
	case Tengo:
		repl = path.Join(home, "bin", repl)
		if goos == "windows" {
			repl = strings.ReplaceAll(repl, "/", "\\")
		}
	case Bash:
		options = append(options, client.ExecWithArgs("-c"))
	case Powershell:
	}

	options = append(options, client.ExecWithArgs(script))
	options = append(options, client.ExecWithArgs(execOptions.Args...))
	for key, value := range execOptions.Environments {
		options = append(options, client.ExecWithEnv(key, value))
	}

	shell := fmt.Sprintf("%s %s %s", repl, script, strings.Join(execOptions.Args, " "))
	lg.Debug("remote execute", zap.String("command", shell))
	cmd, err := conn.Execute(ctx, repl, options...)
	if err != nil {
		return nil, err
	}
	data, err := cmd.CombinedOutput()
	if err != nil {
		return nil, &CommandErr{Err: err, Stderr: beautify(data)}
	}
	return beautify(data), nil
}

type Schema

type Schema struct {
	Name        string       `yaml:"name"`
	Type        string       `yaml:"type"`
	Short       string       `yaml:"short"`
	Description string       `yaml:"description"`
	Default     string       `yaml:"default"`
	Example     string       `yaml:"example"`
	Value       *SchemaValue `yaml:"-"`
}

func (*Schema) InitValue

func (s *Schema) InitValue() *SchemaValue

type SchemaValue

type SchemaValue struct {
	IntP      *int64
	UintP     *uint64
	StringP   *string
	FloatP    *float64
	DurationP *time.Duration
}

func (*SchemaValue) Set

func (sv *SchemaValue) Set(text string) error

func (*SchemaValue) String

func (sv *SchemaValue) String() string

func (*SchemaValue) Type

func (sv *SchemaValue) Type() string

type StableMap

type StableMap struct {
	// contains filtered or unexported fields
}

func NewVariables

func NewVariables() *StableMap

func (*StableMap) Exists added in v0.2.0

func (sm *StableMap) Exists(key string) bool

func (*StableMap) GetDefault

func (sm *StableMap) GetDefault(key string, defaultV string) string

func (*StableMap) Set

func (sm *StableMap) Set(key string, value string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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