cmd

package
v0.0.0-...-b1b21d8 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: LGPL-3.0 Imports: 16 Imported by: 0

README

Function Overview

  • Execute command-line commands
  • Execute cmd commands without memory leaks
  • JavaScript interpreter, used with Node.js, execute JavaScript code using pipes
  • Python interpreter, used with Python, execute Python code using pipes

Execute JavaScript Code Example

package main

import (
	"log"

	"gitee.com/baixudong/gospider/cmd"
)

func TestJs() {
	script := `
	function sign(val,val2){
		return {"signval":val,"signval2":val2}
	}
    function sign2(val,val2){
		return {"sign2val":val,"sign2val2":val2}
	}
	`
	jsCli, err := cmd.NewJsClient(nil, cmd.JsClientOption{
		Script: script,
		Names:  []string{"sign", "sign2"},
	})
	if err != nil {
		log.Fatal(err)
	}
	rs, err := jsCli.Call("sign", 1, 2)
	if err != nil {
		log.Fatal(err)
	}
	if rs.Get("signval").Int() != 1 || rs.Get("signval2").Int() != 2 {
		log.Fatal("sign error")
	}
	rs, err = jsCli.Call("sign2", 1, 2)
	if err != nil {
		log.Fatal(err)
	}
	if rs.Get("sign2val").Int() != 1 || rs.Get("sign2val2").Int() != 2 {
		log.Fatal("sign error")
	}
}

Execute Python Code Example

func TestPy() {
	script := `def sign(val,val2):
	return {"val":val,"val2":val2}`
	pyCli, err := cmd.NewPyClient(nil, cmd.PyClientOption{
		Script: script,
		Names:  []string{"sign"},
	})
	if err != nil {
		log.Fatal(err)
	}
	rs, err := pyCli.Call("sign", 1, 2)
	if err != nil {
		log.Fatal(err)
	}
	if rs.Get("val").Int() != 1 || rs.Get("val2").Int() != 2 {
		log.Fatal("sign error")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("client closed")

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(pre_ctx context.Context, option ClientOption) (*Client, error)

普通的cmd 客户端

func (*Client) Close

func (obj *Client) Close()

关闭客户端

func (*Client) Ctx

func (obj *Client) Ctx() context.Context

func (*Client) Done

func (obj *Client) Done() <-chan struct{}

运行是否结束的 chan

func (*Client) Err

func (obj *Client) Err() error

func (*Client) Join

func (obj *Client) Join()

等待运行结束

func (*Client) Output

func (obj *Client) Output() ([]byte, error)

运行命令

func (*Client) Run

func (obj *Client) Run() error

运行命令

func (*Client) SetStdErr

func (obj *Client) SetStdErr(stderr io.WriteCloser)

设置cmd 的 error管道

func (*Client) SetStdIn

func (obj *Client) SetStdIn(stdin io.ReadCloser)

设置cmd 的 in管道

func (*Client) SetStdOut

func (obj *Client) SetStdOut(stdout io.WriteCloser)

设置cmd 的 out管道

func (*Client) StdErrPipe

func (obj *Client) StdErrPipe() (io.ReadCloser, error)

导出cmd 的error管道

func (*Client) StdInPipe

func (obj *Client) StdInPipe() (io.WriteCloser, error)

导出cmd 的 in管道

func (*Client) StdOutPipe

func (obj *Client) StdOutPipe() (io.ReadCloser, error)

导出cmd 的 out管道

type ClientOption

type ClientOption struct {
	Name          string        //程序执行的名字
	Args          []string      //程序的执行参数
	Dir           string        //程序执行的位置
	TimeOut       time.Duration //程序超时时间
	CloseCallBack func()        //关闭时执行的函数
}

type JsClientOption

type JsClientOption struct {
	Script     string   //加载的js 文件
	Names      []string //要调用的函数名称,只有在这里注册的函数名才能被调用
	NodePath   string   //node 的路径,ex: c:/node.exe
	ModulePath []string //node包搜索路径,如果出现搜索不到包的情况,手动在这里加入路径哈
}

type JyClient

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

func NewJsClient

func NewJsClient(pre_ctx context.Context, option JsClientOption) (*JyClient, error)

创建json解析器

func NewPyClient

func NewPyClient(pre_ctx context.Context, option PyClientOption) (*JyClient, error)

创建py解析器

func (*JyClient) Call

func (obj *JyClient) Call(funcName string, values ...any) (jsonData gjson.Result, err error)

执行函数,第一个参数是要调用的函数名称,后面的是传参

func (*JyClient) Close

func (obj *JyClient) Close()

关闭解析器

type PyClientOption

type PyClientOption struct {
	Script     string   //加载的python 文件
	Names      []string //要调用的函数名称,只有在这里注册的函数名才能被调用
	PythonPath string   //python 的路径,ex: c:/python.exe
	ModulePath []string //python包搜索路径,如果出现搜索不到包的情况,手动在这里加入路径哈
}

Jump to

Keyboard shortcuts

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