executor

package module
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2021 License: MIT Imports: 11 Imported by: 0

README

Executor

A go package for the running of your local or remote(SSH) commands.

Expamle

execute a serial of commands:

import "github.com/crsgyj/executor"

func main() {
  var (
    err error
    exor Excutor
    async bool          = false
  )
  commands []CmdSample := []CmdSample{
    CmdSample{
      Name: "创建helloworld容器",
      Code: "docker run -d --name helloworld hello-world",
      Logging: true,
    },
    CmdSample{
      Name: "移除容器",
      Code: "docker rm helloworld",
      Logging: true,
    },
  }
  if exor, err = excutor.Local().Default(commands, async) {
    panic(err)
  }
  
  if err = exor.Run(); err != nil {
    panic(err)
  }
}

or do it like this:

package main

import (
	"regexp"

	"github.com/crsgyj/executor"
)

func main() {
	var (
		err   error
		ex    executor.Executor
		async bool = false
	)
	commands := []executor.Command{
		executor.Command{
			Name:       "创建helloworld容器",
			Code:       "docker run -tid --name=helloworld hello-world",
			Session:    executor.Sessions.Local(),
			AllowError: true,
			Done: func(c *executor.CmdController) {
				var (
					output      = c.GetOutput()
					containerID = ""
				)
				reg, _ := regexp.Compile("([a-z0-9]{64})")
				keys := reg.FindAllStringSubmatch(output, -1)
				if len(keys) >= 1 && len(keys[0]) >= 2 {
					containerID = keys[0][1]
				}
				c.SetState("containerID", containerID)
			},
		},
		executor.Command{
			Name:       "移除容器",
			Code:       "docker rm $container",
			Session:    executor.Sessions.Local(),
			AllowError: false,
			Logging:    true,
			Init: func(c *executor.CmdController) {
				var (
					containerID = c.GetState("containerID")
				)
				if containerID == nil || containerID == "" {
					c.Abandon()
					return
				}
				c.ReplaceCode("$container", containerID.(string), -1)
			},
		},
	}
	if ex, err = executor.New(commands, async); err != nil {
		panic(err)
	}

	if err = ex.Run(); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Sessions = struct {
	Remote func(config SessionConfig) execSessionBuilder
	Local  func() execSessionBuilder
}{
	Remote: newRemoteSession,
	Local:  newLocalSession,
}

Functions

This section is empty.

Types

type CmdController

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

func (*CmdController) Abandon

func (c *CmdController) Abandon()

Abandon

func (*CmdController) GetOutput

func (c *CmdController) GetOutput() string

GetOutput - get command Output

func (*CmdController) GetState

func (c *CmdController) GetState(key string) interface{}

GetState

func (*CmdController) ReplaceCode

func (c *CmdController) ReplaceCode(old string, new string, n int)

ReplaceCode

func (*CmdController) SetCode

func (c *CmdController) SetCode(code string)

func (*CmdController) SetDelay

func (c *CmdController) SetDelay(n int)

func (*CmdController) SetState

func (c *CmdController) SetState(key string, value interface{})

SetState

type CmdSample

type CmdSample struct {
	Name    string
	Code    string
	Desc    string
	Logging bool
}

type Command

type Command struct {
	// 任务名
	Name string
	// 命令
	Code string
	// 描述
	Desc string
	// 执行环境
	Session func() (execSession, error)
	// 是否允许错误 - 异步执行不关心错误
	AllowError bool
	// 是否异步执行
	Async bool

	// 初始化钩子
	Init ProcessHandler
	// 完成钩子
	Done ProcessHandler
	// 是否打印log
	Logging bool
	// 延迟执行,单位毫秒, 100毫秒以下设置无效
	Delay int // Delay - run after ${delay} millisecond, ignore when < 100

	// 环境变量
	Env []string
	// contains filtered or unexported fields
}

Command 命令

func (*Command) Exec

func (c *Command) Exec() error

func (*Command) Inspect

func (c *Command) Inspect() (err error)

Inspect - inspect if command is valid

type ExecCreator

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

func Local

func Local() *ExecCreator

func Remote

func Remote(config SessionConfig) *ExecCreator

func (*ExecCreator) Default

func (e *ExecCreator) Default(commandLines []CmdSample) (Executor, error)

type Executor

type Executor interface {
	Run() error
	Init() error
}

func New

func New(cmdList []Command, async bool) (Executor, error)

type ProcessHandler

type ProcessHandler = func(m *CmdController)

type SessionConfig

type SessionConfig struct {
	User     string
	Password string
	Host     string
	Port     int8
	Timeout  int
}

Jump to

Keyboard shortcuts

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