Skip to content

Import path: gitlab.soludian.com/soludian/fountain/libs/base/sys_util

sys_util

go
import "gitlab.soludian.com/soludian/fountain/libs/base/sys_util"

Package sys_util provide some system util functions. eg: sysenv, exec, user, process

Index

func BinDir

go
func BinDir() string

BinDir get

func BinFile

go
func BinFile() string

BinFile get

func BinName

go
func BinName() string

BinName get

func CallerName

go
func CallerName(skip int) string

CallerName ...

func ChangeUserByName

go
func ChangeUserByName(newUname string) (err error)

ChangeUserByName change work user by new username.

func ChangeUserUIDGid

go
func ChangeUserUIDGid(newUid int, newGid int) (err error)

ChangeUserUIDGid change work user by new username uid,gid

func ChangeUserUidGid

go
func ChangeUserUidGid(newUid int, newGid int) (err error)

ChangeUserUidGid change work user by new username uid,gid

Deprecated: use ChangeUserUIDGid instead

func CurrentShell

go
func CurrentShell(onlyName bool) (path string)

CurrentShell get current used shell env file.

eg "/bin/zsh" "/bin/bash". if onlyName=true, will return "zsh", "sh"

func CurrentUser

go
func CurrentUser() *user.User

CurrentUser must get current user

func EnvMapWith

go
func EnvMapWith(newEnv map[string]string) map[string]string

EnvMapWith like os.Environ, but will return key-value map[string]string data.

func EnvPaths

go
func EnvPaths() []string

EnvPaths get and split $PATH to []string

func Environ

go
func Environ() map[string]string

Environ like os.Environ, but will returns key-value map[string]string data.

func ExecCmd

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

ExecCmd a command and return output.

Usage:

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

func ExecLine

go
func ExecLine(cmdLine string, workDir ...string) (string, error)

ExecLine quick exec an command line string

func Executable

go
func Executable(binName string) (string, error)

Executable find in the system, alias of FindExecutable()

Usage:

sys_util.Executable("sh")

func ExpandHome

go
func ExpandHome(path string) string

ExpandHome will parse `~` as user home dir path.

func ExpandPath

go
func ExpandPath(path string) string

ExpandPath will parse `~` as user home dir path.

func ExternalIP

go
func ExternalIP() (string, error)

ExternalIP func;

func FindExecutable

go
func FindExecutable(binName string) (string, error)

FindExecutable in the system

Usage:

sys_util.FindExecutable("sh")

func FlushExec

go
func FlushExec(bin string, args ...string) error

FlushExec instance

func GetCallerPackageName

go
func GetCallerPackageName(skips ...int) string

func GetCallerPackageNamePath

go
func GetCallerPackageNamePath(skips ...int) string

func GetLocalMainIP

go
func GetLocalMainIP() (string, int, error)

GetLocalMainIP ...

func Getenv

go
func Getenv(name string, def ...string) string

Getenv get ENV value by key name, can with default value

func GoVersion

go
func GoVersion() string

GoVersion get go runtime version. eg: "1.18.2"

func HasExecutable

go
func HasExecutable(binName string) bool

HasExecutable in the system

Usage:

HasExecutable("sh")

func HasShellEnv

go
func HasShellEnv(shell string) bool

HasShellEnv has shell env check.

Usage:

HasShellEnv("sh")
HasShellEnv("sh")

func HomeDir

go
func HomeDir() string

HomeDir get user home dir path.

func Hostname

go
func Hostname() string

Hostname is alias of os.Hostname, but ignore error

func IsConsole

go
func IsConsole(out io.Writer) bool

IsConsole check out is in stderr/stdout/stdin

Usage:

sys_util.IsConsole(os.Stdout)

func IsDarwin

go
func IsDarwin() bool

IsDarwin system

func IsLinux

go
func IsLinux() bool

IsLinux system

func IsMSys

go
func IsMSys() bool

IsMSys msys(MINGW64) env,不一定支持颜色

func IsMac

