sysutil

package
v0.5.14 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2022 License: MIT Imports: 18 Imported by: 32

README

System Utils

Provide some system util functions. eg: sysenv, exec, user, process

  • quick exec a command line string.
  • quick build a command for run

Install

go get github.com/gookit/goutil/sysutil

Usage

out, err := sysutil.ExecCmd("ls", []string{"-al"})

Clipboard

Package clipboard provide a simple clipboard read and write operations.

Install
go get github.com/gookit/goutil/sysutil/clipboard
Usage

Examples:

src := "hello, this is clipboard"
err = clipboard.WriteString(src)
assert.NoErr(t, err)

// str: "hello, this is clipboard"
str, err = clipboard.ReadString()
assert.NoErr(t, err)
assert.NotEmpty(t, str)
assert.Eq(t, src, str)

Cmd run

Install
go get github.com/gookit/goutil/sysutil/cmdr
Usage

Cmd Builder:

c := cmdr.NewCmd("ls").
    WithArg("-l").
    WithArgs([]string{"-h"}).
    AddArg("-a").
    AddArgf("%s", "./")

c.OnBefore(func(c *cmdr.Cmd) {
    assert.Eq(t, "ls -l -h -a ./", c.Cmdline())
})

out := c.SafeOutput()
fmt.Println(out)

Batch Cmd Tasks:

Can use cmdr.Runner run multi cmd tasks at once.

buf := new(bytes.Buffer)
rr := cmdr.NewRunner()

rr.Add(&cmdr.Task{
    ID:  "task1",
    Cmd: cmdr.NewCmd("id", "-F").WithOutput(buf, buf),
})
rr.AddCmd(cmdr.NewCmd("ls").AddArgs([]string{"-l", "-h"}).WithOutput(buf, buf))

err = rr.Run()

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BinDir added in v0.4.5

func BinDir() string

BinDir get

func BinFile added in v0.4.5

func BinFile() string

BinFile get

func ChangeUserByName added in v0.4.2

func ChangeUserByName(newUname string) (err error)

ChangeUserByName change work user by new username.

func ChangeUserUidGid added in v0.4.2

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

ChangeUserUidGid change work user by new username uid,gid

func CurrentShell added in v0.1.4

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 CurrentUser added in v0.4.3

func CurrentUser() *user.User

CurrentUser must get current user

func EnvPaths added in v0.5.11

func EnvPaths() []string

EnvPaths get and split $PATH to []string

func ExecCmd added in v0.1.4

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

ExecCmd a command and return output.

Usage:

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

func ExecLine added in v0.3.12

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

ExecLine quick exec an command line string

func Executable added in v0.3.12

func Executable(binName string) (string, error)

Executable find in the system

Usage:

sysutil.Executable("bash")

func ExpandPath added in v0.3.9

func ExpandPath(path string) string

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

func FindExecutable added in v0.3.8

func FindExecutable(binName string) (string, error)

FindExecutable in the system

Usage:

sysutil.FindExecutable("bash")

func FlushExec added in v0.5.11

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

FlushExec instance

func GoVersion added in v0.5.11

func GoVersion() string

GoVersion get go runtime version. eg: "1.18.2"

func HasExecutable added in v0.3.8

func HasExecutable(binName string) bool

HasExecutable in the system

Usage:

HasExecutable("bash")

func HasShellEnv added in v0.1.4

func HasShellEnv(shell string) bool

HasShellEnv has shell env check.

Usage:

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

func HomeDir added in v0.3.9

func HomeDir() string

HomeDir get user home dir path.

func Hostname added in v0.3.12

func Hostname() string

Hostname is alias of os.Hostname, but ignore error

func IsConsole added in v0.2.2

func IsConsole(out io.Writer) bool

IsConsole check out is in stderr/stdout/stdin

Usage:

sysutil.IsConsole(os.Stdout)

func IsDarwin added in v0.5.6

func IsDarwin() bool

IsDarwin system

func IsLinux added in v0.2.2

func IsLinux() bool

IsLinux system

func IsMSys added in v0.2.2

func IsMSys() bool

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

func IsMac added in v0.2.2

func IsMac() bool

IsMac system

func IsShellSpecialVar added in v0.3.12

func IsShellSpecialVar(c uint8) bool

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

func IsTerminal added in v0.3.9

func IsTerminal(fd uintptr) bool

IsTerminal isatty check

Usage:

sysutil.IsTerminal(os.Stdout.Fd())

func IsWin added in v0.2.2

func IsWin() bool

IsWin system. linux windows darwin

func IsWindows added in v0.2.3

func IsWindows() bool

IsWindows system. linux windows darwin

func Kill

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

Kill a process by pid

func LoginUser added in v0.3.13

func LoginUser() *user.User

LoginUser must get current user

func MustFindUser added in v0.4.2

func MustFindUser(uname string) *user.User

MustFindUser must find an system user by name

func NewCmd added in v0.5.11

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

NewCmd instance

func OpenBrowser added in v0.5.6

func OpenBrowser(URL string) error

OpenBrowser Open browser URL

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

func ProcessExists(pid int) bool

ProcessExists check process exists by pid

func QuickExec added in v0.1.4

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

QuickExec quick exec an simple command line

func SearchPath added in v0.5.11

func SearchPath(keywords string) []string

SearchPath search executable files in the system $PATH

Usage:

sysutil.SearchPath("go")

func ShellExec added in v0.1.4

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

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

func StdIsTerminal added in v0.3.11

func StdIsTerminal() bool

StdIsTerminal os.Stdout is terminal

func UHomeDir added in v0.3.13

func UHomeDir() string

UHomeDir get user home dir path.

func UserCacheDir added in v0.4.0

func UserCacheDir(subPath string) string

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

func UserConfigDir added in v0.4.0

func UserConfigDir(subPath string) string

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

func UserDir added in v0.4.0

func UserDir(subPath string) string

UserDir will prepend user home dir to subPath

func UserHomeDir added in v0.3.12

func UserHomeDir() string

UserHomeDir is alias of os.UserHomeDir, but ignore error

func Workdir added in v0.4.2

func Workdir() string

Workdir get

Types

type CallerInfo added in v0.5.6

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

CallerInfo struct

func CallersInfos added in v0.5.6

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

CallersInfos returns an array of the CallerInfo.

Usage:

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

func (*CallerInfo) String added in v0.5.10

func (ci *CallerInfo) String() string

String convert

type GoInfo added in v0.5.11

type GoInfo struct {
	Version string
	GoOS    string
	Arch    string
}

GoInfo define

On os by:

go env GOVERSION GOOS GOARCH
go version // "go version go1.19 darwin/amd64"

func OsGoInfo added in v0.5.11

func OsGoInfo() (*GoInfo, error)

OsGoInfo fetch and parse

func ParseGoVersion added in v0.5.11

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

ParseGoVersion get info by parse `go version` results.

Examples:

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

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

Directories

Path Synopsis
Package clipboard provide a simple clipboard read and write operations.
Package clipboard provide a simple clipboard read and write operations.
Package cmdr Provide for quick build and run a cmd, batch run multi cmd tasks
Package cmdr Provide for quick build and run a cmd, batch run multi cmd tasks
Package process Provide some process handle util functions
Package process Provide some process handle util functions

Jump to

Keyboard shortcuts

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