exec

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 14 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

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

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

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

CallSlice executes the command line via system (OS).

func CallSliceQuiet

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

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

func LeftPad(s string, pad int) string

LeftPad inserts spaces at beginning of each line

func LookPath

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

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

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

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

func StripHtmlTags(s string) string

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

func StripLeftTabs

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

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

func TrimQuotes(s string) string

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

Types

type ISSOpt

type ISSOpt func(c *issCtx)

func WithScriptExpander

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

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

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

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

type Opt func(*calling)

func WithCommand

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

func WithCommandArgs

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

func WithCommandString

func WithCommandString(cmd string) Opt

func WithContext

func WithContext(ctx context.Context) Opt

func WithEnv

func WithEnv(key, value string) Opt

func WithExtraFiles

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

func WithOnError

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

func WithOnOK

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

func WithPadding

func WithPadding(leftPadding int) Opt

func WithQuietOnError

func WithQuietOnError(quiet bool) Opt

WithQuietOnError do NOT print error internally

func WithStderrCaught

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

func WithStdoutCaught

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

func WithVerboseCommandLine

func WithVerboseCommandLine(verbose bool) Opt

func WithWorkDir

func WithWorkDir(dir string) Opt

Jump to

Keyboard shortcuts

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