system

package
v0.0.0-...-d1e1ff0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package system contain some functions about os, runtime, shell command.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareOsEnv

func CompareOsEnv(key, comparedEnv string) bool

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

func CurrentShell(onlyName bool) (path string)

CurrentShell get current used shell env file. eg "/bin/zsh" "/bin/bash". if onlyName=true, will return "zsh", "bash"

func ExecCmd

func ExecCmd(binName string, args []string, workDir ...string) (string, error)

ExecCmd an command and return output. 指定目录执行shell Usage:

ExecCmd("ls", []string{"-al"})

func ExecCommand

func ExecCommand(command string, workDir ...string) (stdout, stderr string, err error)

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 FindCommandPath

func FindCommandPath(str string) (string, error)

FindCommandPath 获取命令的路径

func FindUser

func FindUser(uname string) (*user.User, error)

FindUser find an system user by name.

func GetCurrentUser

func GetCurrentUser() *user.User

CurrentUser get current user.

func GetLoginUser

func GetLoginUser() *user.User

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

func GetOsEnv(key string) string

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 HomeDir

func HomeDir() string

HomeDir get user home dir path, alias .

func Hostname

func Hostname() string

Hostname is alias of os.Hostname, but ignore error

func IsAMD64

func IsAMD64() bool

IsAMD64 check if current arch is amd64 or x86_64

func IsARM64

func IsARM64() bool

IsARM64 check if current arch is arm64 or aarch

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

func RemoveOsEnv(key string) error

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 RunCMD

func RunCMD(str string, workDir ...string) (string, error)

RunCMD 标准正确错误输出到标准正确输出

func RunCmd

func RunCmd(str string, workDir ...string) (string, error)

RunCmd 获取标准正确输出

func RunCommandWithTimeout

func RunCommandWithTimeout(timeout int, workDir string, command string, args ...string) (stdout, stderr string, isKilled bool)

RunCommandWithTimeout 带超时控制的执行shell命令

func SetOsEnv

func SetOsEnv(key, value string) error

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

func UserCacheDir(subPath string) string

UserCacheDir will prepend user `$HOME/.cache` to subPath

func UserConfigDir

func UserConfigDir(subPath string) string

UserConfigDir will prepend user `$HOME/.config` to subPath

func UserDir

func UserDir(subPath string) string

UserDir will prepend user home dir to subPath

func UserHomeDir

func UserHomeDir() string

UserHomeDir get user home dir path.

func Where

func Where(str string) (string, error)

Where 获取命令的路径,FindCommandPath的别名

func Workdir

func Workdir() string

Workdir get

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL