exec

package
v1.5.39 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2022 License: MIT Imports: 19 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsPath added in v0.3.9

func AbsPath(pathname string) string

AbsPath returns a clean, normalized and absolute path string for the given pathname. Deprecated see also dir.AbsPath

func Call added in v1.5.3

func Call(cmd string, fn func(retCode int, stdoutText string)) (err error)

Call executes the command line via system (OS).

DO NOT QUOTE: in 'cmd', A command line shouldn't has quoted parts. These are bad:

cmd := "ls '/usr/bin'"
cmd := `tar "c:/My Documents/"`

Uses CallSlice if your args includes space (like 'c:/My Documents/')

func CallQuiet added in v1.5.3

func CallQuiet(cmd string, fn func(retCode int, stdoutText string)) (err error)

CallQuiet executes the command line via system (OS) without error printing.

DO NOT QUOTE: in 'cmd', A command line shouldn't has quoted parts. These are bad:

cmd := "ls '/usr/bin'"
cmd := `tar "c:/My Documents/"`

Uses CallSliceQuiet if your args includes space (like 'c:/My Documents/')

func CallSlice added in v1.5.3

func CallSlice(cmd []string, fn func(retCode int, stdoutText string)) (err error)

CallSlice executes the command line via system (OS).

func CallSliceQuiet added in v1.5.3

func CallSliceQuiet(cmd []string, fn func(retCode int, stdoutText string)) (err error)

CallSliceQuiet executes the command line via system (OS) without error printing.

func CopyFile added in v0.3.12

func CopyFile(src, dst string) (err error)

CopyFile will make a content clone of src. Deprecated see also dir.CopyFile

func CopyFileByLinkFirst added in v0.3.12

func CopyFileByLinkFirst(src, dst string) (err error)

CopyFileByLinkFirst copies a file from src to dst. If src and dst files exist, and are the same, then return success. Otherwise, attempt to create a hard link between the two files. If that fail, copy the file contents from src to dst. Deprecated see also dir.CopyFileByLinkFirst

func DeleteFile added in v0.3.12

func DeleteFile(dst string) (err error)

DeleteFile deletes a file if exists Deprecated see also dir.DeleteFile

func EnsureDir

func EnsureDir(dir string) (err error)

EnsureDir checks and creates the directory. Deprecated see also dir.EnsureDir

func EnsureDirEnh

func EnsureDirEnh(dir string) (err error)

EnsureDirEnh checks and creates the directory, via sudo if necessary. Deprecated see also dir.EnsureDirEnh

func FileExists

func FileExists(filepath string) bool

FileExists returns the existence of an directory or file Deprecated see also dir.FileExist

func FileModeIs added in v0.2.3

func FileModeIs(filepath string, tester func(mode os.FileMode) bool) (ret bool)

FileModeIs tests the mode of 'filepath' with 'tester'. Examples:

var yes = exec.FileModeIs("/etc/passwd", exec.IsModeExecAny)
var yes = exec.FileModeIs("/etc/passwd", exec.IsModeDirectory)

Deprecated see also dir.FileModeIs

func ForDir added in v0.1.18

func ForDir(root string, cb func(depth int, cwd string, fi os.FileInfo) (stop bool, err error)) (err error)

ForDir walks on `root` directory and its children Deprecated see also dir.ForDir

func ForDirMax added in v0.1.18

func ForDirMax(root string, initialDepth, maxDepth int, cb func(depth int, cwd string, fi os.FileInfo) (stop bool, err error)) (err error)

ForDirMax walks on `root` directory and its children with nested levels up to `maxLength`.

Example - discover folder just one level

     _ = ForDirMax(dir, 0, 1, func(depth int, cwd string, fi os.FileInfo) (stop bool, err error) {
			if fi.IsDir() {
				return
			}
         // ... doing something for a file,
			return
		})

maxDepth = -1: no limit. initialDepth: 0 if no idea. Deprecated see also dir.ForDirMax

func ForFile added in v0.3.12

func ForFile(root string, cb func(depth int, cwd string, fi os.FileInfo) (stop bool, err error)) (err error)

ForFile walks on `root` directory and its children Deprecated see also dir.ForFile

func ForFileMax added in v0.3.12

func ForFileMax(root string, initialDepth, maxDepth int, cb func(depth int, cwd string, fi os.FileInfo) (stop bool, err error)) (err error)

ForFileMax walks on `root` directory and its children with nested levels up to `maxLength`.

Example - discover folder just one level

     _ = ForFileMax(dir, 0, 1, func(depth int, cwd string, fi os.FileInfo) (stop bool, err error) {
			if fi.IsDir() {
				return
			}
         // ... doing something for a file,
			return
		})

