代码拉取完成,页面将自动刷新
package gcs
import (
"fmt"
"log"
"os/exec"
"os/user"
"strconv"
)
// 命令实例构造函数, 当不需要执行sudo命令的时候直接传入任意字符串给password即可
// retime 是否开启实时打印数据
func ShellInit(password string, retime bool) *ShellConfig {
shell := new(ShellConfig)
// 系统信息
shell.password = password
get, username, uid, u_home := GetUserInfo(false)
if get {
shell.home = u_home
shell.user = username
shell.uid = uid
} else {
shell.home = "None"
shell.user = "None"
shell.uid = 10000
}
if shell.user == "root" {
shell.is_root = true
}
shell.debug = false // 调试开关
shell.printer = true // 错误信息打印开关
shell.prints = false // 详细信息打印开关
shell.retime = retime // 实时打印信息
return shell
}
// 获取用户名,用户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
}
// 检查命令是否存在
func CheckCmd(cmd string) bool {
// 通过此函数可以从path变量查询命令是否存在,返回命令绝对路径和查找结果
_, err := exec.LookPath(cmd)
return err == nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。