Documentation
¶
Overview ¶
Procs is a library to make working with command line applications a little nicer.
The goal is to expand on the os/exec package by providing some features usually accomplished in a shell, without having to resort to a shell. Procs also tries to make working with output simpler by providing a simple line handler API over working with io pipes.
Finally, while the hope is that procs provides some convenience, it is also a goal to help make it easier to write more secure code. For example, avoiding a shell and the ability to manage the environment as a map[string]string are both measures that intend to make it easier to accomplish things like avoiding outputting secrets and opening the door for MITM attacks. With that said, it is always important to consider the security implications, especially when you are working with untrusted input or sensitive data.
Example ¶
package main
import (
"fmt"
"github.com/ionrock/procs"
)
func main() {
b := procs.Builder{
Context: map[string]string{
"NAME": "eric",
},
Templates: []string{
"echo $NAME |",
"grep $NAME",
},
}
cmd := b.Command()
fmt.Println(cmd)
p := procs.NewProcess(cmd)
p.Run()
out, _ := p.Output()
fmt.Println(string(out))
}
Output: echo eric | grep eric eric
Example (PredefinedCmds) ¶
package main
import (
"fmt"
"os/exec"
"github.com/ionrock/procs"
)
func main() {
p := procs.Process{
Cmds: []*exec.Cmd{
exec.Command("echo", "foo"),
exec.Command("grep", "foo"),
},
}
p.Run()
out, _ := p.Output()
fmt.Println(string(out))
}
Output: foo
Index ¶
- func Env(env map[string]string, useEnv bool) []string
- func ParseEnv(environ []string) map[string]string
- func SplitCommand(cmd string) []string
- func SplitCommandEnv(cmd string, getenv func(key string) string) []string
- type Builder
- type Manager
- func (m *Manager) Remove(name string) error
- func (m *Manager) Start(name, cmd string) error
- func (m *Manager) StartProcess(name string, p *Process) error
- func (m *Manager) StderrHandler(name string) OutHandler
- func (m *Manager) StdoutHandler(name string) OutHandler
- func (m *Manager) Stop(name string) error
- func (m *Manager) Wait() error
- type OutHandler
- type Process
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Env ¶
Env takes a map[string]string and converts it to a []string that can be used with exec.Cmd. The useEnv boolean flag will include the current process environment, overlaying the provided env map[string]string.
func ParseEnv ¶
ParseEnv takes an environment []string and converts it to a map[string]string.
func SplitCommand ¶
SplitCommand parses a command and splits it into lexical arguments like a shell, returning a []string that can be used as arguments to exec.Command.
Example ¶
package main
import (
"fmt"
"github.com/ionrock/procs"
)
func main() {
parts := procs.SplitCommand("echo 'hello world'")
for i, p := range parts {
fmt.Printf("%d %s\n", i+1, p)
}
}
Output: 1 echo 2 hello world
func SplitCommandEnv ¶
SplitCommandEnv parses a command and splits it into lexical arguments like a shell, returning a []string that can be used as arguments to exec.Command. It also allows providing an expansion function that will be used when expanding values within the parsed arguments.
Example ¶
package main
import (
"fmt"
"github.com/ionrock/procs"
)
func main() {
env := map[string]string{
"GREETING": "hello",
"NAME": "world!",
"PASSWORD": "secret",
}
getenv := func(key string) string {
if v, ok := env[key]; ok && key != "PASSWORD" {
return v
}
return ""
}
parts := procs.SplitCommandEnv("echo '$GREETING $NAME $PASSWORD'", getenv)
for i, p := range parts {
fmt.Printf("%d %s\n", i+1, p)
}
}
Output: 1 echo 2 hello world!
Types ¶
type Builder ¶
Builder helps construct commands using templates.
func (*Builder) Command ¶
Command returns the result of the templates as a single string.
type Manager ¶
Manager manages a set of Processes.
func (*Manager) Remove ¶
Remove will try to stop and remove a managed process.
func (*Manager) Start ¶
Start and managed a new process using the default handlers from a string.
func (*Manager) StartProcess ¶
StartProcess starts and manages a predifined process.
func (*Manager) StderrHandler ¶
func (m *Manager) StderrHandler(name string) OutHandler
StderrHandler returns an OutHandler that will ensure the underlying process has an empty stderr buffer and logs to stdout a prefixed value of "$name | $line".
func (*Manager) StdoutHandler ¶
func (m *Manager) StdoutHandler(name string) OutHandler
StdoutHandler returns an OutHandler that will ensure the underlying process has an empty stdout buffer and logs to stdout a prefixed value of "$name | $line".
func (*Manager) Stop ¶
Stop will try to stop a managed process. If the process does not exist, no error is returned.
type OutHandler ¶
OutHandler defines the interface for writing output handlers for Process objects.
type Process ¶
type Process struct {
// CmdString takes a string and parses it into the relevant cmds
CmdString string
// Cmds is the list of command delmited by pipes.
Cmds []*exec.Cmd
// Env provides a map[string]string that can mutated before
// running a command.
Env map[string]string
// Dir defines the directory the command should run in. The
// Default is the current dir.
Dir string
// OutputHandler can be defined to perform any sort of processing
// on the output. The simple interface is to accept a string (a
// line of output) and return a string that will be included in the
// buffered output and/or output written to stdout.'
//
// For example defining the Process as:
//
// prefix := "myapp"
// p := &procs.Process{
// OutputHandler: func(line string) string {
// return fmt.Sprintf("%s | %s", prefix, line)
// },
// }
//
// This would prefix the stdout lines with a "myapp | ".
//
// By the default, this function is nil and will be skipped, with
// the unchanged line being added to the respective output buffer.
OutputHandler OutHandler
// ErrHandler is a OutputHandler for stderr.
ErrHandler OutHandler
// contains filtered or unexported fields
}
Process is intended to be used like exec.Cmd where possible.
func NewProcess ¶
NewProcess creates a new *Process from a command string.
It is assumed that the user will mutate the resulting *Process by setting the necessary attributes.
func (*Process) ErrOutput ¶
ErrOutput returns the buffered stderr as []byte
func (*Process) Output ¶
Output returns the buffered output as []byte.
func (*Process) Run ¶
Run executes the cmds and returns the output as a string and any error.
Source Files
¶
- builder.go
- env.go
- manager.go
- parse.go
- process.go