run

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2024 License: Apache-2.0 Imports: 9 Imported by: 1

README

godoc codecov Go Report Card

run - Go-Curses safe exec.Command wrappers

run is a package for running commands and updating Go-Curses based user interfaces with the STDOUT and STDERR of the running process.

Installation

> go get github.com/go-corelibs/run@latest

Examples

Callback

func main() {
    pid, done, err := run.Callback(Options{
    }, func(line string) {
        // update user interface with normal output
    }, func(line string) {
        // update user interface with error output
    })
    if err != nil {
        panic(err)
    }
    // pid != 0
    <-done // if waiting for command to finish is necessary
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Callback

func Callback(options Options, stdout, stderr func(line string)) (pid int, done chan bool, err error)

Callback runs the command defined by the Options and pipes the standard and error output streams, line by line, to their respective stdout and stderr functions.

Callback returns the `pid`, a `done` channel and any err that happened during the starting of the command. The `done` channel will receive a `true` upon the successful exiting of the command and will receive a `false` if the command exited with an error (after the `stderr` func is called with the `err.Error()` message).

Callback uses cdk.Go to run goroutines and as such is safe for use within a Go-Curses environment that needs to be able to update the UI thread with display updates

func Run

func Run(path, name string, argv ...string) (stdout, stderr string, status int, err error)

Run is a wrapper around With configured with the given Options and the default os.Environ

func With

func With(options Options) (stdout, stderr string, status int, err error)

With is a blocking function which runs a command with the Options given and if there is an error, looks for the last non-empty line of output to STDERR (and if that's empty, checks STDOUT) and returns that as the function `err` return value. If both STDERR and STDOUT are empty, the error message is: "exit status %d" where the `%d` is replaced with the status code.

Types

type Options

type Options struct {
	// Path is the working directory path
	Path string
	// Name is the command to run
	Name string
	// Argv are the arguments passed to Name
	Argv []string
	// Environ are the environment variables provided to Name
	Environ []string
}

Options defines the command to be run

Jump to

Keyboard shortcuts

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