maxDepth = -1: no limit. initialDepth: 0 if no idea.

Deprecated see also dir.ForFileMax

func GetCurrentDir

func GetCurrentDir() string

GetCurrentDir returns the current workingFlag directory it should be equal with os.Getenv("PWD") Deprecated see also dir.GetCurrentDir

func GetExecutableDir

func GetExecutableDir() string

GetExecutableDir returns the executable file directory Deprecated see also dir.GetExecutableDir

func GetExecutablePath added in v0.1.16

func GetExecutablePath() string

GetExecutablePath returns the executable file path Deprecated see also dir.GetExecutablePath

func InvokeShellScripts added in v1.5.15

func InvokeShellScripts(scripts string, opts ...ISSOpt) (err error)

InvokeShellScripts invokes a shell script fragments with internal invoker (typically it's hedzr/log.exec.Run).

InvokeShellScripts finds and prepares the proper shell (bash, or powershell, etc.), and call it by invoker.

The invoker can be customized by yours, use WithScriptInvoker.

func IsDirectory

func IsDirectory(filepath string) (bool, error)

IsDirectory tests whether `path` is a directory or not Deprecated see also dir.IsDirectory

func IsEAccess

func IsEAccess(err error) bool

IsEAccess detects whether err is a EACCESS errno or not

func IsExitError

func IsExitError(err error) (int, bool)

IsExitError checks the error object

func IsModeCharDevice added in v0.2.3

func IsModeCharDevice(mode os.FileMode) bool

IsModeCharDevice give the result of whether a file is a character device Deprecated see also dir.IsModeCharDevice

func IsModeDevice added in v0.2.3

func IsModeDevice(mode os.FileMode) bool

IsModeDevice give the result of whether a file is a device Deprecated see also dir.IsModeDevice

func IsModeDirectory added in v0.2.3

func IsModeDirectory(mode os.FileMode) bool

IsModeDirectory give the result of whether a file is a directory Deprecated see also dir.IsModeDirectory

func IsModeExecAll added in v0.2.3

func IsModeExecAll(mode os.FileMode) bool

IsModeExecAll give the result of whether a file can be invoked by all users Deprecated see also dir.IsModeExecAll

func IsModeExecAny added in v0.2.3

func IsModeExecAny(mode os.FileMode) bool

IsModeExecAny give the result of whether a file can be invoked by anyone Deprecated see also dir.IsModeExecAny

func IsModeExecGroup added in v0.2.3

func IsModeExecGroup(mode os.FileMode) bool

IsModeExecGroup give the result of whether a file can be invoked by its unix-group Deprecated see also dir.IsModeExecGroup

func IsModeExecOther added in v0.2.3

func IsModeExecOther(mode os.FileMode) bool

IsModeExecOther give the result of whether a file can be invoked by its unix-all Deprecated see also dir.IsModeExecOther

func IsModeExecOwner added in v0.2.3

func IsModeExecOwner(mode os.FileMode) bool

IsModeExecOwner give the result of whether a file can be invoked by its unix-owner Deprecated see also dir.IsModeExecOwner

func IsModeIrregular added in v0.2.3

func IsModeIrregular(mode os.FileMode) bool

IsModeIrregular give the result of whether a file is a non-regular file; nothing else is known about this file Deprecated see also dir.IsModeIrregular

func IsModeNamedPipe added in v0.2.3

func IsModeNamedPipe(mode os.FileMode) bool

IsModeNamedPipe give the result of whether a file is a named pipe Deprecated see also dir.IsModePipe

func IsModeReadAll added in v0.2.3

func IsModeReadAll(mode os.FileMode) bool

IsModeReadAll give the result of whether a file can be read by all users Deprecated see also dir.IsModeReadAll

func IsModeReadAny added in v0.2.3

func IsModeReadAny(mode os.FileMode) bool

IsModeReadAny give the result of whether a file can be read by anyone Deprecated see also dir.IsModeReadAny

func IsModeReadGroup added in v0.2.3

func IsModeReadGroup(mode os.FileMode) bool

IsModeReadGroup give the result of whether a file can be read by its unix-group Deprecated see also dir.IsModeReadGroup

func IsModeReadOther added in v0.2.3

func IsModeReadOther(mode os.FileMode) bool

IsModeReadOther give the result of whether a file can be read by its unix-all Deprecated see also dir.IsModeReadOther

func IsModeReadOwner added in v0.2.3

func IsModeReadOwner(mode os.FileMode) bool

IsModeReadOwner give the result of whether a file can be read by its unix-owner Deprecated see also dir.IsModeReadOwner

func IsModeRegular added in v0.2.3

func IsModeRegular(mode os.FileMode) bool

IsModeRegular give the result of whether a file is a regular file Deprecated see also dir.IsModeRegular

func IsModeSetgid added in v0.2.3

func IsModeSetgid(mode os.FileMode) bool

IsModeSetgid give the result of whether a file has the setgid bit Deprecated see also dir.IsModeSetgid

func IsModeSetuid added in v0.2.3

func IsModeSetuid(mode os.FileMode) bool

IsModeSetuid give the result of whether a file has the setuid bit Deprecated see also dir.IsModeSetuid

func IsModeSocket added in v0.2.3

func IsModeSocket(mode os.FileMode) bool

IsModeSocket give the result of whether a file is a socket file Deprecated see also dir.IsModeSocket

func IsModeSticky added in v0.2.3

func IsModeSticky(mode os.FileMode) bool

IsModeSticky give the result of whether a file is a sticky file Deprecated see also dir.IsModeSticky

func IsModeSymbolicLink(mode os.FileMode) bool

IsModeSymbolicLink give the result of whether a file is a symbolic link Deprecated see also dir.IsModeSymbolicLink

func IsModeWriteAll added in v0.2.3

func IsModeWriteAll(mode os.FileMode) bool

IsModeWriteAll give the result of whether a file can be written by all users Deprecated see also dir.IsModeWriteAll

func IsModeWriteAny added in v0.2.3

func IsModeWriteAny(mode os.FileMode) bool

IsModeWriteAny give the result of whether a file can be written by anyone Deprecated see also dir.IsModeWriteAny

func IsModeWriteGroup added in v0.2.3

func IsModeWriteGroup(mode os.FileMode) bool

IsModeWriteGroup give the result of whether a file can be written by its unix-group Deprecated see also dir.IsModeWriteGroup

func IsModeWriteOther added in v0.2.3

func IsModeWriteOther(mode os.FileMode) bool

IsModeWriteOther give the result of whether a file can be written by its unix-all Deprecated see also dir.IsModeWriteOther

func IsModeWriteOwner added in v0.2.3

func IsModeWriteOwner(mode os.FileMode) bool

IsModeWriteOwner give the result of whether a file can be written by its unix-owner Deprecated see also dir.IsModeWriteOwner

func IsRegularFile

func IsRegularFile(filepath string) (bool, error)

IsRegularFile tests whether `path` is a normal regular file or not Deprecated see also dir.IsRegularFile

func LeftPad added in v1.5.3

func LeftPad(s string, pad int) string

LeftPad inserts spaces at beginning of each line

func LookPath added in v1.5.3

func LookPath(file string) (string, error)

LookPath searches for an executable named file in the directories named by the PATH environment variable. If file contains a slash, it is tried directly and the PATH is not consulted. The result may be an absolute path or a path relative to the current directory.

func New added in v1.5.3

func New(opts ...Opt) *calling

New return a calling object to allow you to make the fluent call.

Just like:

exec.New().WithCommand("bash", "-c", "echo hello world!").Run()
err = exec.New().WithCommand("bash", "-c", "echo hello world!").RunAndCheckError()

Processing the invoke result:

exec.New().
    WithCommand("bash", "-c", "echo hello world!").
    WithStdoutCaught().
    WithOnOK(func(retCode int, stdoutText string) { }).
    WithStderrCaught().
    WithOnError(func(err error, retCode int, stdoutText, stderrText string) { }).
    Run()

Use context:

exec.New().
    WithCommandString("bash -c 'echo hello world!'", '\'').
    WithContext(context.TODO()).
    Run()
// or double quote pieces
exec.New().
    WithCommandString("bash -c \"echo hello world!\"").
    Run()

Auto Left-Padding if WithOnError / WithOnOK / WithStderrCaught / WithStdoutCaught specified (It's no effects when you caught stdout/stderr with the handlers above. In this case, do it with LeftPad manually).

args := []string{"-al", "/usr/local/bin"}
err := exec.New().
    WithPadding(8).
    WithCommandArgs("ls", args...).
    RunAndCheckError()

func NormalizeDir

func NormalizeDir(s string) string

NormalizeDir make dir name normalized Deprecated see also dir.NormalizeDir

func NormalizePath added in v0.3.9

func NormalizePath(pathname string) string

NormalizePath cleans up the given pathname Deprecated see also dir.NormalizePath

func RemoveDirRecursive

func RemoveDirRecursive(dir string) (err error)

RemoveDirRecursive removes a directory and any children it contains. Deprecated see also dir.RemoveDirRecursive

func Run

func Run(command string, arguments ...string) (err error)

Run runs an OS command

func RunCommand

func RunCommand(command string, readStdout bool, arguments ...string) (retCode int, stdoutText string, err error)

RunCommand runs an OS command and return outputs

func RunCommandFull added in v1.3.23

func RunCommandFull(command string, readStdout bool, arguments ...string) (retCode int, stdoutText, stderrText string, err error)

RunCommandFull runs an OS command and return the all outputs

func RunWithOutput

func RunWithOutput(command string, arguments ...string) (retCode int, stdoutText string, err error)

RunWithOutput runs an OS command and collect the result outputting

func SplitCommandString added in v1.5.3

func SplitCommandString(s string, quoteChars ...rune) []string

SplitCommandString allows split command-line by quote characters (default is double-quote).

In: `bash -c 'echo hello world!'` Out: []string{ "bash", "-c", "echo hello world!"}

func StripHtmlTags added in v1.5.11

func StripHtmlTags(s string) string

StripHtmlTags aggressively strips HTML tags from a string. It will only keep anything between `>` and `<`.

func StripLeftTabs added in v1.5.11

func StripLeftTabs(s string) string

StripLeftTabs removes the padding tabs at left margin. The least tab chars will be erased at left side of lines, and the tab chars beyond the least at left will be kept.

func StripQuotes added in v1.5.11

func StripQuotes(s string) string

StripQuotes strips first and last quote char (double quote or single quote).

func Sudo

func Sudo(command string, arguments ...string) (retCode int, stdoutText string, err error)

Sudo runs an OS command with sudo prefix

func TrimQuotes added in v1.5.3

func TrimQuotes(s string) string

TrimQuotes strips first and last quote char (double quote or single quote).

Types

type ISSOpt added in v1.5.15

type ISSOpt func(c *issCtx)

func WithScriptExpander added in v1.5.15

func WithScriptExpander(expander func(source string) string) ISSOpt

WithScriptExpander providers a string expander for the given script.

You may specify a special one (such as os.ExpandEnv) rather than internal default (a dummy functor to return the source directly).

func WithScriptInvoker added in v1.5.15

func WithScriptInvoker(invoker func(command string, args ...string) (err error)) ISSOpt

WithScriptInvoker provides a custom runner to run the shell and scripts.

The default is exec.Run in hedzr/log package.

For example:

err = InvokeShellScripts("ls -l /",
	WithScriptShell("/bin/bash"),
	WithScriptIsFile(false),
	WithScriptInvoker(func(command string, args ...string) (err error) {
		err = exec.New().
			WithCommandArgs(command, args...).
			WithOnOK(func(retCode int, stdoutText string) {
				t.Logf("%v", LeftPad(stdoutText, 4))
			}).
			RunAndCheckError()
		return
	}),
)
if err != nil {
	t.Errorf("%v", err)
}

func WithScriptIsFile added in v1.5.15

func WithScriptIsFile(isFile bool) ISSOpt

WithScriptIsFile provides a bool flag for flagging the given scripts is a shell scripts fragments or a file.

func WithScriptShell added in v1.5.15

func WithScriptShell(knownShell string) ISSOpt

WithScriptShell provides a predefined shell executable (short if it's in $PATH, or full-path by you risks).

The knownShell looks like shebang. Such as: '/bin/bash', or '/usr/bin/env bash', ...

type Opt added in v1.5.3

type Opt func(*calling)

func WithCommand added in v1.5.3

func WithCommand(cmd ...interface{}) Opt

func WithCommandArgs added in v1.5.3

func WithCommandArgs(cmd string, args ...string) Opt

func WithCommandString added in v1.5.3

func WithCommandString(cmd string) Opt

func WithContext added in v1.5.3

func WithContext(ctx context.Context) Opt

func WithEnv added in v1.5.3

func WithEnv(key, value string) Opt

func WithExtraFiles added in v1.5.3

func WithExtraFiles(files ...*os.File) Opt

func WithOnError added in v1.5.3

func WithOnError(onError func(err error, retCode int, stdoutText, stderrText string)) Opt

func WithOnOK added in v1.5.3

func WithOnOK(onOK func(retCode int, stdoutText string)) Opt

func WithPadding added in v1.5.3

func WithPadding(leftPadding int) Opt

func WithQuietOnError added in v1.5.9

func WithQuietOnError(quiet bool) Opt

WithQuietOnError do NOT print error internally

func WithStderrCaught added in v1.5.3

func WithStderrCaught(writer ...io.Writer) Opt

func WithStdoutCaught added in v1.5.3

func WithStdoutCaught(writer ...io.Writer) Opt

func WithVerboseCommandLine added in v1.5.3

func WithVerboseCommandLine(verbose bool) Opt

func WithWorkDir added in v1.5.3

func WithWorkDir(dir string) Opt

Jump to

Keyboard shortcuts

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