forked from mirror/cobra
Correct all complaints from golint
* i.e. * go get golang.org/x/lint/golint * go list ./... | xargs golint
This commit is contained in:
parent
9334a46bd6
commit
51f06c7dd1
1
args.go
1
args.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// PositionalArgs defines positional arguments callback
|
||||||
type PositionalArgs func(cmd *Command, args []string) error
|
type PositionalArgs func(cmd *Command, args []string) error
|
||||||
|
|
||||||
// Legacy arg validation has the following behaviour:
|
// Legacy arg validation has the following behaviour:
|
||||||
|
|
4
cobra.go
4
cobra.go
|
@ -52,7 +52,7 @@ var EnableCommandSorting = true
|
||||||
// if the CLI is started from explorer.exe.
|
// if the CLI is started from explorer.exe.
|
||||||
// To disable the mousetrap, just set this variable to blank string ("").
|
// To disable the mousetrap, just set this variable to blank string ("").
|
||||||
// Works only on Microsoft Windows.
|
// Works only on Microsoft Windows.
|
||||||
var MousetrapHelpText string = `This is a command line tool.
|
var MousetrapHelpText = `This is a command line tool.
|
||||||
|
|
||||||
You need to open cmd.exe and run it from there.
|
You need to open cmd.exe and run it from there.
|
||||||
`
|
`
|
||||||
|
@ -61,7 +61,7 @@ You need to open cmd.exe and run it from there.
|
||||||
// if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed.
|
// if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed.
|
||||||
// To disable the mousetrap, just set MousetrapHelpText to blank string ("").
|
// To disable the mousetrap, just set MousetrapHelpText to blank string ("").
|
||||||
// Works only on Microsoft Windows.
|
// Works only on Microsoft Windows.
|
||||||
var MousetrapDisplayDuration time.Duration = 5 * time.Second
|
var MousetrapDisplayDuration = 5 * time.Second
|
||||||
|
|
||||||
// AddTemplateFunc adds a template function that's available to Usage and Help
|
// AddTemplateFunc adds a template function that's available to Usage and Help
|
||||||
// template generation.
|
// template generation.
|
||||||
|
|
|
@ -2,9 +2,10 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra/cobra/tpl"
|
|
||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra/cobra/tpl"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Project contains name, license and paths to projects.
|
// Project contains name, license and paths to projects.
|
||||||
|
@ -18,14 +19,15 @@ type Project struct {
|
||||||
AppName string
|
AppName string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Command structure
|
||||||
type Command struct {
|
type Command struct {
|
||||||
CmdName string
|
CmdName string
|
||||||
CmdParent string
|
CmdParent string
|
||||||
*Project
|
*Project
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create project receiver
|
||||||
func (p *Project) Create() error {
|
func (p *Project) Create() error {
|
||||||
|
|
||||||
// check if AbsolutePath exists
|
// check if AbsolutePath exists
|
||||||
if _, err := os.Stat(p.AbsolutePath); os.IsNotExist(err) {
|
if _, err := os.Stat(p.AbsolutePath); os.IsNotExist(err) {
|
||||||
// create directory
|
// create directory
|
||||||
|
@ -80,6 +82,7 @@ func (p *Project) createLicenseFile() error {
|
||||||
return licenseTemplate.Execute(licenseFile, data)
|
return licenseTemplate.Execute(licenseFile, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create command receiver
|
||||||
func (c *Command) Create() error {
|
func (c *Command) Create() error {
|
||||||
cmdFile, err := os.Create(fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName))
|
cmdFile, err := os.Create(fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package tpl
|
package tpl
|
||||||
|
|
||||||
|
// MainTemplate defines main template string
|
||||||
func MainTemplate() []byte {
|
func MainTemplate() []byte {
|
||||||
return []byte(`/*
|
return []byte(`/*
|
||||||
{{ .Copyright }}
|
{{ .Copyright }}
|
||||||
|
@ -15,6 +16,7 @@ func main() {
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RootTemplate defines root template string
|
||||||
func RootTemplate() []byte {
|
func RootTemplate() []byte {
|
||||||
return []byte(`/*
|
return []byte(`/*
|
||||||
{{ .Copyright }}
|
{{ .Copyright }}
|
||||||
|
@ -108,6 +110,7 @@ func initConfig() {
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddCommandTemplate defines add command template string
|
||||||
func AddCommandTemplate() []byte {
|
func AddCommandTemplate() []byte {
|
||||||
return []byte(`/*
|
return []byte(`/*
|
||||||
{{ .Project.Copyright }}
|
{{ .Project.Copyright }}
|
||||||
|
|
13
command.go
13
command.go
|
@ -28,8 +28,8 @@ import (
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NotRunnable defines subcommand error
|
// ErrSubCommandRequired defines subcommand error
|
||||||
var NotRunnable = errors.New("subcommand is required")
|
var ErrSubCommandRequired = errors.New("subcommand is required")
|
||||||
|
|
||||||
// FParseErrWhitelist configures Flag parse errors to be ignored
|
// FParseErrWhitelist configures Flag parse errors to be ignored
|
||||||
type FParseErrWhitelist flag.ParseErrorsWhitelist
|
type FParseErrWhitelist flag.ParseErrorsWhitelist
|
||||||
|
@ -232,7 +232,7 @@ func (c *Command) SetErr(newErr io.Writer) {
|
||||||
c.errWriter = newErr
|
c.errWriter = newErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetOut sets the source for input data
|
// SetIn sets the source for input data
|
||||||
// If newIn is nil, os.Stdin is used.
|
// If newIn is nil, os.Stdin is used.
|
||||||
func (c *Command) SetIn(newIn io.Reader) {
|
func (c *Command) SetIn(newIn io.Reader) {
|
||||||
c.inReader = newIn
|
c.inReader = newIn
|
||||||
|
@ -301,7 +301,7 @@ func (c *Command) ErrOrStderr() io.Writer {
|
||||||
return c.getErr(os.Stderr)
|
return c.getErr(os.Stderr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrOrStderr returns output to stderr
|
// InOrStdin returns output to stderr
|
||||||
func (c *Command) InOrStdin() io.Reader {
|
func (c *Command) InOrStdin() io.Reader {
|
||||||
return c.getIn(os.Stdin)
|
return c.getIn(os.Stdin)
|
||||||
}
|
}
|
||||||
|
@ -790,7 +790,7 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.Runnable() {
|
if !c.Runnable() {
|
||||||
return NotRunnable
|
return ErrSubCommandRequired
|
||||||
}
|
}
|
||||||
|
|
||||||
c.preRun()
|
c.preRun()
|
||||||
|
@ -927,7 +927,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
// If command wasn't runnable, show full help, but do return the error.
|
// If command wasn't runnable, show full help, but do return the error.
|
||||||
// This will result in apps by default returning a non-success exit code, but also gives them the option to
|
// This will result in apps by default returning a non-success exit code, but also gives them the option to
|
||||||
// handle specially.
|
// handle specially.
|
||||||
if err == NotRunnable {
|
if err == ErrSubCommandRequired {
|
||||||
cmd.HelpFunc()(cmd, args)
|
cmd.HelpFunc()(cmd, args)
|
||||||
return cmd, err
|
return cmd, err
|
||||||
}
|
}
|
||||||
|
@ -947,6 +947,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
return cmd, err
|
return cmd, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidateArgs validates arguments
|
||||||
func (c *Command) ValidateArgs(args []string) error {
|
func (c *Command) ValidateArgs(args []string) error {
|
||||||
if c.Args == nil {
|
if c.Args == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -836,7 +836,7 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
||||||
rootCmd.AddCommand(childCmd)
|
rootCmd.AddCommand(childCmd)
|
||||||
|
|
||||||
output, err := executeCommand(rootCmd, "child")
|
output, err := executeCommand(rootCmd, "child")
|
||||||
if err != NotRunnable {
|
if err != ErrSubCommandRequired {
|
||||||
t.Errorf("Expected error")
|
t.Errorf("Expected error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue