process

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2021 License: MIT Imports: 14 Imported by: 0

README

exec

go package os/exec 的封装

usage:

import "gitee.com/go-wena/process"

func main(){
    process.NewWith("ls_example", "sh", "-c", "ls -la").
        SetOnReceived(func(line string, isError bool) {
            if isError {
                fmt.Printf("ERR: %s\n", line)
            } else {
                fmt.Printf("OUT: %s\n", line)
            }
        }).Start()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Commander

type Commander struct {
	Status //状态
	// contains filtered or unexported fields
}

Commander 命令

func Command added in v0.0.2

func Command(path string, args ...string) *Commander

Command 使用程序路径和参数创建一个命令, 类似 exec.Command

func New

func New() *Commander

New 创建一个命令

func NewWith

func NewWith(name, path string, args ...string) *Commander

NewWith 使用名称和程序路径,参数创建一个命令

func WithContext added in v0.0.6

func WithContext(ctx context.Context) *Commander

func (*Commander) AddEnv

func (c *Commander) AddEnv(key, value string) *Commander

AddEnv 设置环境变量

func (*Commander) AppendEnv

func (c *Commander) AppendEnv(env ...string) *Commander

AppendEnv 增加环境变量

func (*Commander) ErrOut added in v0.0.3

func (c *Commander) ErrOut(out ...io.Writer) *Commander

ErrOut 错误输出

func (*Commander) GetOutput added in v0.0.4

func (c *Commander) GetOutput() ([]string, error)

GetOutput 执行命令获取输出

func (*Commander) Name

func (c *Commander) Name() string

Name 命令名称(标识)

func (*Commander) SetBackground

func (c *Commander) SetBackground(yes ...bool) *Commander

SetBackground 后台运行(不阻断 Start 方法)

func (*Commander) SetCommand

func (c *Commander) SetCommand(path string, args ...string) *Commander

SetCommand 设置命令的执行路径(绝对路径或者相对DIR路径或者PATH)和参数

func (*Commander) SetDIR

func (c *Commander) SetDIR(dir string) *Commander

SetDIR 设置工作目录

func (*Commander) SetEnv

func (c *Commander) SetEnv(env []string) *Commander

SetEnv 设置环境变量

func (*Commander) SetName

func (c *Commander) SetName(name string) *Commander

SetName 设置命令名称(标识),用于友好识别

func (*Commander) SetOnReceived

func (c *Commander) SetOnReceived(onReceived ReceiverFunc) *Commander

SetOnReceived 设置输出接收方法(逐行输出)

func (*Commander) SetOnStatusChanged

func (c *Commander) SetOnStatusChanged(onStatusChanged StatusChangedFunc) *Commander

SetOnStatusChanged 设置状态变更监听

func (*Commander) SetQuit

func (c *Commander) SetQuit(quit <-chan struct{}) *Commander

SetQuit 设置退出信道

func (*Commander) Start

func (c *Commander) Start() error

Start 运行

func (*Commander) StdIn added in v0.0.5

func (c *Commander) StdIn(in ...io.Reader) *Commander

StdIn 正常输入

func (*Commander) StdOut added in v0.0.3

func (c *Commander) StdOut(out ...io.Writer) *Commander

StdOut 正常输出

func (*Commander) Stop

func (c *Commander) Stop() error

Stop 停止程序

func (*Commander) String

func (c *Commander) String() string

String 输出命令行

func (*Commander) UseWindowsLegacyProcessStrategy

func (c *Commander) UseWindowsLegacyProcessStrategy(yes ...bool) *Commander

UseWindowsLegacyProcessStrategy 使用传统进程管理(windows)

func (*Commander) Verbose added in v0.0.3

func (c *Commander) Verbose(verbose bool) *Commander

Verbose 打印执行的命令

func (*Commander) Wait

func (c *Commander) Wait() error

Wait 等待执行完成

func (*Commander) WithContext added in v0.0.6

func (c *Commander) WithContext(ctx context.Context) *Commander

WithContext 设置上下文对象(控制退出)

type PID

type PID int

PID 进程编号对象

func (PID) GetPID

func (id PID) GetPID() int

GetPID 获取进程编号

func (PID) Interrupt

func (id PID) Interrupt() error

Interrupt 发送Ctrl+C信号

func (PID) Kill

func (id PID) Kill() error

Kill 强制终止进程

func (PID) Terminate

func (id PID) Terminate() error

Terminate 终止进程

type ReceiverFunc

type ReceiverFunc = func(line string, isError bool)

ReceiverFunc 输出接收方法

type Resource

type Resource struct {
	CPU string
	Mem string
}

type Shell

type Shell struct {
	Name string   `json:"name" yaml:"name" toml:"name"`
	Dir  string   `json:"dir" yaml:"dir" toml:"dir"`
	Env  []string `json:"env" yaml:"env" toml:"env"`
	Step []*Step  `json:"steps" yaml:"steps" toml:"steps"`
}

func (*Shell) CreateCommander

func (s *Shell) CreateCommander(step *Step) *Commander

func (*Shell) Run

func (s *Shell) Run(ctx context.Context) error

type Status

type Status struct {
	PID                //PID
	Err     error      //错误
	Sig     os.Signal  //退出信号
	Code    int        //退出代码
	StartAt time.Time  //启动时间
	StopAt  time.Time  //停止时间
	Status  StatusCode //状态
}

Status 进程状态

func (*Status) Reset

func (s *Status) Reset()

func (Status) String

func (s Status) String() string

type StatusChangedFunc

type StatusChangedFunc = func(status Status)

StatusChangedFunc 状态变更委托方法

type StatusCode

type StatusCode int
const (
	Ready StatusCode = 1 << iota
	Starting
	Running
	Stopping
	Stopped
	Exited
)

type Step

type Step struct {
	Name string   `json:"name" yaml:"name" toml:"name"`
	Dir  string   `json:"dir" yaml:"dir" toml:"dir"`
	Env  []string `json:"env" yaml:"env" toml:"env"`
	Path string   `json:"path" yaml:"path" toml:"path"`
	Args []string `json:"args" yaml:"args" toml:"args"`
}

Jump to

Keyboard shortcuts

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