Skip to content

Commit

Permalink
make lint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravgahlot committed Oct 7, 2022
1 parent cb8beb5 commit 4ac7215
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
65 changes: 34 additions & 31 deletions cmd/gidari/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package cmd

import (
"context"
"fmt"
"errors"
"os"
"strings"

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/gidari/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 4ac7215

Please sign in to comment.