process

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

process

ci

process provides a simple interface for executing a new process.

Installation

Add the following import to your code.

import "github.com/kmlib/process"

Usage

func ExampleProcess() {
        proc, err := process.New(context.Background(), "sh", "-c", "echo stdout1; echo stdout2; echo 1>&2 stderr1; echo 1>&2 stderr2")
        if err != nil {
                panic(err)
        }

        if err := proc.Start(); err != nil {
                panic(err)
        }

        for line := range proc.Stderr() {
                fmt.Println(line)
        }
        for line := range proc.Stdout() {
                fmt.Println(line)
        }

        if err := proc.Wait(); err != nil {
                panic(err)
        }

        // Output:
        // stderr1
        // stderr2
        // stdout1
        // stdout2
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Process

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

Process wraps exec.Cmd providing channels for stdout and stderr.

Example
package main

import (
	"context"
	"fmt"

	"github.com/kmlib/process"
)

func main() {
	proc, err := process.New(context.Background(), "sh", "-c", "echo stdout1; echo stdout2; echo 1>&2 stderr1; echo 1>&2 stderr2")
	if err != nil {
		panic(err)
	}

	if err := proc.Start(); err != nil {
		panic(err)
	}

	for line := range proc.Stderr() {
		fmt.Println(line)
	}
	for line := range proc.Stdout() {
		fmt.Println(line)
	}

	if err := proc.Wait(); err != nil {
		panic(err)
	}

}
Output:

stderr1
stderr2
stdout1
stdout2

func New

func New(ctx context.Context, name string, arg ...string) (*Process, error)

New returns the Process struct to execute a program specified by the arguments.

func (*Process) Start

func (p *Process) Start() error

Start starts the process, and it does not wait for the completion of the process.

func (*Process) Stderr

func (p *Process) Stderr() <-chan string

Stderr returns a channel for stderr strings.

func (*Process) Stdout

func (p *Process) Stdout() <-chan string

Stdout returns a channel for stdout strings.

func (*Process) Wait

func (p *Process) Wait() error

Wait waits for the process to complete.

Jump to

Keyboard shortcuts

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