1 Star 0 Fork 0

坐公交也用券 / gcs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
base.go 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
liumou.site@qq.com 提交于 2022-12-15 21:07 . 规范语法
package gcs
import (
"fmt"
"log"
"os/exec"
"os/user"
"strconv"
)
// ShellInit 命令实例构造函数, 当不需要执行sudo命令的时候直接传入任意字符串给password即可
// realtime 是否开启实时打印数据
func ShellInit(password string, realtime bool) *ShellConfig {
shell := new(ShellConfig)
// 系统信息
shell.password = password
get, username, uid, uHome := GetUserInfo(false)
if get {
shell.home = uHome
shell.user = username
shell.uid = uid
} else {
shell.home = "None"
shell.user = "None"
shell.uid = 10000
}
if shell.user == "root" {
shell.isRoot = true
}
// 获取sudo路径
sp, err := ScriptSystem("which sudo")
if err != nil {
return nil
}
shell.SudoPath = sp
shell.debug = false // 调试开关
shell.printer = true // 错误信息打印开关
shell.prints = false // 详细信息打印开关
shell.realtime = realtime // 实时打印信息
return shell
}
// GetUserInfo 获取用户名,用户uid,用户家目录
func GetUserInfo(display bool) (ok bool, username string, userid int, UserHome string) {
var id int
// fmt.Println(path)
currentUser, err := user.Current()
if err != nil {
log.Fatalf(err.Error())
return false, "None", 10000, "None"
}
name := currentUser.Name
id, err = strconv.Atoi(currentUser.Uid)
if err != nil {
id = 10000
}
home := currentUser.HomeDir
if display {
fmt.Println("UserName is: ", name)
fmt.Println("UserId is: ", id)
fmt.Println("UserHome : ", home)
}
return true, name, id, home
}
// CheckCmd 检查命令是否存在
func CheckCmd(cmd string) bool {
// 通过此函数可以从path变量查询命令是否存在,返回命令绝对路径和查找结果
_, err := exec.LookPath(cmd)
return err == nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/liumou_site/gcs.git
git@gitee.com:liumou_site/gcs.git
liumou_site
gcs
gcs
v1.2.9

搜索帮助

344bd9b3 5694891 D2dac590 5694891