exec

package
v1.6.25 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2023 License: MIT Imports: 14 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 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 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 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