cmdio

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FlagArmor = &cli.BoolFlag{
	Name:        "armor",
	Aliases:     []string{"a"},
	Usage:       "use ascii-armored output",
	Destination: &armor,
	DefaultText: "enabled by default for terminal output",
}

FlagArmor is an armor flag.

View Source
var FlagCompression = &cli.StringFlag{
	Name:        "compression",
	Usage:       "compression `ALGORITHM:LVL`",
	DefaultText: "no compression",
	Action: func(_ *cli.Context, v string) error {
		i := strings.Index(v, ":")
		if i == 0 {
			return cli.Exit(fmt.Errorf("invalid compression: %s", v), 1)
		}
		var alg string
		var lvl int
		if i == -1 {
			alg = v
		} else {
			alg = v[:i]
			lvl, _ = strconv.Atoi(v[i+1:])
		}
		c, ok := str2compression[alg]
		if ok {
			compressor = c(lvl)
			return nil
		}
		list := make([]string, 0, len(str2compression))
		for k := range str2compression {
			list = append(list, k)
		}
		return cli.Exit(fmt.Errorf("available compression algorithms: %s", strings.Join(list, ", ")), 1)
	},
}

FlagCompression is a compression flag.

View Source
var FlagInput = &cli.StringFlag{
	Name:      "input",
	Aliases:   []string{"i"},
	Usage:     "input `FILE` (override stdin)",
	TakesFile: true,
	Action: func(_ *cli.Context, path string) error {
		i, err := CustomInput(path)
		if err != nil {
			return err
		}
		os.Stdin = i.Raw()
		return nil
	},
}

FlagInput is an input flag.

View Source
var FlagOutput = &cli.PathFlag{
	Name:      "output",
	Aliases:   []string{"o"},
	Usage:     "output `FILE` (override stdout)",
	TakesFile: true,
	Action: func(_ *cli.Context, path string) error {
		o, err := CustomOutput(path)
		if err != nil {
			return err
		}
		os.Stdout = o.Raw()
		return nil
	},
}

FlagOutput is an output flag.

Functions

func CustomIO

func CustomIO(inputArg, outputArg string, outputName func(string) string) (input Input, output Output, err error)

CustomIO accept input and output arguments and returns custom input and output.

func IOFlags

func IOFlags() []cli.Flag

IOFlags returns both input and output flags.

func InputFlags

func InputFlags() []cli.Flag

InputFlags returns the input flags.

func OutputFlags

func OutputFlags() []cli.Flag

OutputFlags returns the output flags.

func PassphraseFunc

func PassphraseFunc(prompt string) func() (string, error)

PassphraseFunc returns a function that prompts the user for a passphrase.

func Read

func Read() (pack.Tag, pack.Packable, error)

Read is an alias of GetInput().Read.

func ReadExact

func ReadExact[T pack.Packable](in Input) (v T, err error)

ReadExact reads the value with specified type from the provided input.

func RequestPassphrase

func RequestPassphrase(prompt string) (string, error)

RequestPassphrase prompts the user for a passphrase.

func Status

func Status(v ...any)

Status prints a status message.

func Statusf

func Statusf(format string, v ...any)

Statusf prints a status message with format.

func WithPassphrase

func WithPassphrase(prompt string) error

WithPassphrase requests the passphrase and creates packing option.

func Write

func Write(v pack.Packable) error

Write is an alias of GetInput().Write.

func YesNo

func YesNo(prompt string) (bool, error)

YesNo prompts the user for yes or no input. Actually just checks the input for "y" or "yes".

Types

type ErrNonTerminal added in v0.1.3

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

ErrNonTerminal is returned when the input is not a terminal.

func (ErrNonTerminal) Error added in v0.1.3

func (e ErrNonTerminal) Error() string

type Input

type Input interface {
	Raw() *os.File
	Close() error

	// Read reads the value from input.
	Read() (pack.Tag, pack.Packable, error)
	// contains filtered or unexported methods
}

Input represents a cmd input.

func CustomInput

func CustomInput(path string) (Input, error)

CustomInput opens the specified file and returns it as Input. If the path is empty, it returns the standard input.

func GetInput

func GetInput() Input

GetInput returns the standard input.

type Output

type Output interface {
	Raw() *os.File
	Close() error

	// Write writes the specified value to the output.
	Write(pack.Packable) error
}

Output represents a cmd output.

func CustomOutput

func CustomOutput(path string) (Output, error)

CustomOutput opens the specified file and returns it as Output. If the path is empty, it returns the standard output.

func GetOutput

func GetOutput() Output

GetOutput returns the standard output.

Jump to

Keyboard shortcuts

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