Documentation
¶
Index ¶
- Variables
- func WithDebug(debugOut ...io.Writer) configApply
- func WithEnv(env []string) configApply
- func WithStdErr(writer io.Writer) configApply
- func WithStdIn(reader io.Reader) configApply
- func WithStdOut(writer io.Writer) configApply
- func WithTimeout(timeout time.Duration) configApply
- func WithTimeoutMillis(millis int) configApply
- func WithTimeoutSeconds(seconds int) configApply
- type Cmd
- func (c *Cmd) Run(commandStr string) error
- func (c *Cmd) RunPowershell(commandStr string) error
- func (c *Cmd) RunPowershellWithDir(commandStr string, runDirStr string) error
- func (c *Cmd) RunShell(commandStr string) error
- func (c *Cmd) RunShellWithDir(commandStr string, runDirStr string) error
- func (c *Cmd) RunWithDir(commandStr string, runDirStr string) error
- type DebugLogger
- func (d *DebugLogger) Environment(envCount int)
- func (d *DebugLogger) ExecutionCommand(name string, args []string)
- func (d *DebugLogger) ExecutionCompleted()
- func (d *DebugLogger) ExecutionDirectory(dir string)
- func (d *DebugLogger) ExecutionFailed(err error, isTimeout bool)
- func (d *DebugLogger) ExecutionStart()
- func (d *DebugLogger) ParsedCommand(command string)
- func (d *DebugLogger) StartFailed(err error)
- func (d *DebugLogger) Timeout(timeout time.Duration)
- type Logger
- type NoOpLogger
- func (n *NoOpLogger) Environment(envCount int)
- func (n *NoOpLogger) ExecutionCommand(name string, args []string)
- func (n *NoOpLogger) ExecutionCompleted()
- func (n *NoOpLogger) ExecutionDirectory(dir string)
- func (n *NoOpLogger) ExecutionFailed(err error, isTimeout bool)
- func (n *NoOpLogger) ExecutionStart()
- func (n *NoOpLogger) ParsedCommand(command string)
- func (n *NoOpLogger) StartFailed(err error)
- func (n *NoOpLogger) Timeout(timeout time.Duration)
Constants ¶
This section is empty.
Variables ¶
var EmptyCmdError = errors.New("empty command")
EmptyCmdError is returned by the Run family of methods when the provided command string is empty (or resolves to only a shell wrapper prefix with no payload). Use errors.Is to check for this sentinel.
Functions ¶
func WithDebug ¶ added in v0.4.0
WithDebug enables debug mode for command execution. The optional debugOut writer receives debug messages; if omitted, debug output is written to os.Stderr.
When debug mode is active, the following information is emitted:
- Parsed command string
- Resolved command name and argument list
- Working directory (when set)
- Timeout setting (when set)
- Number of environment variables (when set)
- Execution start, completion, and failure messages
- Measured execution duration
func WithEnv ¶ added in v0.5.0
func WithEnv(env []string) configApply
WithEnv sets the environment variables for the command.
The provided slice fully replaces the process environment for the executed command; it is not merged with os.Environ(). Variables such as PATH and HOME are therefore dropped unless explicitly included, which may cause shells or external binaries to fail to launch. To preserve the current environment, append to os.Environ():
env := append(os.Environ(), "MY_VAR=hello") cmd := easycmd.New(easycmd.WithEnv(env))
func WithStdErr ¶ added in v0.4.0
WithStdErr sets the standard error stream for the command.
func WithStdOut ¶ added in v0.4.0
WithStdOut sets the standard output stream for the command.
func WithTimeout ¶ added in v0.5.0
WithTimeout sets the command execution timeout as a time.Duration.
Because time.Duration's underlying unit is nanoseconds, passing a bare integer literal is almost always a bug: WithTimeout(5) means "5 nanoseconds", which triggers an immediate timeout. Multiply by a unit constant (for example, 5 * time.Second) or prefer the unit-specific helpers WithTimeoutSeconds and WithTimeoutMillis.
func WithTimeoutMillis ¶ added in v0.6.0
func WithTimeoutMillis(millis int) configApply
WithTimeoutMillis sets the command execution timeout in milliseconds. It is a convenience wrapper around WithTimeout that avoids the nanosecond pitfall of passing a bare integer to WithTimeout.
func WithTimeoutSeconds ¶ added in v0.6.0
func WithTimeoutSeconds(seconds int) configApply
WithTimeoutSeconds sets the command execution timeout in seconds. It is a convenience wrapper around WithTimeout that avoids the nanosecond pitfall of passing a bare integer to WithTimeout.
Types ¶
type Cmd ¶
type Cmd struct {
// contains filtered or unexported fields
}
Cmd executes an external command using the configuration captured at construction time via New.
func New ¶
func New(configApplies ...configApply) *Cmd
New creates a new Cmd instance. Configuration is applied through the functional options exposed by the With* functions. Options that are not provided fall back to defaults: os.Stdin, os.Stdout, and os.Stderr for the standard streams, no timeout, no debug output, and the inherited process environment.
func (*Cmd) Run ¶
Run executes commandStr as-is.
Run returns EmptyCmdError when commandStr is empty. When a timeout is configured via WithTimeout, WithTimeoutSeconds, or WithTimeoutMillis, the returned error message uses one of two Korean prefixes to indicate the phase in which the deadline was exceeded:
- "command failed to start": the deadline expired before the process started.
- "command execution timed out": the deadline expired after the process started.
These prefixes are currently the only way to distinguish the two timeout phases; no sentinel error or typed error is exposed for them, so callers should treat the exact message format as subject to change.
func (*Cmd) RunPowershell ¶ added in v0.2.0
RunPowershell wraps commandStr with PowerShell and executes it. Error semantics match Run.
func (*Cmd) RunPowershellWithDir ¶ added in v0.2.0
RunPowershellWithDir executes a PowerShell-wrapped commandStr in the working directory runDirStr. Error semantics match Run.
func (*Cmd) RunShell ¶ added in v0.2.0
RunShell wraps commandStr with bash -c and executes it. Error semantics match Run.
func (*Cmd) RunShellWithDir ¶ added in v0.2.0
RunShellWithDir executes a bash-wrapped commandStr in the working directory runDirStr. Error semantics match Run.
type DebugLogger ¶ added in v0.5.0
type DebugLogger struct {
// contains filtered or unexported fields
}
DebugLogger 실제 디버그 출력을 수행하는 구현체
func NewDebugLogger ¶ added in v0.5.0
func NewDebugLogger(out io.Writer) *DebugLogger
NewDebugLogger DebugLogger 인스턴스를 생성합니다
func (*DebugLogger) Environment ¶ added in v0.5.0
func (d *DebugLogger) Environment(envCount int)
func (*DebugLogger) ExecutionCommand ¶ added in v0.5.0
func (d *DebugLogger) ExecutionCommand(name string, args []string)
func (*DebugLogger) ExecutionCompleted ¶ added in v0.5.0
func (d *DebugLogger) ExecutionCompleted()
func (*DebugLogger) ExecutionDirectory ¶ added in v0.5.0
func (d *DebugLogger) ExecutionDirectory(dir string)
func (*DebugLogger) ExecutionFailed ¶ added in v0.5.0
func (d *DebugLogger) ExecutionFailed(err error, isTimeout bool)
func (*DebugLogger) ExecutionStart ¶ added in v0.5.0
func (d *DebugLogger) ExecutionStart()
func (*DebugLogger) ParsedCommand ¶ added in v0.5.0
func (d *DebugLogger) ParsedCommand(command string)
func (*DebugLogger) StartFailed ¶ added in v0.5.0
func (d *DebugLogger) StartFailed(err error)
func (*DebugLogger) Timeout ¶ added in v0.5.0
func (d *DebugLogger) Timeout(timeout time.Duration)
type Logger ¶ added in v0.5.0
type Logger interface {
ParsedCommand(command string)
ExecutionCommand(name string, args []string)
ExecutionDirectory(dir string)
ExecutionStart()
Timeout(timeout time.Duration)
Environment(envCount int)
StartFailed(err error)
ExecutionFailed(err error, isTimeout bool)
ExecutionCompleted()
}
Logger 디버그 출력을 담당하는 인터페이스
type NoOpLogger ¶ added in v0.5.0
type NoOpLogger struct{}
NoOpLogger 아무것도 하지 않는 로거 구현체 (Null Object Pattern)
func NewNoOpLogger ¶ added in v0.5.0
func NewNoOpLogger() *NoOpLogger
NewNoOpLogger NoOpLogger 인스턴스를 생성합니다
func (*NoOpLogger) Environment ¶ added in v0.5.0
func (n *NoOpLogger) Environment(envCount int)
func (*NoOpLogger) ExecutionCommand ¶ added in v0.5.0
func (n *NoOpLogger) ExecutionCommand(name string, args []string)
func (*NoOpLogger) ExecutionCompleted ¶ added in v0.5.0
func (n *NoOpLogger) ExecutionCompleted()
func (*NoOpLogger) ExecutionDirectory ¶ added in v0.5.0
func (n *NoOpLogger) ExecutionDirectory(dir string)
func (*NoOpLogger) ExecutionFailed ¶ added in v0.5.0
func (n *NoOpLogger) ExecutionFailed(err error, isTimeout bool)
func (*NoOpLogger) ExecutionStart ¶ added in v0.5.0
func (n *NoOpLogger) ExecutionStart()
func (*NoOpLogger) ParsedCommand ¶ added in v0.5.0
func (n *NoOpLogger) ParsedCommand(command string)
func (*NoOpLogger) StartFailed ¶ added in v0.5.0
func (n *NoOpLogger) StartFailed(err error)
func (*NoOpLogger) Timeout ¶ added in v0.5.0
func (n *NoOpLogger) Timeout(timeout time.Duration)