proc

package module
v0.1.3 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: 2

README

exec

go package os/exec 的封装

usage:

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

func main(){
    err := proc.Command("sh", "-c", "ls -la").
        ReadOut(PrintLine("OUT")).
        ReadErr(PrintLine("ERR")).
        Verbose().
        Start()
    if err != nil {
        t.Fatal(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

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

Cmd 命令

func Command

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

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

func CommandContext

func CommandContext(ctx context.Context, path string, args ...string) *Cmd

CommandContext 使用程序路径和参数创建一个命令 exec.CommandContext

func Context

func Context(ctx context.Context) *Cmd

Context 使用上下文Context对象创建一个命令

func New

func New() *Cmd

New 创建一个命令

func (*Cmd) Background

func (c *Cmd) Background(background ...bool) *Cmd

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

func (*Cmd) Command

func (c *Cmd) Command(path string, args ...string) *Cmd

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

func (*Cmd) Context

func (c *Cmd) Context(ctx context.Context) *Cmd

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

func (*Cmd) DIR

func (c *Cmd) DIR(dir string) *Cmd

DIR 设置工作目录

func (*Cmd) Env

func (c *Cmd) Env(env []string) *Cmd

Env 设置环境变量

func (*Cmd) ErrOut

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

ErrOut 错误输出

func (*Cmd) ExitCode

func (c *Cmd) ExitCode() int

ExitCode 退出代码

func (*Cmd) GetName

func (c *Cmd) GetName() string

GetName 命令名称(标识)

func (*Cmd) GetOutput

func (c *Cmd) GetOutput() (string, error)

GetOutput 执行命令获取输出

func (*Cmd) Name

func (c *Cmd) Name(name string) *Cmd

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

func (*Cmd) OnExit added in v0.1.2

func (c *Cmd) OnExit(onExit ...func()) *Cmd

OnExit 退出时执行

func (*Cmd) OnStatusChanged

func (c *Cmd) OnStatusChanged(onStatusChanged StatusChangedFunc) *Cmd

OnStatusChanged 设置状态变更监听

func (*Cmd) PID

func (c *Cmd) PID() PID

PID 获取命令执行的PID

func (*Cmd) QuitWith

func (c *Cmd) QuitWith(quit <-chan struct{}) *Cmd

QuitWith 设置退出信道

func (*Cmd) ReadErr

func (c *Cmd) ReadErr(readErr ReadFunc) *Cmd

ReadErr 读取错误输出

func (*Cmd) ReadOut

func (c *Cmd) ReadOut(readOut ReadFunc) *Cmd

ReadOut 读取正常输入

func (*Cmd) Start

func (c *Cmd) Start() error

Start 运行

func (*Cmd) Status

func (c *Cmd) Status() Status

Status 获取命令执行状态

func (*Cmd) StdIn

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

StdIn 正常输入

func (*Cmd) StdOut

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

StdOut 正常输出

func (*Cmd) Stop

func (c *Cmd) Stop() error

Stop 停止程序

func (*Cmd) String

func (c *Cmd) String() string

String 输出命令行

func (*Cmd) UseWindowsLegacyProcessStrategy

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

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

func (*Cmd) UsedTime

func (c *Cmd) UsedTime() time.Duration

UsedTime 从开始到退出之间的时长

func (*Cmd) Verbose

func (c *Cmd) Verbose(verbose ...bool) *Cmd

Verbose 打印执行的命令

func (*Cmd) Wait

func (c *Cmd) Wait() error

Wait 等待执行完成

type PID

type PID int

PID 进程编号对象

func (PID) Int

func (id PID) Int() int

Int 获取进程编号

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 ReadFunc

type ReadFunc = func(r io.Reader)

ReadFunc 读取方法

func PrintLine

func PrintLine(prefix string) ReadFunc

PrintLine 逐行读取并打印

func ReadByLine

func ReadByLine(read func(line string)) ReadFunc

ReadByLine 逐行读取

type Status

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

func (Status) String

func (code Status) String() string

type StatusChangedFunc

type StatusChangedFunc = func(status Status)

StatusChangedFunc 状态变更委托方法

Jump to

Keyboard shortcuts

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