cmdpkg

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

cmd

执行外部命令,支持 context.Context 和工作目录。

基础用法

out, err := cmdpkg.RunCommandContext(ctx, "go", []string{"version"})
out, err = cmdpkg.RunCommandWithWorkDirContext(ctx, ".", "go", []string{"test", "./cmd"})

注意事项

  • 命令和参数要分开传递,不要拼接未校验的用户输入。
  • 长耗时命令必须使用带超时的 context。

验证

go test ./cmd

Documentation

Index

Constants

View Source
const (
	// LinuxShellBin 执行脚本
	LinuxShellBin   string = "/bin/sh -c" // mac & linux
	WindowsShellBin string = "cmd.exe /C" // windows
)

Variables

This section is empty.

Functions

func ExecShell

func ExecShell() []string

ExecShell 执行二进制

func RunCommand deprecated

func RunCommand(command string, args []string) (output []byte, err error)

Deprecated: 使用 RunCommandContext 替代

func RunCommandContext

func RunCommandContext(ctx context.Context, command string, args []string) ([]byte, error)

RunCommandContext 运行命令(支持 Context)

func RunCommandWithWorkDir deprecated

func RunCommandWithWorkDir(workDir, command string, args []string) (output []byte, err error)

Deprecated: 使用 RunCommandWithWorkDirContext 替代

func RunCommandWithWorkDirContext

func RunCommandWithWorkDirContext(ctx context.Context, workDir, command string, args []string) ([]byte, error)

RunCommandWithWorkDirContext 运行命令(支持 Context 和工作目录)

Types

This section is empty.

Jump to

Keyboard shortcuts

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