gcs

package module
v1.5.7 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: Apache-2.0 Imports: 12 Imported by: 3

README

gcs

介绍

使用Golang编写的系统命令执行模块,实现类似Python3os.Systemsubprocess.getstatusoutputsubprocess.getoutput

安装教程

执行下面的命令

go get -u gitee.com/liumou_site/gcs

效果如下

PS D:\data\git\Go\glbm> go  get -u gitee.com/liumou_site/gcs   
go: downloading gitee.com/liumou_site/gcs v1.1.0
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
go: added gitee.com/liumou_site/gcs v1.1.0
PS D:\data\git\Go\glbm> 

使用说明

package demo

import (
	"fmt"
	"gitee.com/liumou_site/gcs"
	"gitee.com/liumou_site/logger"
)

func GcsShell(cmd string) {
	shell := gcs.NewShell()
	shell.RunShell(cmd)
	if shell.Err != nil {
		logger.Error("执行失败")
	} else {
		logger.Info("执行成功")
		fmt.Println(shell.Strings)
	}
}

func GcsSudo(cmd, password string)  {
	s := gcs.NewSudo(password)
	s.Realtime = true // 开启实时打印
	s.RunShell(cmd)
	s.Line(1).Column(5, " ")
	fmt.Println(s.Strings)
}

常用参数说明

参数名称 作用
Realtime bool 实时打印命令执行反馈信息
Strings String 存储命令执行反馈文本信息

更多参数请查阅api.go文件

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckCmd

func CheckCmd(cmd string) bool

CheckCmd 检查命令是否存在

func GetUserInfo

func GetUserInfo(display bool) (ok bool, username string, userid int, UserHome string)

GetUserInfo 获取用户名,用户uid,用户家目录

func Version

func Version() string

Types

type ApiHandle added in v1.5.7

type ApiHandle struct {
	Strings string   // 处理结果
	Err     error    // 错误
	Slice   []string // 切片
	// contains filtered or unexported fields
}

func (*ApiHandle) Column added in v1.5.7

func (api *ApiHandle) Column(col int, sep string) *ApiHandle

Column 截取指定列

func (*ApiHandle) Grep added in v1.5.7

func (api *ApiHandle) Grep(match string) *ApiHandle

func (*ApiHandle) Line added in v1.5.7

func (api *ApiHandle) Line(n int) *ApiHandle

Line 截取指定行

type ApiShell added in v1.4.7

type ApiShell struct {
	Printer bool // 打印详细错误信息(默认: true)
	Prints  bool // 是否打印详细信息(默认: false)
	Debug   bool // 是否打印调试信息(默认: false)

	Realtime bool   // 是否开启实时打印信息
	Result   bool   // 是否执行成功
	Text     string // 需要执行的命令
	OsType   string // 系统类型

	Strings  string // 输出结果
	ExitCode int    // 命令执行退出代码
	Err      error  // 错误

	Slice []string // 数据切片
	// contains filtered or unexported fields
}

ApiShell 定义命令的结构体

func NewShell added in v1.4.6

func NewShell() *ApiShell

NewShell 命令实例构造函数 Realtime 是否开启实时打印数据

func (*ApiShell) Column added in v1.5.5

func (api *ApiShell) Column(col int, sep string) *ApiShell

Column 截取指定列

func (*ApiShell) Echo added in v1.5.5

func (api *ApiShell) Echo()

Echo 打印命令执行反馈信息

func (*ApiShell) Grep added in v1.5.7

func (api *ApiShell) Grep(match string) *ApiShell

func (*ApiShell) Line added in v1.5.5

func (api *ApiShell) Line(n int) *ApiShell

Line 截取指定行

func (*ApiShell) RunShell added in v1.4.7

func (api *ApiShell) RunShell(command ...string)

RunShell 执行命令

type ApiSudo added in v1.4.7

type ApiSudo struct {
	Printer  bool   // 打印详细错误信息(默认: true)
	Prints   bool   // 是否打印详细信息(默认: false)
	Debug    bool   // 是否打印调试信息(默认: false)
	Password string // sudo权限使用的密码

	Realtime bool     // 是否开启实时打印信息
	Result   bool     // 是否执行成功
	SudoPath string   // sudo命令绝对路径
	Text     string   // 需要执行的命令
	OsType   string   // 系统类型
	Shell    ApiShell // 实例化Shell

	Strings  string // 输出结果
	ExitCode int    // 命令执行退出代码
	Err      error  // 错误

	Slice []string // 数据切片
	// contains filtered or unexported fields
}

ApiSudo 定义命令的结构体

func NewSudo added in v1.4.6

func NewSudo(password string) *ApiSudo

NewSudo 命令实例构造函数, 当不需要执行sudo命令的时候直接传入任意字符串给password即可 Realtime 是否开启实时打印数据

func (*ApiSudo) Column added in v1.5.5

func (api *ApiSudo) Column(col int, sep string) *ApiSudo

Column 截取指定列

func (*ApiSudo) Echo added in v1.5.5

func (api *ApiSudo) Echo()

Echo 打印命令执行反馈信息

func (*ApiSudo) Line added in v1.5.5

func (api *ApiSudo) Line(n int) *ApiSudo

Line 截取指定行

func (*ApiSudo) RunShell added in v1.4.7

func (api *ApiSudo) RunShell(command ...string)

RunShell 执行命令

Jump to

Keyboard shortcuts

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