exec

package
v0.0.0-...-3c33c60 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const (
	StderrRedirectNone = iota
	StderrRedirectStdout
	StderrRedirectNull
)

Stderr redirection

Variables

This section is empty.

Functions

This section is empty.

Types

type PipedExec

type PipedExec struct {
	// contains filtered or unexported fields
}

PipedExec allows to execute commands in pipe

func (*PipedExec) Command

func (Self *PipedExec) Command(name string, args ...string) *PipedExec

Command adds a command to a pipe

func (*PipedExec) GetCmd

func (Self *PipedExec) GetCmd(idx int) *exec.Cmd

GetCmd returns cmd with given index

func (*PipedExec) Run

func (Self *PipedExec) Run(out io.Writer, err io.Writer) error

Run starts the pipe

Example

Run a command and use os.Stdout, os.Stderr

package main

import (
	"fmt"
	"os"

	"github.com/untillpro/goutils/exec"
)

func main() {

	err := new(exec.PipedExec).
		Command("echo", "Run").
		Run(os.Stdout, os.Stderr)

	if err != nil {
		fmt.Println("exec.PipedExec failed:", err)
	}

}
Output:

Run

func (*PipedExec) RunToStrings

func (Self *PipedExec) RunToStrings() (stdout string, stderr string, err error)

RunToStrings runs the pipe and saves outputs to strings

Example

Run a command and capture its output to strings

package main

import (
	"fmt"
	"strings"

	"github.com/untillpro/goutils/exec"
)

func main() {

	stdout, stderr, err := new(exec.PipedExec).
		Command("echo", "RunToStrings").
		RunToStrings()

	if err != nil {
		fmt.Println("exec.PipedExec failed:", err, stderr)
	}

	fmt.Println(strings.TrimSpace(stdout))
}
Output:

RunToStrings
Example (Pipe)

printf "1\n2\n3" | grep 2

package main

import (
	"fmt"
	"strings"

	"github.com/untillpro/goutils/exec"
)

func main() {

	stdout, stderr, err := new(exec.PipedExec).
		Command("printf", `1\n2\n3`).
		Command("grep", "2").
		RunToStrings()

	if err != nil {
		fmt.Println("exec.PipedExec failed:", err, stderr)
	}

	fmt.Println(strings.TrimSpace(stdout))
}
Output:

2

func (*PipedExec) Start

func (Self *PipedExec) Start(out io.Writer, err io.Writer) error

Start all cmds

func (*PipedExec) Wait

func (Self *PipedExec) Wait() error

Wait until all cmds finish

func (*PipedExec) WorkingDir

func (Self *PipedExec) WorkingDir(wd string) *PipedExec

WorkingDir sets working directory for the last command

Jump to

Keyboard shortcuts

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