Documentation ¶
Overview ¶
Package system contain some functions about os, runtime, shell command.
Index ¶
- func CompareOsEnv(key, comparedEnv string) bool
- func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error)
- func GetOsBits() int
- func GetOsEnv(key string) string
- func IsLinux() bool
- func IsMac() bool
- func IsWindows() bool
- func RemoveOsEnv(key string) error
- func SetOsEnv(key, value string) error
- type Option
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 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 GetOsBits ¶ added in v2.1.3
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
Types ¶
type Option ¶ added in v2.1.19
func WithForeground ¶ added in v2.1.19
func WithForeground() Option
func WithWinHide ¶ added in v2.1.20
func WithWinHide() Option