mainer

package
v0.0.0-...-e1e9d1d Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package mainer defines types relevant to command entrypoint implementation. A typical main entrypoint looks like this, assuming cmd is a struct which defines the command's flags and implements mainer.Mainer:

func main() {
  var c cmd
  os.Exit(int(c.Main(os.Args, mainer.CurrentStdio())))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CancelOnSignal

func CancelOnSignal(ctx context.Context, signals ...os.Signal) context.Context

CancelOnSignal returns a context that is canceled when the process receives one of the specified signals.

Types

type ExitCode

type ExitCode int

ExitCode is the type of a process exit code.

const (
	Success ExitCode = iota
	Failure
	InvalidArgs
)

List of pre-defined exit codes.

type Mainer

type Mainer interface {
	Main([]string, Stdio) ExitCode
}

Mainer defines the method to implement for a type that implements a Main entrypoint of a command.

type MockMainer

type MockMainer struct {
	MainFunc func([]string, Stdio) ExitCode
}

MockMainer is a test mock for the Mainer interface.

func (*MockMainer) Main

func (m *MockMainer) Main(args []string, stdio Stdio) ExitCode

type Stdio

type Stdio struct {
	// Cwd is the current working directory.
	Cwd string

	// Stdin is the standard input reader.
	Stdin io.Reader

	// Stdout is the standard output writer.
	Stdout io.Writer

	// Stderr is the standard error writer.
	Stderr io.Writer
}

Stdio defines the OS abstraction for standard I/O.

func CurrentStdio

func CurrentStdio() Stdio

CurrentStdio returns the Stdio for the current process. Its Cwd field reflects the working directory at the time of the call.

Jump to

Keyboard shortcuts

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