asyncexec

package
v0.0.0-...-4c4ccab Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StreamCommand

func StreamCommand(workDir string, command string, args []string) error

StreamCommand executes a given command in a context directory and streams the outputs to their according stdeout/stderr.

Types

type Cmd

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

Cmd is a wrapper arround exec.Cmd. Mainly used to execute command asynchronously with/or without output stream.

func New

func New(cmd *exec.Cmd, buff int) *Cmd

New instantiate a new Cmd object.

func (*Cmd) ExitCode

func (c *Cmd) ExitCode() int

ExitCode returns the command process exit code.

func (*Cmd) Exited

func (c *Cmd) Exited() bool

Exited returns true if the command exited, false otherwise.

func (*Cmd) Run

func (c *Cmd) Run() error

Run runs the command. if streamOutput is true, it will spin two goroutine responsible of streaming the stdout and stderr

Example (WithNoStream)
package main

import (
	"fmt"
	"os/exec"

	"github.com/aws-controllers-k8s/dev-tools/pkg/asyncexec"
)

func main() {
	cmd := asyncexec.New(exec.Command("echo", "Hello ACK"), 16)
	cmd.Run()
	cmd.Wait()
	fmt.Println(cmd.ExitCode())
}
Output:

0
Example (WithStream)
package main

import (
	"fmt"
	"os/exec"

	"github.com/aws-controllers-k8s/dev-tools/pkg/asyncexec"
)

func main() {
	cmd := asyncexec.New(exec.Command("echo", "Hello ACK"), 16)
	cmd.Run()

	done := make(chan struct{})
	go func() {
		for b := range cmd.StdoutStream() {
			fmt.Println(string(b))

		}
		done <- struct{}{}
	}()
	go func() {
		for b := range cmd.StderrStream() {
			fmt.Println(string(b))

		}
		done <- struct{}{}
	}()

	defer func() { _, _ = <-done, <-done }()

	cmd.Wait()
}
Output:

Hello ACK

func (*Cmd) StderrStream

func (c *Cmd) StderrStream() <-chan []byte

StderrStream returns a channel streaming the command Stderr.

func (*Cmd) StdoutStream

func (c *Cmd) StdoutStream() <-chan []byte

StdoutStream returns a channel streaming the command Stdout.

func (*Cmd) Stop

func (c *Cmd) Stop()

Stop signals the Wrapper to kill the process running the command.

func (*Cmd) Wait

func (c *Cmd) Wait() error

Wait blocks until the command exits

Jump to

Keyboard shortcuts

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