exec

package
v1.202405300917.1 Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MIT, MIT Imports: 7 Imported by: 2

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 (pe *PipedExec) Command(name string, args ...string) *PipedExec

Command adds a command to a pipe

func (*PipedExec) GetCmd

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

GetCmd returns cmd with given index

func (*PipedExec) Run

func (pe *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/voedger/voedger/pkg/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 (pe *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/voedger/voedger/pkg/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/voedger/voedger/pkg/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 (pe *PipedExec) Start(out io.Writer, err io.Writer) error

Start all cmds

func (*PipedExec) Wait

func (pe *PipedExec) Wait() error

Wait until all cmds finish

func (*PipedExec) WorkingDir

func (pe *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