sysutil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

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/zhangyiming748/pretty/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/zhangyiming748/pretty/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/zhangyiming748/pretty/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()
Functions API
func BinDir() string
func BinFile() string
func ChangeUserByName(newUname string) (err error)
func ChangeUserUidGid(newUid int, newGid int) (err error)
func CurrentShell(onlyName bool) (path string)
func CurrentUser() *user.User
func EnvPaths() []string
func ExecCmd(binName string, args []string, workDir ...string) (string, error)
func ExecLine(cmdLine string, workDir ...string) (string, error)
func Executable(binName string) (string, error)
func ExpandPath(path string) string
func FindExecutable(binName string) (string, error)
func FlushExec(bin string, args ...string) error
func GoVersion() string
func HasExecutable(binName string) bool
func HasShellEnv(shell string) bool
func HomeDir() string
func Hostname() string
func IsConsole(out io.Writer) bool
func IsDarwin() bool
func IsLinux() bool
func IsMSys() bool
func IsMac() bool
func IsShellSpecialVar(c uint8) bool
func IsTerminal(fd uintptr) bool
func IsWin() bool
func IsWindows() bool
func Kill(pid int, signal syscall.Signal) error
func LoginUser() *user.User
func MustFindUser(uname string) *user.User
func NewCmd(bin string, args ...string) *cmdr.Cmd
func OpenBrowser(URL string) error
func ProcessExists(pid int) bool
func QuickExec(cmdLine string, workDir ...string) (string, error)
func SearchPath(keywords string) []string
func ShellExec(cmdLine string, shells ...string) (string, error)
func StdIsTerminal() bool
func UHomeDir() string
func UserCacheDir(subPath string) string
func UserConfigDir(subPath string) string
func UserDir(subPath string) string
func UserHomeDir() string
func Workdir() string
type CallerInfo struct{ ... }
    func CallersInfos(skip, num int, filters ...func(file string, fc *runtime.Func) bool) []*CallerInfo
type GoInfo struct{ ... }
    func OsGoInfo() (*GoInfo, error)
    func ParseGoVersion(line string) (*GoInfo, error)

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

func BinDir() string

BinDir get

func BinFile

func BinFile() string

BinFile get

func ChangeUserByName

func ChangeUserByName(newUname string) (err error)

ChangeUserByName change work user by new username.

func ChangeUserUidGid

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

ChangeUserUidGid change work user by new username uid,gid

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 CurrentUser

func CurrentUser() *user.User

CurrentUser must get current user

func EnvPaths

func EnvPaths() []string

EnvPaths get and split $PATH to []string

func Environ

func Environ() map[string]string

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

func ExecCmd

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

ExecCmd a command and return output.

Usage:

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

func ExecLine

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

ExecLine quick exec an command line string

func Executable

func Executable(binName string) (string, error)

Executable find in the system, alias of FindExecutable()

Usage:

sysutil.Executable("bash")

func ExpandPath

func ExpandPath(path string) string

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

func FindExecutable

func FindExecutable(binName string) (string, error)

FindExecutable in the system

Usage:

sysutil.FindExecutable("bash")

func FlushExec

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

FlushExec instance

func GoVersion

func GoVersion() string

GoVersion get go runtime version. eg: "1.18.2"

func HasExecutable

func HasExecutable(binName string) bool

HasExecutable in the system

Usage:

HasExecutable("bash")

func HasShellEnv

func HasShellEnv(shell string) bool

HasShellEnv has shell env check.

Usage:

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

func HomeDir

func HomeDir() string

HomeDir get user home dir path.

func Hostname

func Hostname() string

Hostname is alias of os.Hostname, but ignore error

func IsConsole

func IsConsole(out io.Writer) bool

IsConsole check out is in stderr/stdout/stdin

Usage:

sysutil.IsConsole(os.Stdout)

func IsDarwin

func IsDarwin() bool

IsDarwin system

func IsLinux

func IsLinux() bool

IsLinux system

func IsMSys

func IsMSys() bool

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

func IsMac

func IsMac() bool

IsMac system

func IsShellSpecialVar

func IsShellSpecialVar(c uint8) bool

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

func IsTerminal

func IsTerminal(fd uintptr) bool

IsTerminal isatty check

Usage:

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

func IsWin

func IsWin() bool

IsWin system. linux windows darwin

func IsWindows

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

func LoginUser() *user.User

LoginUser must get current user

func MustFindUser

func MustFindUser(uname string) *user.User

MustFindUser must find an system user by name

func NewCmd

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

NewCmd instance

func OpenBrowser

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

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

QuickExec quick exec an simple command line

func SearchPath

func SearchPath(keywords string) []string

SearchPath search executable files in the system $PATH

Usage:

sysutil.SearchPath("go")

func ShellExec

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

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

func StdIsTerminal

func StdIsTerminal() bool

StdIsTerminal os.Stdout is terminal

func UHomeDir

func UHomeDir() string

UHomeDir get user home dir path.

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 is alias of os.UserHomeDir, but ignore error

func Workdir

func Workdir() string

Workdir get

Types

type CallerInfo

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

CallerInfo struct

func CallersInfos

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

func (ci *CallerInfo) String() string

String convert

type GoInfo

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

func OsGoInfo() (*GoInfo, error)

OsGoInfo fetch and parse

func ParseGoVersion

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()
 	pretty.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