mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-12 16:52:29 +08:00
test: add examples for system package
This commit is contained in:
@@ -102,7 +102,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isOsMac := system.IsMac
|
isOsMac := system.IsMac()
|
||||||
fmt.Println(isOsMac)
|
fmt.Println(isOsMac)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
- [GetOsEnv](#GetOsEnv)
|
- [GetOsEnv](#GetOsEnv)
|
||||||
- [SetOsEnv](#SetOsEnv)
|
- [SetOsEnv](#SetOsEnv)
|
||||||
- [RemoveOsEnv](#RemoveOsEnv)
|
- [RemoveOsEnv](#RemoveOsEnv)
|
||||||
|
|
||||||
- [CompareOsEnv](#CompareOsEnv)
|
- [CompareOsEnv](#CompareOsEnv)
|
||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
- [GetOsBits](#GetOsBits)
|
- [GetOsBits](#GetOsBits)
|
||||||
@@ -103,7 +102,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
isOsMac := system.IsMac
|
isOsMac := system.IsMac()
|
||||||
fmt.Println(isOsMac)
|
fmt.Println(isOsMac)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
20
system/os.go
20
system/os.go
@@ -15,37 +15,44 @@ import (
|
|||||||
"golang.org/x/text/encoding/simplifiedchinese"
|
"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 {
|
func IsWindows() bool {
|
||||||
return runtime.GOOS == "windows"
|
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 {
|
func IsLinux() bool {
|
||||||
return runtime.GOOS == "linux"
|
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 {
|
func IsMac() bool {
|
||||||
return runtime.GOOS == "darwin"
|
return runtime.GOOS == "darwin"
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOsEnv gets the value of the environment variable named by the key.
|
// GetOsEnv gets the value of the environment variable named by the key.
|
||||||
|
// Play: https://go.dev/play/p/D88OYVCyjO-
|
||||||
func GetOsEnv(key string) string {
|
func GetOsEnv(key string) string {
|
||||||
return os.Getenv(key)
|
return os.Getenv(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetOsEnv sets the value of the environment variable named by the 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 {
|
func SetOsEnv(key, value string) error {
|
||||||
return os.Setenv(key, value)
|
return os.Setenv(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveOsEnv remove a single environment variable.
|
// RemoveOsEnv remove a single environment variable.
|
||||||
|
// Play: https://go.dev/play/p/fqyq4b3xUFQ
|
||||||
func RemoveOsEnv(key string) error {
|
func RemoveOsEnv(key string) error {
|
||||||
return os.Unsetenv(key)
|
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 {
|
func CompareOsEnv(key, comparedEnv string) bool {
|
||||||
env := GetOsEnv(key)
|
env := GetOsEnv(key)
|
||||||
if env == "" {
|
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
|
// 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 linux, use /bin/bash -c to execute command
|
||||||
// in windows, use powershell.exe 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) {
|
func ExecCommand(command string) (stdout, stderr string, err error) {
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
var errOut bytes.Buffer
|
var errOut bytes.Buffer
|
||||||
@@ -109,8 +117,8 @@ func byteToString(data []byte, charset string) string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOsBits get this system bits 32bit or 64bit
|
// GetOsBits return this system bits (32 or 64)
|
||||||
// return bit int (32/64)
|
// Play: https://go.dev/play/p/ml-_XH3gJbW
|
||||||
func GetOsBits() int {
|
func GetOsBits() int {
|
||||||
return 32 << (^uint(0) >> 63)
|
return 32 << (^uint(0) >> 63)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user