command

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: MIT Imports: 10 Imported by: 5

Documentation

Overview

Package command provides helpers to execute process and parse the output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(ctx context.Context, logger *log.Entry, exe string, params ...string) (output []byte, exitCode int, err error)

Execute executes a 'command' in a new process Parameter command must contain a path to the command, or simply the command name if lookup in path is wanted. A nil value can be passed in parameters ctx and logger. Returns the outputs of the process written to the standard output and the status code returned by the command. Where there is an error, Note that, contrary to the standard library, the function doesn't return an error if the command execution returned a value different from 0. The new process where the command is executed inherits all the env vars of the current process.

func ExecuteAndParse

func ExecuteAndParse(ctx context.Context, logger *log.Entry, parser OutputParser, result interface{}, exe string, params ...string) (int, error)

ExecuteAndParse executes a command, using the func Execute and parsing the output using the provided parser function. After execution: returned error is nil and the param result contains the output parsed as json. (x)or error is not nil and the result doesn't contain the result parsed as json. If an error is raised when trying to parse the output, the function returns an error of type ParseError that contains the the raw output of the process and the error returned by the json parser.

func ExecuteAndParseJSON

func ExecuteAndParseJSON(ctx context.Context, logger *log.Entry, result interface{}, exe string, params ...string) (int, error)

ExecuteAndParseJSON executes a command, using the func Execute. After execution: returned error is nil and the param result contains the output parsed as json. (x)or error is not nil and the result doesn't contain the process output parsed as json. If an error is raised when trying to parse the process output, the function returns an error of type ParseError that contains the raw output of the process and the error returned by the json parser.

func ExecuteAndParseXML

func ExecuteAndParseXML(ctx context.Context, logger *log.Entry, result interface{}, exe string, params ...string) (int, error)

ExecuteAndParseXML executes a command, using the func Execute. After execution: returned error is nil and the param result contains the output parsed as XML. (x)or error is not nil and the result doesn't contain the process output parsed as XML. If an error is raised when trying to parse the process output, the function returns an error of type ParseError that contains the the raw output of the process and the error returned by the json parser.

func ExecuteWithStdErr

func ExecuteWithStdErr(ctx context.Context, logger *log.Entry, exe string, params ...string) ([]byte, []byte, int, error)

ExecuteWithStdErr executes a 'command' in a new process Parameter command must contain a path to the command, or simply the command name if lookup in path is wanted. A nil value can be passed in parameters ctx and logger. Returns the outputs of the process written to the standard output and error, also returns the status code returned by the command. Note that, contrary to the standard library, the function doesn't return an error if the command execution returned a value different from 0. The new process where the command is executed inherits all the env vars of the current process.

Types

type OutputParser

type OutputParser func(output []byte, result interface{}) error

OutputParser represent a function that parses an output from process

type ParseError

type ParseError struct {
	// ProcessOutput output of the process that couldn't be parsed.
	ProcessOutput []byte

	// ProcessErrOutput output written by the process to the standard error.
	ProcessErrOutput []byte

	// ProcessStatus contains the process status returned by the execution of the process.
	ProcessStatus int

	// ParserError contains the error returned by the parser when trying to parse the result.
	ParserError string
}

ParseError reports a failure when trying to parse a process output.

func (*ParseError) Error

func (e *ParseError) Error() string

Jump to

Keyboard shortcuts

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