cmdutil

package
v0.0.0-...-2bc12df Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package cmdutil provides utilities for formatting CLI output.

Index

Constants

View Source
const (
	// JSON represents the json marshaller
	JSON = "json"
	// YAML represents the YAML marshaller
	YAML = "yaml"
	// TEXT represents an easily greppable text format
	TEXT = "text"
)

Variables

View Source
var ColoredStderr io.Writer = color.Error

ColoredStderr represents a color supporting writer for Stderr

View Source
var NewClient = func() client.Client {
	return client.ForUNIXSocket(config.Socket)
}

NewClient returns a new Wash client for the given subcommand. Tests can set NewClient to a stub that returns a mock client.

View Source
var Stderr io.Writer = os.Stderr

Stderr represents Stderr

View Source
var Stdout io.Writer = os.Stdout

Stdout represents Stdout

Functions

func ErrPrintf

func ErrPrintf(msg string, a ...interface{})

ErrPrintf formats and prints the provided format string and args on stderr and colors the output red.

func FormatDuration

func FormatDuration(dur time.Duration) string

FormatDuration formats a duration as `[[dd-]hh:]mm:ss` according to http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html.

func LongestFieldFromColumn

func LongestFieldFromColumn(rows [][]string, colIdx int) string

LongestFieldFromColumn returns the longest string for a particular column index from the provided table.

func Print

func Print(a ...interface{})

Print is a wrapper to fmt.Print that prints to cmdutil.Stdout

func Printf

func Printf(msg string, a ...interface{})

Printf is a wrapper to fmt.Printf that prints to cmdutil.Stdout

func Println

func Println(a ...interface{})

Println is a wrapper to fmt.Println that prints to cmdutil.Stdout

func Prompt

func Prompt(msg string, parser InputParser) (interface{}, error)

Prompt prints the supplied message, waits for input on stdin, then passes the input over to the supplied parser. The actual prompt displayed to the user is "{msg} ".

func SafeErrPrintf

func SafeErrPrintf(msg string, a ...interface{})

SafeErrPrintf is a thread-safe wrapper to ErrPrintf

func SafePrint

func SafePrint(a ...interface{})

SafePrint is a thread-safe wrapper to Print

func SafePrintf

func SafePrintf(msg string, a ...interface{})

SafePrintf is a thread-safe wrapper to Printf

func SafePrintln

func SafePrintln(a ...interface{})

SafePrintln is a thread-safe wrapper to Println

Types

type ColumnHeader

type ColumnHeader struct {
	ShortName, FullName string
}

ColumnHeader describes a short and long name for a column.

type InputParser

type InputParser = func(string) (interface{}, error)

InputParser represents a parser that parses input passed into Prompt.

var YesOrNoP InputParser = func(input string) (confirmed interface{}, err error) {
	confirmed = len(input) > 0 && (input[0] == 'y' || input[0] == 'Y')
	return
}

YesOrNoP parses input representing confirmation. confirmed (bool) is true if the input starts with "y" or "Y".

type Marshaller

type Marshaller func(interface{}) ([]byte, error)

Marshaller is a type that marshals a given value

func NewMarshaller

func NewMarshaller(format string) (Marshaller, error)

NewMarshaller returns a marshaller that marshals values into the specified format. Currently only JSON or YAML are supported.

All non-JSON marshallers have a default implementation of

"Marshal to JSON" =>
"Unmarshal the JSON" =>
"Marshal to <format>"

(the first two steps are effectively "Marshal to a a JSON map/array/value Go type" so that people don't have to implement multiple Marshaler interfaces).

For types that have their own MarshalJSON implementation, this could be a problem because the "Unmarshal the JSON" step unmarshals the data into a different type (so that the custom MarshalJSON implementation is not called). Thus, these types may need to implement the format-specific Marshaler interfaces:

  • For YAML, this is cmdutil.YamlMarshaler
  • For TEXT, this is cmdutil.TextMarshaler

func (Marshaller) Marshal

func (m Marshaller) Marshal(v interface{}) (string, error)

Marshal marshals the given value

type Pool

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

Pool is a worker pool to limit work-in-progress and wait for a dynamic batch of work to complete. It uses WorkerPool to limit how many tasks run at once, and a wait group to know when all queued work is complete. Because the queue is dynamic, we need a wait group to tell us when all potential work is complete before we tell the pool to stop accepting new work and shutdown.

func NewPool

func NewPool(parallel int) Pool

NewPool creates a new dynamic worker pool.

func (Pool) Done

func (p Pool) Done()

Done is used to mark that a task has completed.

func (Pool) Finish

func (p Pool) Finish()

Finish waits until no more tasks are running, then shuts down the worker pool. This allows new tasks to be added to the pool after it's called by using a WaitGroup to determine when all tasks are done.

func (Pool) Submit

func (p Pool) Submit(f func())

Submit adds a new task to the pool.

type Table

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

Table represents a formatted table. Use the NewTable* functions to create your Table objects.

func NewTable

func NewTable(rows ...[]string) *Table

NewTable creates a new Table object with the given rows

func NewTableWithHeaders

func NewTableWithHeaders(headers []ColumnHeader, rows [][]string) *Table

NewTableWithHeaders creates a new Table object with the given headers and rows

func (*Table) Append

func (t *Table) Append(rows [][]string)

Append appends the given rows to t

func (*Table) Format

func (t *Table) Format() string

Format formats the provided table to display with sufficient padding to align columns

func (*Table) Rows

func (t *Table) Rows() [][]string

Rows returns the table's rows

type TextMarshaler

type TextMarshaler interface {
	MarshalTEXT() ([]byte, error)
}

TextMarshaler is a type that can be marshaled into Text output.

For structured data, text output prefixes each line with the entire key path (using .KEY for map indexing and .N for array indexing) to make it more greppable. For example, given something like

AppArmorProfile:
  Args:
    - redis-server
  Config:
    AttachStdout: false
    Cmd:
      - redis-server

Its Text output would be:

AppArmorProfile:
Args.0: redis-server
Config.AttachStdout: false
Config.Cmd.0: redis-server

type YamlMarshaler

type YamlMarshaler interface {
	MarshalYAML() ([]byte, error)
}

YamlMarshaler is a type that can be marshalled into YAML

Jump to

Keyboard shortcuts

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