go
func IsMac() bool

IsMac system

func IsShellSpecialVar

go
func IsShellSpecialVar(c uint8) bool

IsShellSpecialVar reports whether the character identifies a special shell variable such as $*.

func IsTerminal

go
func IsTerminal(fd uintptr) bool

IsTerminal isatty check

Usage:

sys_util.IsTerminal(os.Stdout.Fd())

func IsWin

go
func IsWin() bool

IsWin system. linux windows darwin

func IsWindows

go
func IsWindows() bool

IsWindows system. linux windows darwin

func Kill

go
func Kill(pid int, signal syscall.Signal) error

Kill a process by pid

func LoginUser

go
func LoginUser() *user.User

LoginUser must get current user

func MustFindUser

go
func MustFindUser(uname string) *user.User

MustFindUser must find an system user by name

func NewCmd

go
func NewCmd(bin string, args ...string) *cmdr.Cmd

NewCmd instance

func Open

go
func Open(fileOrURL string) error

Open file or url address

func OpenBrowser

go
func OpenBrowser(fileOrURL string) error

OpenBrowser file or url address

func OpenFile

go
func OpenFile(path string) error

OpenFile opens new browser window for the file path.

func OpenURL

go
func OpenURL(url string) error

OpenURL Open file or browser URL

- refers https://github.com/pkg/browser

Mac:

open 'https://github.com/inhere'

Linux:

xdg-open URL
x-www-browser 'https://github.com/inhere'

Windows:

cmd /c start https://github.com/inhere

func ProcessExists

go
func ProcessExists(pid int) bool

ProcessExists check process exists by pid

func QuickExec

go
func QuickExec(cmdLine string, workDir ...string) (string, error)

QuickExec quick exec an simple command line

func SearchPath

go
func SearchPath(keywords string, limit int) []string

SearchPath search executable files in the system $PATH

Usage:

sys_util.SearchPath("go")

func ShellExec

go
func ShellExec(cmdLine string, shells ...string) (string, error)

ShellExec exec command by shell cmdLine. eg: "ls -al"

func StdIsTerminal

go
func StdIsTerminal() bool

StdIsTerminal os.Stdout is terminal

func UHomeDir

go
func UHomeDir() string

UHomeDir get user home dir path.

func UserCacheDir

go
func UserCacheDir(subPath string) string

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

func UserConfigDir

go
func UserConfigDir(subPath string) string

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

func UserDir

go
func UserDir(subPath string) string

UserDir will prepend user home dir to subPath

func UserHomeDir

go
func UserHomeDir() string

UserHomeDir is alias of os.UserHomeDir, but ignore error

func WorkDir

go
func WorkDir() string

WorkDir get

type CallerInfo

CallerInfo struct

go
type CallerInfo struct {
    PC   uintptr
    Fc   *runtime.Func
    File string
    Line int
}

func CallersInfos

go
func CallersInfos(skip, num int, filters ...func(file string, fc *runtime.Func) bool) []*CallerInfo

CallersInfos returns an array of the CallerInfo.

Usage:

cs := sys_util.CallersInfos(3, 2)
 for _, ci := range cs {
		fc := runtime.FuncForPC(pc)
		// maybe need check fc = nil
		fnName = fc.Name()
	}

func (*CallerInfo) String

go
func (ci *CallerInfo) String() string

String convert

type GoInfo

GoInfo define. alias of goinfo.GoInfo

go
type GoInfo = goinfo.GoInfo

func OsGoInfo

go
func OsGoInfo() (*GoInfo, error)

OsGoInfo fetch and parse. alias of goinfo.OsGoInfo()

func ParseGoVersion

go
func ParseGoVersion(line string) (*GoInfo, error)

ParseGoVersion get info by parse `go version` results. alias of goinfo.ParseGoVersion()

Examples:

line, err := sys_util.ExecLine("go version")
	if err != nil {
		return err
	}

	info, err := sys_util.ParseGoVersion()
 	dump.P(info)

Generated by gomarkdoc