Documentation ¶
Index ¶
- func Call(cmd string, fn func(retCode int, stdoutText string)) (err error)
- func CallQuiet(cmd string, fn func(retCode int, stdoutText string)) (err error)
- func CallSlice(cmd []string, fn func(retCode int, stdoutText string)) (err error)
- func CallSliceQuiet(cmd []string, fn func(retCode int, stdoutText string)) (err error)
- func InvokeShellScripts(scripts string, opts ...ISSOpt) (err error)
- func IsEAccess(err error) bool
- func IsExitError(err error) (int, bool)
- func LeftPad(s string, pad int) string
- func LookPath(file string) (string, error)
- func New(opts ...Opt) *calling
- func Run(command string, arguments ...string) (err error)
- func RunCommand(command string, readStdout bool, arguments ...string) (retCode int, stdoutText string, err error)
- func RunCommandFull(command string, readStdout bool, arguments ...string) (retCode int, stdoutText, stderrText string, err error)
- func RunWithOutput(command string, arguments ...string) (retCode int, stdoutText string, err error)
- func SplitCommandString(s string, quoteChars ...rune) []string
- func StripHtmlTags(s string) string
- func StripLeftTabs(s string) string
- func StripQuotes(s string) string
- func Sudo(command string, arguments ...string) (retCode int, stdoutText string, err error)
- func TrimQuotes(s string) string
- type ISSOpt
- type Opt
- func WithCommand(cmd ...interface{}) Opt
- func WithCommandArgs(cmd string, args ...string) Opt
- func WithCommandString(cmd string) Opt
- func WithContext(ctx context.Context) Opt
- func WithEnv(key, value string) Opt
- func WithExtraFiles(files ...*os.File) Opt
- func WithOnError(onError func(err error, retCode int, stdoutText, stderrText string)) Opt
- func WithOnOK(onOK func(retCode int, stdoutText string)) Opt
- func WithPadding(leftPadding int) Opt
- func WithQuietOnError(quiet bool) Opt
- func WithStderrCaught(writer ...io.Writer) Opt
- func WithStdoutCaught(writer ...io.Writer) Opt
- func WithVerboseCommandLine(verbose bool) Opt
- func WithWorkDir(dir string) Opt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Call ¶ added in v1.5.3
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
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 CallSliceQuiet ¶ added in v1.5.3
CallSliceQuiet executes the command line via system (OS) without error printing.
func InvokeShellScripts ¶ added in v1.5.15
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 LookPath ¶ added in v1.5.3
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 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 ¶
RunWithOutput runs an OS command and collect the result outputting
func SplitCommandString ¶ added in v1.5.3
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
StripHtmlTags aggressively strips HTML tags from a string. It will only keep anything between `>` and `<`.
func StripLeftTabs ¶ added in v1.5.11
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
StripQuotes strips first and last quote char (double quote or single quote).
func TrimQuotes ¶ added in v1.5.3
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
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
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
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
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 WithCommandString ¶ added in v1.5.3
func WithContext ¶ added in v1.5.3
func WithExtraFiles ¶ added in v1.5.3
func WithOnError ¶ added in v1.5.3
func WithPadding ¶ added in v1.5.3
func WithQuietOnError ¶ added in v1.5.9
WithQuietOnError do NOT print error internally