gobash

package
v1.5.8 Latest Latest
Warning

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

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

README

gobash

Execute commands, scripts, executables in the go environment with live log output.


Example of use

Run

Run executes commands and can actively end them, returning logs and error messages in real time, recommended.


    arg := "for i in $(seq 1 5); do echo 'test cmd' $i;sleep 1; done"
    ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) // timeout control

    result := Run(ctx, "bash", "-c", arg)
    // real-time output of logs and error messages
    for v := range result.StdOut {
        fmt.Printf(v)
    }
    if result.Err != nil {
        fmt.Println("execute command failed,", result.Err.Error())
    }

Exec

Exec is suitable for executing a single non-blocking command, outputting standard and error logs, but the log output is not real-time, note: if the execution of the command is permanently blocked, it will cause a concurrent leak

    arg := "for i in $(seq 1 5); do echo 'test cmd' $i;sleep 1; done"
    out, err := Exec("bash", "-c", arg)
    if err != nil {
        return
    }
    fmt.Println(string(out))

Documentation

Overview

Package gobash provides the ability to execute commands, scripts, executables in the go environment with live log output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(name string, args ...string) ([]byte, error)

Exec suitable for executing a single non-blocking command, outputting standard and error logs, but the log output is not real time, no execution, command name must be in system path, Note: If the execution of a command blocks permanently, it can cause a concurrent leak.

Types

type Result

type Result struct {
	StdOut chan string
	Err    error // If nil after the command is executed, the command is executed successfully
}

Result of the execution of the command

func Run

func Run(ctx context.Context, name string, args ...string) *Result

Run execute the command, no execution, command name must be in system path, you can actively end the command, the execution results are returned in real time in Result.StdOut

Jump to

Keyboard shortcuts

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