Documentation
¶
Overview ¶
Package exec provides some convenient execution functions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultShellConfig = ShellConfig{Shell: "bash", Timeout: time.Minute}
DefaultShellConfig is the default ShellConfig.
Functions ¶
func ExecuteShellByHTTP ¶ added in v0.30.0
ExecuteShellByHTTP executes the shell command or script by HTTP.
Notice: it uses the default interface implementation of ExecuteShell.
func ExecuteShellHandler ¶ added in v0.30.0
func ExecuteShellHandler(handler ShellResultHandler, config ...ShellConfig) http.Handler
ExecuteShellHandler returns a http handler to execute a SHELL command or script.
The request body is the command to be executed as JSON like this:
{ "cmd": "BASE64_COMMAND", // Optional "script": "BASE64_SCRIPT_FILE_CONTENT", // Optional "shell": "SHELL_COMMAND", // Optional "timeout": "10s" // Optional }
handle is used to handle the result of the command or script. If nil, it will use the default that returns a JSON as the response body like this,
{ "stdout": "BASE64_STD_OUTPUT", "stderr": "BASE64_STD_ERR_OUTPUT", "error": "BASE64 failure reason. If successfully, it is empty." }
Notice:
- The executed command or script must be encoded by base64.
- If shell is given, it will override the Shell in ShellConfig.
- If timeout is given, it will override the Timeout in ShellConfig.
The returned http handler is very dangerous, and should not be called by the non-trusted callers.
func LogExecutedCmdResultHook ¶
func LogExecutedCmdResultHook(r exec.Result)
LogExecutedCmdResultHook is a hook to log the executed command.
Types ¶
type ShellConfig ¶ added in v0.30.0
type ShellConfig struct { Timeout time.Duration // The timeout to execute the shell command. Shell string // The shell name or path, which is "bash" by default. Dir string // The directory to save and run the shell script. }
ShellConfig is used to configure the shell execution.
type ShellResultHandler ¶ added in v0.30.0
type ShellResultHandler func(w http.ResponseWriter, stdout, stderr string, err error)
ShellResultHandler is used to handle the result of the shell command or script.