gosh/interceptor/process.go

78 lines
1.5 KiB
Go

package interceptor
import (
"os"
"runtime"
"strings"
)
// ProcessModule 提供对进程信息和环境变量的访问
type ProcessModule struct{}
// GetEnv 获取环境变量
func (p *ProcessModule) GetEnv(key string) string {
return os.Getenv(key)
}
// SetEnv 设置环境变量
func (p *ProcessModule) SetEnv(key, value string) error {
return os.Setenv(key, value)
}
// GetAllEnv 获取所有环境变量
func (p *ProcessModule) GetAllEnv() map[string]string {
envMap := make(map[string]string)
for _, env := range os.Environ() {
pair := strings.SplitN(env, "=", 2)
if len(pair) == 2 {
envMap[pair[0]] = pair[1]
}
}
return envMap
}
// GetPid 获取当前进程ID
func (p *ProcessModule) GetPid() int {
return os.Getpid()
}
// GetPpid 获取父进程ID
func (p *ProcessModule) GetPpid() int {
return os.Getppid()
}
// GetHostname 获取主机名
func (p *ProcessModule) GetHostname() (string, error) {
return os.Hostname()
}
// GetOS 获取操作系统类型
func (p *ProcessModule) GetOS() string {
return runtime.GOOS
}
// GetArch 获取系统架构
func (p *ProcessModule) GetArch() string {
return runtime.GOARCH
}
// GetCwd 获取当前工作目录
func (p *ProcessModule) GetCwd() (string, error) {
return os.Getwd()
}
// Chdir 改变当前工作目录
func (p *ProcessModule) Chdir(dir string) error {
return os.Chdir(dir)
}
// GetArgs 获取命令行参数
func (p *ProcessModule) GetArgs() []string {
return os.Args
}
// Exit 退出程序
func (p *ProcessModule) Exit(code int) {
os.Exit(code)
}