Documentation ¶
Overview ¶
Package system contain some functions about os, runtime, shell command.
Index ¶
- func CompareOsEnv(key, comparedEnv string) bool
- func CurrentShell(onlyName bool) (path string)
- func ExecCmd(binName string, args []string, workDir ...string) (string, error)
- func ExecCommand(command string, workDir ...string) (stdout, stderr string, err error)
- func FindCommandPath(str string) (string, error)
- func FindUser(uname string) (*user.User, error)
- func GetCurrentUser() *user.User
- func GetLoginUser() *user.User
- func GetOsBits() int
- func GetOsEnv(key string) string
- func HomeDir() string
- func Hostname() string
- func IsAMD64() bool
- func IsARM64() bool
- func IsLinux() bool
- func IsMac() bool
- func IsWindows() bool
- func RemoveOsEnv(key string) error
- func RunCMD(str string, workDir ...string) (string, error)
- func RunCmd(str string, workDir ...string) (string, error)
- func RunCommandWithTimeout(timeout int, workDir string, command string, args ...string) (stdout, stderr string, isKilled bool)
- func SetOsEnv(key, value string) error
- func UserCacheDir(subPath string) string
- func UserConfigDir(subPath string) string
- func UserDir(subPath string) string
- func UserHomeDir() string
- func Where(str string) (string, error)
- func Workdir() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompareOsEnv ¶
CompareOsEnv gets env named by the key and compare it with comparedEnv. Play: https://go.dev/play/p/BciHrKYOHbp
Example ¶
err := SetOsEnv("foo", "abc") if err != nil { return } result := CompareOsEnv("foo", "abc") fmt.Println(result)
Output: true
func CurrentShell ¶
CurrentShell get current used shell env file. eg "/bin/zsh" "/bin/bash". if onlyName=true, will return "zsh", "bash"
func ExecCmd ¶
ExecCmd an command and return output. 指定目录执行shell Usage:
ExecCmd("ls", []string{"-al"})
func ExecCommand ¶
ExecCommand execute command, return the stdout and stderr string of command, and error if error occur param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1 in linux, use /bin/bash -c to execute command in windows, use powershell.exe to execute command Play: https://go.dev/play/p/n-2fLyZef-4
Example ¶
_, stderr, err := ExecCommand("ls") // fmt.Println(stdout) fmt.Println(stderr) fmt.Println(err)
Output: <nil>
func GetLoginUser ¶
GetLoginUser get current user, alias of CurrentUser.
func GetOsBits ¶
func GetOsBits() int
GetOsBits return current os bits (32 or 64). Play: https://go.dev/play/p/ml-_XH3gJbW
Example ¶
osBits := GetOsBits() fmt.Println(osBits)
Output: 64
func GetOsEnv ¶
GetOsEnv gets the value of the environment variable named by the key. Play: https://go.dev/play/p/D88OYVCyjO-
Example ¶
ok := SetOsEnv("foo", "abc") result := GetOsEnv("foo") fmt.Println(ok) fmt.Println(result)
Output: <nil> abc
func IsLinux ¶
func IsLinux() bool
IsLinux check if current os is linux. Play: https://go.dev/play/p/zIflQgZNuxD
func IsMac ¶
func IsMac() bool
IsMac check if current os is macos. Play: https://go.dev/play/p/Mg4Hjtyq7Zc
func IsWindows ¶
func IsWindows() bool
IsWindows check if current os is windows. Play: https://go.dev/play/p/XzJULbzmf9m
func RemoveOsEnv ¶
RemoveOsEnv remove a single environment variable. Play: https://go.dev/play/p/fqyq4b3xUFQ
Example ¶
err1 := SetOsEnv("foo", "abc") result1 := GetOsEnv("foo") err2 := RemoveOsEnv("foo") result2 := GetOsEnv("foo") fmt.Println(err1) fmt.Println(err2) fmt.Println(result1) fmt.Println(result2)
Output: <nil> <nil> abc
func RunCommandWithTimeout ¶
func RunCommandWithTimeout(timeout int, workDir string, command string, args ...string) (stdout, stderr string, isKilled bool)
RunCommandWithTimeout 带超时控制的执行shell命令
func SetOsEnv ¶
SetOsEnv sets the value of the environment variable named by the key. Play: https://go.dev/play/p/D88OYVCyjO-
Example ¶
err := SetOsEnv("foo", "abc") result := GetOsEnv("foo") fmt.Println(err) fmt.Println(result)
Output: <nil> abc
func UserCacheDir ¶
UserCacheDir will prepend user `$HOME/.cache` to subPath
func UserConfigDir ¶
UserConfigDir will prepend user `$HOME/.config` to subPath
Types ¶
This section is empty.