Skip to content

Commit

Permalink
Fix mockery failing on --help or --version
Browse files Browse the repository at this point in the history
Closes #879. `version` is instead now a subcommand instead of a flag.
  • Loading branch information
LandonTClipp committed Jan 2, 2025
1 parent 81f0b3d commit f5b3134
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 39 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tasks:
mocks.generate.mockery:
silent: True
cmds:
- MOCKERY_CONFIG=./.mockery.yaml go run .
- MOCKERY_CONFIG=./.mockery.yml go run .

mocks.generate.moq:
silent: True
Expand Down
73 changes: 35 additions & 38 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
)

var (
cfgFile = ""
cfgFile = ""
ErrCfgFileNotFound = errors.New("config file not found")
)

func NewRootCmd() (*cobra.Command, error) {
viperCfg, err := getConfig(nil, nil)
if err != nil {
if err != nil && !errors.Is(err, ErrCfgFileNotFound) {
return nil, err
}
cmd := &cobra.Command{
Expand All @@ -46,7 +47,6 @@ func NewRootCmd() (*cobra.Command, error) {

pFlags := cmd.PersistentFlags()
pFlags.StringVar(&cfgFile, "config", "", "config file to use")
pFlags.Bool("version", false, "prints the installed version of mockery")
pFlags.String("tags", "", "space-separated list of additional build tags to load packages")
pFlags.String("mock-build-tags", "", "set the build tags of the generated mocks. Read more about the format: https://pkg.go.dev/cmd/go#hdr-Build_constraints")
pFlags.String("log-level", "info", "Level of logging")
Expand All @@ -58,6 +58,7 @@ func NewRootCmd() (*cobra.Command, error) {
}

cmd.AddCommand(NewShowConfigCmd())
cmd.AddCommand(NewVersionCmd())
return cmd, nil
}

Expand Down Expand Up @@ -100,47 +101,43 @@ func getConfig(
viperObj.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viperObj.AutomaticEnv()

if !viperObj.GetBool("disable-config-search") {
if configPath == nil && cfgFile != "" {
// Use config file from the flag.
viperObj.SetConfigFile(cfgFile)
} else if configPath != nil {
viperObj.SetConfigFile(configPath.String())
} else if viperObj.IsSet("config") {
viperObj.SetConfigFile(viperObj.GetString("config"))
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Fatal().Err(err).Msgf("Failed to find homedir")
}
if configPath == nil && cfgFile != "" {
// Use config file from the flag.
viperObj.SetConfigFile(cfgFile)
} else if configPath != nil {
viperObj.SetConfigFile(configPath.String())
} else if viperObj.IsSet("config") {
viperObj.SetConfigFile(viperObj.GetString("config"))
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
log.Fatal().Err(err).Msgf("Failed to find homedir")
}

currentDir := baseSearchPath
currentDir := baseSearchPath

for {
viperObj.AddConfigPath(currentDir.String())
if len(currentDir.Parts()) <= 1 {
break
}
currentDir = currentDir.Parent()
for {
viperObj.AddConfigPath(currentDir.String())
if len(currentDir.Parts()) <= 1 {
break
}

viperObj.AddConfigPath(home)
viperObj.SetConfigName(".mockery")
}
if err := viperObj.ReadInConfig(); err != nil {
log, _ := logging.GetLogger("debug")
log.Err(err).Msg("couldn't read any config file")
return nil, err
currentDir = currentDir.Parent()
}

viperObj.AddConfigPath(home)
viperObj.SetConfigName(".mockery")
}
if err := viperObj.ReadInConfig(); err != nil {
log, _ := logging.GetLogger("debug")
log.Debug().Err(err).Msg("couldn't read any config file")
return viperObj, ErrCfgFileNotFound
}

viperObj.Set("config", viperObj.ConfigFileUsed())
return viperObj, nil
}

const regexMetadataChars = "\\.+*?()|[]{}^$"

type RootApp struct {
pkg.Config
}
Expand Down Expand Up @@ -206,17 +203,17 @@ func (r *RootApp) Run() error {
return err
}
log.Info().Msgf("Starting mockery")
if r.Config.Config == "" {
log.Error().Msg("Config file not found. Mockery requires a config file to run.")
return ErrCfgFileNotFound
}
log.Info().Msgf("Using config: %s", r.Config.Config)
ctx := log.WithContext(context.Background())

if err := r.Config.Initialize(ctx); err != nil {
return err
}

if r.Config.Version {
fmt.Println(logging.GetSemverInfo())
return nil
}
buildTags := strings.Split(r.Config.BuildTags, " ")

configuredPackages, err := r.Config.GetPackages(ctx)
Expand Down
18 changes: 18 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/vektra/mockery/v3/pkg/logging"
)

func NewVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version of mockery",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(logging.GetSemverInfo())
},
}
}

0 comments on commit f5b3134

Please sign in to comment.