cobrax

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: MIT Imports: 11 Imported by: 8

README

cobrax

CI Status Go Reference Go report

A thin wrapper around library spf13/cobra that streamlines the integration of library spf13/viper and spf13/afero.

Usage

package main

import (
	"fmt"
	"os"

	"github.com/haijima/cobrax"
	"github.com/spf13/afero"
	"github.com/spf13/viper"
)

func main() {
	rootCmd := cobrax.NewCommand(viper.New(), afero.NewOsFs())
	rootCmd.Use = "test-cli"
	rootCmd.Short = "cobrax sample CLI"
	rootCmd.Run = func(cmd *cobrax.Command, args []string) {
		fmt.Println("test-cli called")
	}

	if err := rootCmd.Execute(); err != nil {
		os.Exit(1)
	}
}

Installation

go get github.com/haijima/cobrax@latest

License

This tool is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	*cobra.Command

	D *log.Logger
	V *log.Logger

	UseConfigFile      bool
	UseEnv             bool
	UseDebugLogging    bool
	AutomaticBindViper bool

	// PersistentPreRun: children of this command will inherit and execute.
	PersistentPreRun func(cmd *Command, args []string)
	// PersistentPreRunE: PersistentPreRun but returns an error.
	PersistentPreRunE func(cmd *Command, args []string) error
	// PreRun: children of this command will not inherit.
	PreRun func(cmd *Command, args []string)
	// PreRunE: PreRun but returns an error.
	PreRunE func(cmd *Command, args []string) error
	// Run: Typically the actual work function. Most commands will only implement this.
	Run func(cmd *Command, args []string)
	// RunE: Run but returns an error.
	RunE func(cmd *Command, args []string) error
	// PostRun: run after the Run command.
	PostRun func(cmd *Command, args []string)
	// PostRunE: PostRun but returns an error.
	PostRunE func(cmd *Command, args []string) error
	// PersistentPostRun: children of this command will inherit and execute after PostRun.
	PersistentPostRun func(cmd *Command, args []string)
	// PersistentPostRunE: PersistentPostRun but returns an error.
	PersistentPostRunE func(cmd *Command, args []string) error
	// contains filtered or unexported fields
}

Command is a wrapper around cobra.Command that adds some additional functionality.

func NewCommand

func NewCommand(v *viper.Viper, fs afero.Fs) *Command

NewCommand creates a new Command.

func Wrap

func Wrap(cmd *cobra.Command, v *viper.Viper, fs afero.Fs) *Command

Wrap creates a new Command from a cobra.Command.

func (*Command) AddCommand

func (c *Command) AddCommand(commands ...*Command)

AddCommand adds a command to the command.

func (*Command) BindEnv

func (c *Command) BindEnv(input ...string) error

BindEnv binds a viper key to an environment variable.

func (*Command) BindFlag

func (c *Command) BindFlag(key string) error

BindFlag binds a flag to a viper key.

func (*Command) BindFlags

func (c *Command) BindFlags() error

BindFlags binds all flags to viper.

func (*Command) BindInheritedFlag

func (c *Command) BindInheritedFlag(key string) error

BindInheritedFlag binds a flag inherited from a parent command to a viper key.

func (*Command) BindInheritedFlags

func (c *Command) BindInheritedFlags() error

BindInheritedFlags binds all flags inherited from a parent command to viper.

func (*Command) BindLocalFlag

func (c *Command) BindLocalFlag(key string) error

BindLocalFlag binds a local flag to a viper key.

func (*Command) BindLocalFlags

func (c *Command) BindLocalFlags() error

BindLocalFlags binds all local flags to viper.

func (*Command) BindLocalNonPersistentFlag

func (c *Command) BindLocalNonPersistentFlag(key string) error

BindLocalNonPersistentFlag binds a flag specific only to this to a viper key.

func (*Command) BindLocalNonPersistentFlags

func (c *Command) BindLocalNonPersistentFlags() error

BindLocalNonPersistentFlags binds all flags specific to only this command to viper.

func (*Command) BindNonInheritedFlag

func (c *Command) BindNonInheritedFlag(key string) error

BindNonInheritedFlag binds a flag which were not inherited from parent commands to a viper key.

func (*Command) BindNonInheritedFlags

func (c *Command) BindNonInheritedFlags() error

BindNonInheritedFlags binds all flags which were not inherited from parent commands to viper.

func (*Command) BindPersistentFlag

func (c *Command) BindPersistentFlag(key string) error

BindPersistentFlag binds a persistent flag to a viper key.

func (*Command) BindPersistentFlags

func (c *Command) BindPersistentFlags() error

BindPersistentFlags binds all persistent flags to viper.

func (*Command) Commands

func (c *Command) Commands() []*Command

Commands returns a sorted slice of child commands.

func (*Command) Execute

func (c *Command) Execute() error

Execute is a wrapper around cobra.Command.Execute.

func (*Command) ExecuteC

func (c *Command) ExecuteC() (cmd *cobra.Command, err error)

ExecuteC is a wrapper around cobra.Command.ExecuteC.

func (*Command) ExecuteContext

func (c *Command) ExecuteContext(ctx context.Context) error

ExecuteContext is a wrapper around cobra.Command.ExecuteContext.

func (*Command) ExecuteContextC

func (c *Command) ExecuteContextC(ctx context.Context) (*cobra.Command, error)

ExecuteContextC is a wrapper around cobra.Command.ExecuteContextC.

func (*Command) Fs

func (c *Command) Fs() afero.Fs

Fs returns the afero.Fs filesystem.

func (*Command) PrintOut

func (c *Command) PrintOut(i ...interface{})

PrintOut is a convenience method to Print to the defined output, fallback to Stdout if not set.

func (*Command) PrintOutf

func (c *Command) PrintOutf(format string, i ...interface{})

PrintOutf is a convenience method to Printf to the defined output, fallback to Stdout if not set.

func (*Command) PrintOutln

func (c *Command) PrintOutln(i ...interface{})

PrintOutln is a convenience method to Println to the defined output, fallback to Stdout if not set.

func (*Command) ReadFileOrStdIn

func (c *Command) ReadFileOrStdIn(fileFlag string) (io.ReadCloser, error)

ReadFileOrStdIn returns io.ReadCloser. If a file is specified, it is opened and returned. Otherwise, stdin is returned. When a file is returned, it must be closed by the caller.

func (*Command) RemoveCommand

func (c *Command) RemoveCommand(cmds ...*Command)

func (*Command) ResetCommands

func (c *Command) ResetCommands()

func (*Command) Root

func (c *Command) Root() *Command

func (*Command) Viper

func (c *Command) Viper() *viper.Viper

Viper returns the viper.Viper instance.

func (*Command) WalkCommands

func (c *Command) WalkCommands(fn func(*Command))

Jump to

Keyboard shortcuts

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