From 4ac72153edb2e1d1226a60e1b7159b0a7c5ee0e2 Mon Sep 17 00:00:00 2001 From: Gaurav Gahlot Date: Fri, 7 Oct 2022 23:42:36 +0530 Subject: [PATCH] make lint happy --- cmd/gidari/cmd/root.go | 65 ++++++++++++++++++++++-------------------- cmd/gidari/main.go | 2 +- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/cmd/gidari/cmd/root.go b/cmd/gidari/cmd/root.go index b05a2e86..6a388efc 100644 --- a/cmd/gidari/cmd/root.go +++ b/cmd/gidari/cmd/root.go @@ -9,7 +9,7 @@ package cmd import ( "context" - "fmt" + "errors" "os" "strings" @@ -24,44 +24,47 @@ const ( flagVerbose = "verbose" ) -var ( - configFilepath string // configFilepath is the path to the configuration file. - verbose bool // verbose is a flag that enables verbose logging. -) - -var rootCMD = &cobra.Command{ - Long: "Gidari is a tool for querying web APIs and persisting resultant data onto local storage\n" + - "using a configuration file.", - - Use: "gidari", - Short: "Persist data from the web to your database", - Example: "gidari --config config.yaml", - Version: version.Gidari, - PreRunE: func(cmd *cobra.Command, args []string) error { - v, err := cmd.Flags().GetString(flagConfig) - if err != nil { - return err - } - - if !strings.HasSuffix(v, ".yaml") && !strings.HasSuffix(v, ".yml") { - return fmt.Errorf("configuration must be a YAML document") - } - - return nil - }, - RunE: runE, -} +func RootCommand() *cobra.Command { + var ( + configFilepath string // configFilepath is the path to the configuration file. + verbose bool // verbose is a flag that enables verbose logging. + ) + + rootCMD := &cobra.Command{ + Long: "Gidari is a tool for querying web APIs and persisting resultant data onto local storage\n" + + "using a configuration file.", + + Use: "gidari", + Short: "Persist data from the web to your database", + Example: "gidari --config config.yaml", + Version: version.Gidari, + PreRunE: func(cmd *cobra.Command, args []string) error { + value, err := cmd.Flags().GetString(flagConfig) + if err != nil { + //nolint:wrapcheck // need not wrap the error + return err + } + + if !strings.HasSuffix(value, ".yaml") && !strings.HasSuffix(value, ".yml") { + //nolint:goerr113 // don't have static error + return errors.New("configuration must be a YAML document") + } + + return nil + }, + RunE: func(_ *cobra.Command, args []string) error { return runE(configFilepath, verbose) }, + } -func Execute() error { rootCMD.PersistentFlags().StringVarP(&configFilepath, flagConfig, "c", "", "path to configuration") rootCMD.PersistentFlags().BoolVar(&verbose, flagVerbose, false, "print log data as the binary executes") _ = rootCMD.MarkPersistentFlagRequired(flagConfig) - return rootCMD.Execute() + return rootCMD } -func runE(cmd *cobra.Command, _ []string) error { +//nolint:wrapcheck // need not wrap the error +func runE(configFilepath string, verbose bool) error { file, err := os.Open(configFilepath) if err != nil { return err diff --git a/cmd/gidari/main.go b/cmd/gidari/main.go index 707d6a79..00a2baea 100644 --- a/cmd/gidari/main.go +++ b/cmd/gidari/main.go @@ -15,7 +15,7 @@ import ( ) func main() { - if err := cmd.Execute(); err != nil { + if err := cmd.RootCommand().Execute(); err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) }