From ae92ae766658481c9b5fb08e4b5bd15d2fbef3a2 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 30 Dec 2022 14:14:22 +0800 Subject: [PATCH] test: add examples for system package --- docs/system.md | 2 +- docs/system_zh-CN.md | 3 +-- system/os.go | 20 ++++++++++++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/system.md b/docs/system.md index 4499487..34f1cc1 100644 --- a/docs/system.md +++ b/docs/system.md @@ -102,7 +102,7 @@ import ( ) func main() { - isOsMac := system.IsMac + isOsMac := system.IsMac() fmt.Println(isOsMac) } ``` diff --git a/docs/system_zh-CN.md b/docs/system_zh-CN.md index c739cb2..9aa3a90 100644 --- a/docs/system_zh-CN.md +++ b/docs/system_zh-CN.md @@ -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) } ``` diff --git a/system/os.go b/system/os.go index b975392..3a85bf1 100644 --- a/system/os.go +++ b/system/os.go @@ -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) }