runcmd

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: Apache-2.0 Imports: 11 Imported by: 4

README

Runcmd

CI Linux CI Windows GoDoc Go Report Card

Should be a Go library to execute external commands.

Import the library:

import (
    "github.com/enr/runcmd"
)

You can use this library in two ways:

  • Run
  • Start

Run starts the specified command, waits for it to complete and returns a result complete of stdout and stderr:

executable := "/usr/bin/ls"
args := []string{"-al"}
command := &runcmd.Command{
    Exe:  executable,
    Args: args,
}
res := command.Run()
if res.Success() {
    fmt.Printf("standard output: %s", res.Stdout().String())
} else {
    fmt.Printf("error executing %s. Exit code %d", command, res.ExitStatus())
    fmt.Printf("error output: %s", res.Stderr().String())
    fmt.Printf("the error: %v", res.Error())
}

Start starts the specified command but does not wait for it to complete.

executable := "/usr/local/bin/start-server"
command := &runcmd.Command{
    Exe:  executable,
}
logFile := command.GetLogfile()
// maybe you want to follow logs...
t, _ := tail.TailFile(logFile, tail.Config{Follow: true})
command.Start()
runningProcess := command.Process

License

Apache 2.0 - see LICENSE file.

Copyright 2020 runcmd contributors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Name string
	// the executable
	Exe string
	// args passed to executable
	Args []string
	// the command is run in a shell, that is prepend `/bin/sh -c` or `cmd /C` to the command line
	CommandLine string
	WorkingDir  string
	// only for *nix: if not set, runcmd uses env[$SHELL] or defaults to /bin/sh
	ForceShell string
	// custom environment variables. these are overwritten from .env file if UseEnv is true
	Env Env
	// only for *nix: if true .profile file in the working dir is sourced
	UseProfile bool // dovrebbe essere Profile string : path
	// only for *nix: if true .env file in the working dir is used to initialize env vars
	UseEnv bool // dovrebbe essere EnvFile string: path
	// used only if command is started as process
	Logfile string
	// the underlying process
	Process *os.Process
	// contains filtered or unexported fields
}

Command is the launched command, it wraps the process.

func (*Command) FullCommand

func (c *Command) FullCommand() string

FullCommand returns the full command line string.

func (*Command) GetLogfile

func (c *Command) GetLogfile() string

GetLogfile get the full path to the file containing the process output.

func (*Command) GetName

func (c *Command) GetName() string

GetName get the name of the command.

func (*Command) Run

func (c *Command) Run() *ExecResult

Run starts the specified command and waits for it to complete.

func (*Command) Start

func (c *Command) Start() error

Start start a process without waiting

func (*Command) String

func (c *Command) String() string

type Env

type Env map[string]string

Env is the map of environment variables for the command.

type ExecResult

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

ExecResult is the result of the completed command execution.

func (*ExecResult) Error

func (r *ExecResult) Error() error

func (*ExecResult) ExitStatus

func (r *ExecResult) ExitStatus() int

ExitStatus is the command exit.

func (*ExecResult) Stderr

func (r *ExecResult) Stderr() *bytes.Buffer

Stderr returns the underlying buffer with the contents of the error stream.

func (*ExecResult) Stdout

func (r *ExecResult) Stdout() *bytes.Buffer

Stdout returns the underlying buffer with the contents of the output stream.

func (*ExecResult) String

func (r *ExecResult) String() string

func (*ExecResult) Success

func (r *ExecResult) Success() bool

Success true if command completed with no error.

Jump to

Keyboard shortcuts

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