gcmd

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: Apache-2.0 Imports: 10 Imported by: 1

README

gcmd

This package is simple to run cmd.

installation
go get github.com/PeterYangs/gcmd
Quick start
package main

import "github.com/PeterYangs/gcmd"

func main() {

	gcmd.Command("php index.php").Start()

}

php script is

<?php

$index=0;

while (true){

    if($index>=10){

        throw new Exception("error here!");

    }

    echo 'echo success'.PHP_EOL;

    $index++;

    sleep(1);

}

Console output

echo success
echo success
echo success
echo success
echo success
echo success
echo success
echo success
echo success
echo success

Fatal error: Uncaught Exception: error here! in D:\goDemo\cmd\index.php:9
Stack trace:
#0 {main}
  thrown in D:\goDemo\cmd\index.php on line 9
wait err: exit status 255
Custom output
package main

import (
	"fmt"
	"github.com/PeterYangs/gcmd"
)

func main() {

	command := gcmd.Command("php index.php").WaitCustomChan()

	outChan := command.GetCustomOutChan()
	errChan := command.GetCustomErrChan()

	go func() {

		for t := range outChan {

			fmt.Println(string(t))
		}

		fmt.Println("out chan close")

		command.Done()

	}()

	go func() {

		for t := range errChan {

			fmt.Println("err:", string(t))
		}

		fmt.Println("err chan close")

		command.Done()

	}()

	command.Start()

}

Throw Panic
package main

import (
	"github.com/PeterYangs/gcmd"
)

func main() {

	gcmd.Command("php index.php").ThrowPanic().Start()

}

Return Output
package main

import (
	"fmt"
	"github.com/PeterYangs/gcmd"
)

func main() {

	out, err := gcmd.Command("php index.php").OutPut().Start()

	if err != nil {

		fmt.Println(err)

		return
	}

	fmt.Println(string(out))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	Command   string //命令行
	Cmd       *exec.Cmd
	CtxCancel context.CancelFunc
	Ctx       context.Context
	OutPutBuf *bufio.Reader //命令输出流
	ErrPutBuf *bufio.Reader //错误输出流
	// contains filtered or unexported fields
}

func Command

func Command(command string) *Cmd

func (*Cmd) ConvertUtf8

func (c *Cmd) ConvertUtf8() *Cmd

func (*Cmd) Done

func (c *Cmd) Done()

Done customchan done

func (*Cmd) GetCustomErrChan

func (c *Cmd) GetCustomErrChan() chan []byte

func (*Cmd) GetCustomOutChan

func (c *Cmd) GetCustomOutChan() chan []byte

func (*Cmd) OutPut added in v0.0.4

func (c *Cmd) OutPut() *Cmd

func (*Cmd) SetTimeout

func (c *Cmd) SetTimeout(timeout time.Duration) *Cmd

SetTimeout cxt timeout

func (*Cmd) Start

func (c *Cmd) Start() ([]byte, error)

Start run command

func (*Cmd) ThrowPanic added in v0.0.3

func (c *Cmd) ThrowPanic() *Cmd

ThrowPanic 出错抛出异常

func (*Cmd) WaitCustomChan

func (c *Cmd) WaitCustomChan() *Cmd

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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