...

Package xmain

import "oss.terrastruct.com/util-go/xmain"
Overview
Index

Overview ▾

flag_helpers.go are private functions from pflag/flag.go

Package xmain provides a standard stub for the main of a command handling logging, flags, signals and shutdown.

Index ▾

func Main(run RunFunc)
type ExitError
    func ExitErrorf(code int, msg string, v ...interface{}) ExitError
    func (ee ExitError) Error() string
type Opts
    func NewOpts(env *xos.Env, args []string) *Opts
    func (o *Opts) Bool(envKey, flag, shortFlag string, defaultVal bool, usage string) (*bool, error)
    func (o *Opts) Defaults() string
    func (o *Opts) Float64(envKey, flag, shortFlag string, defaultVal float64, usage string) (*float64, error)
    func (o *Opts) Int64(envKey, flag, shortFlag string, defaultVal int64, usage string) (*int64, error)
    func (o *Opts) Int64Slice(envKey, flag, shortFlag string, defaultVal []int64, usage string) (*[]int64, error)
    func (o *Opts) String(envKey, flag, shortFlag string, defaultVal, usage string) *string
type RunFunc
type State
    func (ms *State) AbsPath(fp string) string
    func (ms *State) HumanPath(fp string) string
    func (ms *State) Main(ctx context.Context, sigs <-chan os.Signal, run func(context.Context, *State) error) error
    func (ms *State) ReadPath(fp string) ([]byte, error)
    func (ms *State) WritePath(fp string, p []byte) error
type TestState
    func (ts *TestState) Cleanup(tb testing.TB)
    func (ts *TestState) Signal(ctx context.Context, sig os.Signal) (err error)
    func (ts *TestState) Start(tb testing.TB, ctx context.Context)
    func (ts *TestState) StderrPipe() (pr io.Reader)
    func (ts *TestState) StdinPipe() (pw io.WriteCloser)
    func (ts *TestState) StdoutPipe() (pr io.Reader)
    func (ts *TestState) Wait(ctx context.Context) (err error)
type UsageError
    func UsageErrorf(msg string, v ...interface{}) UsageError
    func (ue UsageError) Error() string

Package files

flag_helpers.go opts.go stdlib_exec.go xmain.go xmaintest.go

func Main

func Main(run RunFunc)

type ExitError

type ExitError struct {
    Code    int    `json:"code"`
    Message string `json:"message"`
}

func ExitErrorf

func ExitErrorf(code int, msg string, v ...interface{}) ExitError

func (ExitError) Error

func (ee ExitError) Error() string

type Opts

type Opts struct {
    Args  []string
    Flags *pflag.FlagSet
    // contains filtered or unexported fields
}

func NewOpts

func NewOpts(env *xos.Env, args []string) *Opts

func (*Opts) Bool

func (o *Opts) Bool(envKey, flag, shortFlag string, defaultVal bool, usage string) (*bool, error)

func (*Opts) Defaults

func (o *Opts) Defaults() string

Mostly copy pasted pasted from pflag.FlagUsagesWrapped with modifications for env var

func (*Opts) Float64

func (o *Opts) Float64(envKey, flag, shortFlag string, defaultVal float64, usage string) (*float64, error)

func (*Opts) Int64

func (o *Opts) Int64(envKey, flag, shortFlag string, defaultVal int64, usage string) (*int64, error)

func (*Opts) Int64Slice

func (o *Opts) Int64Slice(envKey, flag, shortFlag string, defaultVal []int64, usage string) (*[]int64, error)

func (*Opts) String

func (o *Opts) String(envKey, flag, shortFlag string, defaultVal, usage string) *string

type RunFunc

type RunFunc func(context.Context, *State) error

type State

type State struct {
    Name string

    Stdin  io.Reader
    Stdout io.WriteCloser
    Stderr io.WriteCloser

    Log  *cmdlog.Logger
    Env  *xos.Env
    Opts *Opts

    PWD string
}

func (*State) AbsPath

func (ms *State) AbsPath(fp string) string

AbsPath joins the PWD with fp to give the absolute path to fp.

func (*State) HumanPath

func (ms *State) HumanPath(fp string) string

HumanPath makes absolute path fp more suitable for human consumption by replacing $HOME in fp with ~ and making it relative to the current PWD.

func (*State) Main

func (ms *State) Main(ctx context.Context, sigs <-chan os.Signal, run func(context.Context, *State) error) error

func (*State) ReadPath

func (ms *State) ReadPath(fp string) ([]byte, error)

func (*State) WritePath

func (ms *State) WritePath(fp string, p []byte) error

type TestState

type TestState struct {
    Run  func(context.Context, *State) error
    Env  *xos.Env
    Args []string
    PWD  string

    Stdin  io.Reader
    Stdout io.Writer
    Stderr io.Writer
    // contains filtered or unexported fields
}

func (*TestState) Cleanup

func (ts *TestState) Cleanup(tb testing.TB)

func (*TestState) Signal

func (ts *TestState) Signal(ctx context.Context, sig os.Signal) (err error)

func (*TestState) Start

func (ts *TestState) Start(tb testing.TB, ctx context.Context)

func (*TestState) StderrPipe

func (ts *TestState) StderrPipe() (pr io.Reader)

func (*TestState) StdinPipe

func (ts *TestState) StdinPipe() (pw io.WriteCloser)

func (*TestState) StdoutPipe

func (ts *TestState) StdoutPipe() (pr io.Reader)

func (*TestState) Wait

func (ts *TestState) Wait(ctx context.Context) (err error)

type UsageError

type UsageError struct {
    Message string `json:"message"`
}

func UsageErrorf

func UsageErrorf(msg string, v ...interface{}) UsageError

func (UsageError) Error

func (ue UsageError) Error() string