1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

test: add examples for system package

This commit is contained in:
dudaodong
2022-12-30 14:14:22 +08:00
parent 526e568d0e
commit ae92ae7666
3 changed files with 16 additions and 9 deletions

View File

@@ -102,7 +102,7 @@ import (
)
func main() {
isOsMac := system.IsMac
isOsMac := system.IsMac()
fmt.Println(isOsMac)
}
```

View File

@@ -26,7 +26,6 @@ import (
- [GetOsEnv](#GetOsEnv)
- [SetOsEnv](#SetOsEnv)
- [RemoveOsEnv](#RemoveOsEnv)
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
@@ -103,7 +102,7 @@ import (
)
func main() {
isOsMac := system.IsMac
isOsMac := system.IsMac()
fmt.Println(isOsMac)
}
```

View File

@@ -15,37 +15,44 @@ import (
"golang.org/x/text/encoding/simplifiedchinese"
)
// IsWindows check if current os is windows
// IsWindows check if current os is windows.
// Play: https://go.dev/play/p/XzJULbzmf9m
func IsWindows() bool {
return runtime.GOOS == "windows"
}
// IsLinux check if current os is linux
// IsLinux check if current os is linux.
// Play: https://go.dev/play/p/zIflQgZNuxD
func IsLinux() bool {
return runtime.GOOS == "linux"
}
// IsMac check if current os is macos
// IsMac check if current os is macos.
// Play: https://go.dev/play/p/Mg4Hjtyq7Zc
func IsMac() bool {
return runtime.GOOS == "darwin"
}
// GetOsEnv gets the value of the environment variable named by the key.
// Play: https://go.dev/play/p/D88OYVCyjO-
func GetOsEnv(key string) string {
return os.Getenv(key)
}
// SetOsEnv sets the value of the environment variable named by the key.
// Play: https://go.dev/play/p/D88OYVCyjO-
func SetOsEnv(key, value string) error {
return os.Setenv(key, value)
}
// RemoveOsEnv remove a single environment variable.
// Play: https://go.dev/play/p/fqyq4b3xUFQ
func RemoveOsEnv(key string) error {
return os.Unsetenv(key)
}
// CompareOsEnv gets env named by the key and compare it with comparedEnv
// CompareOsEnv gets env named by the key and compare it with comparedEnv.
// Play: https://go.dev/play/p/BciHrKYOHbp
func CompareOsEnv(key, comparedEnv string) bool {
env := GetOsEnv(key)
if env == "" {
@@ -58,6 +65,7 @@ func CompareOsEnv(key, comparedEnv string) bool {
// 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
func ExecCommand(command string) (stdout, stderr string, err error) {
var out bytes.Buffer
var errOut bytes.Buffer
@@ -109,8 +117,8 @@ func byteToString(data []byte, charset string) string {
return result
}
// GetOsBits get this system bits 32bit or 64bit
// return bit int (32/64)
// GetOsBits return this system bits (32 or 64)
// Play: https://go.dev/play/p/ml-_XH3gJbW
func GetOsBits() int {
return 32 << (^uint(0) >> 63)
}