cli

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: Apache-2.0 Imports: 10 Imported by: 68

README

StreamingFast CLI Library

reference License

Quick and opinionated library aiming to create CLI application rapidly. The library contains CLI primitives (around Cobra/Viper) as well as low-level primitives to ease the creation of developer scripts.

Note This library is experimental and the API could change without notice.

Example

The folder ./example contains example usage of the library. You can run them easily them, open a terminal and navigate in the example folder, then go run ...

  • Flat - go run ./flat
  • Nested - go run ./nested
Public Helpers
Method Description
cli.NoError(err, "file not found %q", fileName) Exit the process with exit code 1 and prints fmt.Printf("file not found %q: %w\n", fileName, err) if err != nil
cli.Ensure(x == 0, "x point should be 0, got %d", x) Exit the process with exit code 1 and prints fmt.Printf("x point should be 0, got %d\n", x) if condition received is false
cli.Quit("current date %d is too far away", time.Now()) Exit the process with exit code 1 and prints fmt.Printf("x point should be 0, got %d\n", x)
cli.FileExists("./some/file.png") Returns true if the file received in argument exists, false otherwise
cli.CopyFile("current date %d is too far away", time.Now()) Exit the process with exit code 1 and prints fmt.Printf("x point should be 0, got %d\n", x)
- -
Sample Boilerplate (copy/paste ready)
package main

import (
	"fmt"
	"os"

	. "github.com/streamingfast/cli"
	"github.com/streamingfast/logging"
	"github.com/spf13/cobra"
	"go.uber.org/zap"
)

var zlog = zap.NewNop()
var tracer = logging.ApplicationLogger("nested", "github.com/acme/nested", &zlog)

func main() {
	Run("runner", "Some random command runner with 2 sub-commands",
		Command(generateE,
			"generate",
			"Quick command summary, without a description",
		),
		Command(compareE,
			"compare <input_file>",
			"Quick command summary, with a description, the actual usage above is descriptive, you must handle the arguments manually",
			Description(`
				Description of the command, automatically de-indented by using first line identation,
				use 'go run ./example/nested compare --help' to see it in action!
			`),
			ExamplePrefixed("runner", `
				compare relative_file.json
				compare /absolute/file.json
			`),
		),
	)
}

func generateE(cmd *cobra.Command, args []string) error {
	_, err := os.Getwd()
	NoError(err, "unable to get working directory")

	fmt.Println("Generating something")
	return nil
}

func compareE(cmd *cobra.Command, args []string) error {
	shouldContinue, wasAnswered := AskConfirmation(`Do you want to continue?`)
	if wasAnswered && shouldContinue {

	} else {
		fmt.Println("Not showing diff between files, run the following command to see it manually:")
	}

	return nil
}

Contributing

Issues and PR in this repo related strictly to the cli library.

Report any protocol-specific issues in their respective repositories

Please first refer to the general StreamingFast contribution guide, if you wish to contribute to this codebase.

This codebase uses unit tests extensively, please write and run tests.

License

Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AskConfirmation

func AskConfirmation(label string, args ...interface{}) (answeredYes bool, wasAnswered bool)

func CopyFile

func CopyFile(inPath, outPath string)

func Dedent

func Dedent(input string) string

func Description

func Description(value string) description

func Ensure

func Ensure(condition bool, message string, args ...interface{})

func Example

func Example(value string) example

func ExamplePrefixed

func ExamplePrefixed(prefix string, examples string) example

func Execute

func Execute(f func(cmd *cobra.Command, args []string) error) execute

func FileExists

func FileExists(path string) bool

func NoError

func NoError(err error, message string, args ...interface{})

func Quit

func Quit(message string, args ...interface{})

func Root

func Root(usage, short string, opts ...CommandOption) *cobra.Command

func Run

func Run(usage, short string, opts ...CommandOption)

Types

type Args

type Args cobra.PositionalArgs

func ExactArgs

func ExactArgs(n int) Args

ExactArgs returns an error if there are not exactly n args.

func ExactValidArgs

func ExactValidArgs(n int) Args

ExactValidArgs returns an error if there are not exactly N positional args OR there are any positional args that are not in the `ValidArgs` field of `Command`

func MaximumNArgs

func MaximumNArgs(n int) Args

MaximumNArgs returns an error if there are more than N args.

func MinimumNArgs

func MinimumNArgs(n int) Args

MinimumNArgs returns an error if there is not at least N args.

func RangeArgs

func RangeArgs(min int, max int) Args

RangeArgs returns an error if the number of args is not within the expected range.

type BeforeAllHook

type BeforeAllHook func(cmd *cobra.Command)

type CommandOption

type CommandOption interface {
	// contains filtered or unexported methods
}

func Command

func Command(execute func(cmd *cobra.Command, args []string) error, usage, short string, opts ...CommandOption) CommandOption

func Group

func Group(usage, short string, opts ...CommandOption) CommandOption

type CommandOptionFunc

type CommandOptionFunc func(cmd *cobra.Command)

type Flags

type Flags func(flags *pflag.FlagSet)

type PersistentFlags

type PersistentFlags func(flags *pflag.FlagSet)

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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