Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"log"
"catinello.eu/exec"
)
func main() {
o, _, c := exec.Simple("echo '!olleH' | rev")
if c != 0 {
log.Fatal("Unexpected failure.")
}
fmt.Println(o)
}
Output: Hello!
Index ¶
- Variables
- func Fork(cmd string, attr *syscall.ProcAttr) (int, error)
- func Run(cmdline string) ([]string, []string, []int, error)
- func RunWithAttr(cmdline string, attr *syscall.SysProcAttr) ([]string, []string, []int, error)
- func RunWithContext(ctx context.Context, cmdline string) ([]string, []string, []int, error)
- func RunWithTokens(cmdline []string) ([]string, []string, []int, error)
- func RunWithTokensContext(ctx context.Context, cmdline []string) ([]string, []string, []int, error)
- func Simple(cmdline string) (string, string, int)
- func SimpleWithContext(ctx context.Context, cmdline string) (string, string, int)
- type Command
- func (c *Command) Fork(cmd string, attr *syscall.ProcAttr) (int, error)
- func (c *Command) Run(cmdline string) ([]string, []string, []int, error)
- func (c *Command) RunWithAttr(cmdline string, attr *syscall.SysProcAttr) ([]string, []string, []int, error)
- func (c *Command) Simple(cmdline string) (string, string, int)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Fork ¶
Fork takes your cmd and returns the pid of the started process.
Example ¶
package main
import (
"fmt"
"log"
"catinello.eu/exec"
)
func main() {
pid, err := exec.Fork("sleep 1", nil)
if err != nil {
log.Fatal("Unexpected failure.")
}
if pid > 1 {
fmt.Println("success")
} else {
fmt.Println("failure")
}
}
Output: success
func Run ¶
Run takes your cmdline and executes it.
Example ¶
package main
import (
"fmt"
"log"
"catinello.eu/exec"
)
func main() {
stdout, stderr, codes, err := exec.Run(`echo "Hello world!"; python3 -c 'import sys; print("error", file=sys.stderr)' && false || echo "success"`)
if err != nil {
log.Fatal(err)
}
for n, code := range codes {
// skip the third command (false) that returns 1
if code != 0 && n != 2 {
log.Fatal(stderr[n])
}
}
// the python command prints error to stderr
if codes[1] == 0 {
if stderr[1] != "error\n" {
log.Fatal("Stderr output missing.")
}
}
fmt.Println(stdout[0])
}
Output: Hello world!
func RunWithAttr ¶
RunAttr takes your cmdline and *SysProcAttr and executes it.
func RunWithContext ¶
func RunWithTokens ¶
RunWithTokens takes your cmdline tokens and does not try to tokenize before executing it.
Example ¶
package main
import (
"fmt"
"log"
"catinello.eu/exec"
)
func main() {
stdout, stderr, codes, err := exec.RunWithTokens([]string{"echo", "Hello world!", ";", "python3", "-c", `'import sys; print("error", file=sys.stderr)'`, "&&", "false", "||", "echo", "success"})
if err != nil {
log.Fatal(err)
}
for n, code := range codes {
// skip the third command (false) that returns 1
if code != 0 && n != 2 {
log.Fatal(stderr[n])
}
}
// the python command prints error to stderr
if codes[1] == 0 {
if stderr[1] != "error\n" {
log.Fatal("Stderr output missing.")
}
}
fmt.Println(stdout[0])
}
Output: Hello world!
func RunWithTokensContext ¶
func Simple ¶
Simple takes your cmdline and executes it.
Example ¶
package main
import (
"fmt"
"log"
"catinello.eu/exec"
)
func main() {
stdout, stderr, code := exec.Simple(`echo "Hello world!"; python3 -c 'import sys; print("error", file=sys.stderr)' && false || echo "success"`)
if code == -1 {
log.Fatal(stderr)
}
if code > 0 {
log.Fatal("Unexpected failure.")
}
if code == 0 && stderr != "error\n" {
log.Fatal("Stderr output missing.")
}
fmt.Println(stdout)
}
Output: Hello world! success
Types ¶
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
func (*Command) RunWithAttr ¶
func (c *Command) RunWithAttr(cmdline string, attr *syscall.SysProcAttr) ([]string, []string, []int, error)
RunAttr takes your cmdline and *SysProcAttr and executes it.
func (*Command) Simple ¶
Simple takes your cmdline and executes it.
Example ¶
package main
import (
"context"
"fmt"
"log"
"catinello.eu/exec"
)
func main() {
r := exec.New(context.Background(), []string{"hello=world!"})
o, e, c := r.Simple("echo $hello")
if len(e) > 0 || c != 0 {
log.Fatal(e)
}
fmt.Println(o)
}
Output: world!
Click to show internal directories.
Click to hide internal directories.