Skip to content

Commit

Permalink
Correct all complaints from golint
Browse files Browse the repository at this point in the history
* i.e.
* go get golang.org/x/lint/golint
* go list ./... | xargs golint
  • Loading branch information
bruceadowns authored and n10v committed Aug 1, 2019
1 parent 9334a46 commit 51f06c7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
)

// PositionalArgs defines positional arguments callback
type PositionalArgs func(cmd *Command, args []string) error

// Legacy arg validation has the following behaviour:
Expand Down
4 changes: 2 additions & 2 deletions cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var EnableCommandSorting = true
// if the CLI is started from explorer.exe.
// To disable the mousetrap, just set this variable to blank string ("").
// 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.
`
Expand All @@ -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.
// To disable the mousetrap, just set MousetrapHelpText to blank string ("").
// 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
// template generation.
Expand Down
7 changes: 5 additions & 2 deletions cobra/cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra/cobra/tpl"
"os"
"text/template"

"github.com/spf13/cobra/cobra/tpl"
)

// Project contains name, license and paths to projects.
Expand All @@ -18,14 +19,15 @@ type Project struct {
AppName string
}

// Command structure
type Command struct {
CmdName string
CmdParent string
*Project
}

// Create project receiver
func (p *Project) Create() error {

// check if AbsolutePath exists
if _, err := os.Stat(p.AbsolutePath); os.IsNotExist(err) {
// create directory
Expand Down Expand Up @@ -80,6 +82,7 @@ func (p *Project) createLicenseFile() error {
return licenseTemplate.Execute(licenseFile, data)
}

// Create command receiver
func (c *Command) Create() error {
cmdFile, err := os.Create(fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName))
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cobra/tpl/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tpl

// MainTemplate defines main template string
func MainTemplate() []byte {
return []byte(`/*
{{ .Copyright }}
Expand All @@ -15,6 +16,7 @@ func main() {
`)
}

// RootTemplate defines root template string
func RootTemplate() []byte {
return []byte(`/*
{{ .Copyright }}
Expand Down Expand Up @@ -108,6 +110,7 @@ func initConfig() {
`)
}

// AddCommandTemplate defines add command template string
func AddCommandTemplate() []byte {
return []byte(`/*
{{ .Project.Copyright }}
Expand Down
13 changes: 7 additions & 6 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
flag "github.com/spf13/pflag"
)

// NotRunnable defines subcommand error
var NotRunnable = errors.New("subcommand is required")
// ErrSubCommandRequired defines subcommand error
var ErrSubCommandRequired = errors.New("subcommand is required")

// FParseErrWhitelist configures Flag parse errors to be ignored
type FParseErrWhitelist flag.ParseErrorsWhitelist
Expand Down Expand Up @@ -232,7 +232,7 @@ func (c *Command) SetErr(newErr io.Writer) {
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.
func (c *Command) SetIn(newIn io.Reader) {
c.inReader = newIn
Expand Down Expand Up @@ -301,7 +301,7 @@ func (c *Command) ErrOrStderr() io.Writer {
return c.getErr(os.Stderr)
}

// ErrOrStderr returns output to stderr
// InOrStdin returns output to stderr
func (c *Command) InOrStdin() io.Reader {
return c.getIn(os.Stdin)
}
Expand Down Expand Up @@ -790,7 +790,7 @@ func (c *Command) execute(a []string) (err error) {
}

if !c.Runnable() {
return NotRunnable
return ErrSubCommandRequired
}

c.preRun()
Expand Down Expand Up @@ -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.
// This will result in apps by default returning a non-success exit code, but also gives them the option to
// handle specially.
if err == NotRunnable {
if err == ErrSubCommandRequired {
cmd.HelpFunc()(cmd, args)
return cmd, err
}
Expand All @@ -947,6 +947,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
return cmd, err
}

// ValidateArgs validates arguments
func (c *Command) ValidateArgs(args []string) error {
if c.Args == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
rootCmd.AddCommand(childCmd)

output, err := executeCommand(rootCmd, "child")
if err != NotRunnable {
if err != ErrSubCommandRequired {
t.Errorf("Expected error")
}

Expand Down

0 comments on commit 51f06c7

Please sign in to comment.