1 Star 11 Fork 3

ZX/php-code-generator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
os.go 1.91 KB
一键复制 编辑 原始数据 按行查看 历史
ZX 提交于 2023-09-16 14:26 +08:00 . 增加goravel工具库
package sysos
import (
"bytes"
"errors"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
)
// IsWindows determines whether current OS is Windows.
func IsWindows() bool {
return "windows" == runtime.GOOS
}
// IsLinux determines whether current OS is Linux.
func IsLinux() bool {
return "linux" == runtime.GOOS
}
// IsDarwin determines whether current OS is Darwin.
func IsDarwin() bool {
return "darwin" == runtime.GOOS
}
// Pwd gets the path of current working directory.
func Pwd() string {
file, _ := exec.LookPath(os.Args[0])
pwd, _ := filepath.Abs(file)
return filepath.Dir(pwd)
}
// Home returns the home directory for the executing user.
//
// This uses an OS-specific method for discovering the home directory.
// An error is returned if a home directory cannot be detected.
func Home() (string, error) {
user, err := user.Current()
if nil == err {
return user.HomeDir, nil
}
// cross compile support
if IsWindows() {
return homeWindows()
}
// Unix-like system, so just assume Unix
return homeUnix()
}
func homeUnix() (string, error) {
// First prefer the HOME environmental variable
if home := os.Getenv("HOME"); home != "" {
return home, nil
}
// If that fails, try the shell
var stdout bytes.Buffer
cmd := exec.Command("sh", "-c", "eval echo ~$USER")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}
result := strings.TrimSpace(stdout.String())
if result == "" {
return "", errors.New("blank output when reading home directory")
}
return result, nil
}
func homeWindows() (string, error) {
drive := os.Getenv("HOMEDRIVE")
path := os.Getenv("HOMEPATH")
home := drive + path
if drive == "" || path == "" {
home = os.Getenv("USERPROFILE")
}
if home == "" {
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
}
return home, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/open-php/php-code-generator.git
git@gitee.com:open-php/php-code-generator.git
open-php
php-code-generator
php-code-generator
v0.1.0

搜索帮助