Skip to content

Commit

Permalink
Command clean up
Browse files Browse the repository at this point in the history
Avoid package and variable name collision
Avoid constructing a version struct each time kubebuilder version is called
Turn version strings into constants
Ignoring errors explicitly
Use initial lower case for error messages

Signed-off-by: Adrian Orive <[email protected]>
  • Loading branch information
Adirio committed Oct 31, 2019
1 parent 90850ad commit a7aec50
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
12 changes: 6 additions & 6 deletions cmd/init_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func (o *projectOptions) bindCmdlineFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&o.dep, "dep", true, "if specified, determines whether dep will be used.")
o.depFlag = cmd.Flag("dep")
cmd.Flags().StringArrayVar(&o.depArgs, "depArgs", nil, "Additional arguments for dep")
cmd.Flags().MarkDeprecated("dep", "use the fetch-deps flag instead")
cmd.Flags().MarkDeprecated("depArgs", "will be removed with version 1 scaffolding")
_ = cmd.Flags().MarkDeprecated("dep", "use the fetch-deps flag instead")
_ = cmd.Flags().MarkDeprecated("depArgs", "will be removed with version 1 scaffolding")

// boilerplate args
cmd.Flags().StringVar(&o.boilerplate.Path, "path", "", "path for boilerplate")
Expand Down Expand Up @@ -168,7 +168,7 @@ func (o *projectOptions) validate() error {
}

if util.ProjectExist() {
return fmt.Errorf("Failed to initialize project because project is already initialized")
return fmt.Errorf("failed to initialize project because project is already initialized")
}

return nil
Expand All @@ -186,16 +186,16 @@ func fetchAndCheckGoVersion() error {
cmd := exec.Command("go", "version")
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("Failed to retrieve 'go version': %v", string(out))
return fmt.Errorf("failed to retrieve 'go version': %v", string(out))
}

split := strings.Split(string(out), " ")
if len(split) < 3 {
return fmt.Errorf("Found invalid Go version: %q", string(out))
return fmt.Errorf("found invalid Go version: %q", string(out))
}
goVer := split[2]
if err := checkGoVersion(goVer); err != nil {
return fmt.Errorf("Go version '%s' is incompatible because '%s'", goVer, err)
return fmt.Errorf("go version '%s' is incompatible because '%s'", goVer, err)
}
return nil
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func findCurrentRepo() (string, error) {
// give up, let the user figure it out
return "", fmt.Errorf("could not determine repository path from module data, package data, or by initializing a module: %v", err)
}
defer os.Remove("go.mod") // clean up after ourselves
defer func() {
_ = os.Remove("go.mod")
}() // clean up after ourselves
return findGoModulePath(true)
}

Expand All @@ -111,8 +113,8 @@ func main() {
version.NewVersionCmd(),
)

foundProject, version := getProjectVersion()
if foundProject && version == "1" {
foundProject, versionString := getProjectVersion()
if foundProject && versionString == "1" {
rootCmd.AddCommand(
newAlphaCommand(),
newVendorUpdateCmd(),
Expand Down Expand Up @@ -171,7 +173,7 @@ After the scaffold is written, api will run make on the project.
`,

Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
_ = cmd.Help()
},
}
}
Expand Down
22 changes: 10 additions & 12 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/spf13/cobra"
)

var (
const (
kubeBuilderVersion = "unknown"
kubernetesVendorVersion = "unknown"
goos = "unknown"
Expand All @@ -41,15 +41,13 @@ type Version struct {
GoArch string `json:"goArch"`
}

func GetVersion() Version {
return Version{
kubeBuilderVersion,
kubernetesVendorVersion,
gitCommit,
buildDate,
goos,
goarch,
}
var version = Version{
kubeBuilderVersion,
kubernetesVendorVersion,
gitCommit,
buildDate,
goos,
goarch,
}

func (v Version) Print() {
Expand All @@ -66,6 +64,6 @@ func NewVersionCmd() *cobra.Command {
}
}

func runVersion(cmd *cobra.Command, args []string) {
GetVersion().Print()
func runVersion(_ *cobra.Command, _ []string) {
version.Print()
}

0 comments on commit a7aec50

Please sign in to comment.