Categorygithub.com/zdz1715/go-sh
modulepackage
0.1.4
Repository: https://github.com/zdz1715/go-sh.git
Documentation: pkg.go.dev

# README

go-sh

go写的shell脚本处理包,为提高工作效率而开发

  • 支持执行多条命令,并等待所有后台任务结束
  • 自定义实时输出方式
  • 自定义执行ID生成方式,便于追踪执行记录
  • 可快捷指定shell类型和Set-Builtin
  • 支持获取执行完所在的工作目录,便于设置下一次执行的工作目录
  • 支持根据命令生成脚本文件去执行,可存储每次执行脚本
  • 可全局设置一些选项,减少每次生成去设置的工作量

Contents

Installation

go get -u github.com/zdz1715/go-sh@latest

Quick start

package main

import (
	"fmt"
	"os"

	"github.com/zdz1715/go-sh"
)

func main() {
	e, err := sh.NewExec()
	if err != nil {
		fmt.Printf("new exec fail:%s\n", err)
		return
	}

	dir, _ := os.Getwd()
	fmt.Printf("[%s] %s\n", dir, e.String())

	e.AddCommand("echo", "pwd: $PWD")
	e.AddCommand("cd", "/")
	// 后台执行
	e.AddCommand("sleep 5 &")

	// 添加多行脚本,配合存储可以生成脚本文件,不调用Run()就不会执行
	e.AddRawCommand([]byte(`
# hello world
hello() {
	echo hello world
}

hello
`))

	if err = e.Run("echo command-1 | cut -d '-' -f2", "echo command2"); err != nil {
		fmt.Printf("exec fail:%s\n", err)
	}

	fmt.Println("exec last work dir:", e.GetLastWorkDir())
}

/*
[go-sh] /bin/bash -ex -o pipefail
+ cd /
+ hello
+ echo hello world
hello world
+ sleep 5
+ echo command-1
+ cut -d - -f2
1
+ echo command2
command2
exec last work dir: /
*/

Examples

全局设置执行选项

如果没有单独的设置,全局设置则会覆盖,有则不会覆盖

package main

import (
	"fmt"

	"github.com/zdz1715/go-sh/shell"

	"github.com/zdz1715/go-sh"
)

func main() {
	// 设置全局执行的工作目录
	sh.SetGlobalExecWorkDir("/usr")
	// 设置全局执行的用户
	sh.SetGlobalExecUser("")
	// 设置全局执行的输出方法
	sh.SetGlobalExecOutput(func(num int, line []byte) {
		fmt.Println(string(line))
	})
	// 设置全局执行脚本存储方式
	sh.SetGlobalStorage(&sh.Storage{
		Dir: "/tmp",
		//NotAutoClean: true,
	})
	// 设置全局的id生成方式
	sh.SetGlobalIDCreator(func() string {
		return "jenkins-" + sh.XidCreator()
	})

	// 设置全局的shell 类型
	sh.SetGlobalShell(&shell.Shell{
		Type: shell.Sh,
	})

	e1, _ := sh.NewExec()
	e2, _ := sh.NewExec()
	e3, _ := sh.NewExec()
	e1.Run("echo e1: $(pwd)")
	e2.Run("echo e2: $(pwd)")
	e3.Run("echo e3: $(pwd)")
}

# Packages

No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SetGlobalExecOutput Sets the output func for execution globally.
SetGlobalExecUser Sets the user for execution globally.
SetGlobalExecWorkDir Sets the working directory for execution globally.
SetGlobalIDCreator Sets the ID Creator for execution globally.
SetGlobalShell Sets the shell for execution globally.
SetGlobalStorage Sets the storage for execution globally.

# Variables

No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author