cli

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 25, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

dfuse CLI Library

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/dfuse-io/cli"
	"github.com/dfuse-io/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!
			`),
			Example("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
}

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 added in v0.0.2

func Example(value string) example

func ExamplePrefixed added in v0.0.2

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 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

type CommandOptionFunc

type CommandOptionFunc func(cmd *cobra.Command)

Jump to

Keyboard shortcuts

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