tool

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ExitCodeUnknownError   = 1
	ExitCodeInvalidArgs    = 2
	ExitCodeInvalidFormats = 3
)

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, args []string) error

Do tool with specific arguments.

func DoWithOS

func DoWithOS()

DoWithOS is wrapper for Do with NewContext(context.Background()) and os.Args. It also handles returned error and exit code.

func ExitCode

func ExitCode(from error) int

ExitCode returns exit code from passed error. If passed error or its wrapped error doesn't implement ExitCode, it returns ExitCodeUnknownError. See also ExitError.

func ExtractFormat

func ExtractFormat(arg string) (path, format string)

ExtractFormat returns path from passed arg and derived format. Default format is derived from file extension. It can be overridden if arg contains # with following format.

func Help

func Help() string

Help returns text with manual for user.

func RegisterWorker

func RegisterWorker(inputFormat, outputFormat string, worker Worker) error

Types

type Context

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

Context represents cancelable context which also reacts to os.Interrupt signal.

func NewContext

func NewContext(parent context.Context) *Context

NewContext returns new Context based on parent.

func (*Context) Cancel

func (c *Context) Cancel()

Cancel context and stops listening. It can be called repeatedly and safely from simultaneous goroutines.

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (c *Context) Done() <-chan struct{}

func (*Context) Err

func (c *Context) Err() error

func (*Context) Value

func (c *Context) Value(key any) any

type ExitCodeBearer

type ExitCodeBearer interface {
	ExitCode() int
}

ExitCodeBearer represents value with specified exit code. See also ExitCode or ExitError.

type ExitError

type ExitError struct {
	Code int
	Err  error
}

ExitError represents error with specified exit code.

func NewExitError

func NewExitError(code int, err error) *ExitError

NewExitError creates new ExitError.

func (*ExitError) ErrString

func (e *ExitError) ErrString() string

ErrString returns string representation of Err. If e or Err is nil, it returns value "unknown error".

func (*ExitError) Error

func (e *ExitError) Error() string

Error returns string representation of error.

func (*ExitError) ExitCode

func (e *ExitError) ExitCode() int

ExitCode returns specified exit code.

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

Unwrap returns wrapped error if any.

type Work

type Work interface {
	Context() context.Context
	Formats() (input, output string)
	Input() []byte
	Output(data []byte) error
}

Work represents one specific run with arguments.

func NewWorkFromOS

func NewWorkFromOS(ctx context.Context, input, output string) (w Work, err error)

NewWorkFromOS returns new Work based on passed parameters. Values input and output can be path to file or - (representing stdin and stdout). Format is derived from file extension or overridden if # is present. See also ExtractFormat.

type Worker

type Worker interface {
	DoWork(Work) error
}

Worker allows to do Work.

func RegisteredWorker

func RegisteredWorker(inputFormat, outputFormat string) Worker

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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