Skip to content

Commit

Permalink
Fix issue with log-level and config not being set to default values.
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 6, 2025
1 parent 237c4fa commit 7b76134
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
14 changes: 3 additions & 11 deletions internal/cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func NewRootCmd() (*cobra.Command, error) {
}
level, err := pFlags.GetString("log-level")
if err != nil {
fmt.Printf("failed to get log-level from flags: %s", err.Error())
fmt.Printf("failed to get log-level from flags: %s\n", err.Error())
os.Exit(1)
}
log, err := logging.GetLogger(level)
if err != nil {
fmt.Printf("failed to get logger: %s", err.Error())
fmt.Printf("failed to get logger: %s\n", err.Error())
os.Exit(1)
}
ctx := log.WithContext(context.Background())
Expand Down Expand Up @@ -91,16 +91,8 @@ type RootApp struct {
}

func GetRootApp(ctx context.Context, flags *pflag.FlagSet) (*RootApp, error) {
var configFile *pathlib.Path
r := &RootApp{}
configFromFlags, err := flags.GetString("config")
if err != nil {
return nil, err
}
if configFromFlags != "" {
configFile = pathlib.NewPath(configFromFlags)
}
config, _, err := pkg.NewRootConfig(ctx, configFile, flags)
config, _, err := pkg.NewRootConfig(ctx, flags)
if err != nil {
return nil, stackerr.NewStackErrf(err, "failed to get config")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/showconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewShowConfigCmd() *cobra.Command {
}

ctx := log.WithContext(context.Background())
conf, _, err := pkg.NewRootConfig(ctx, nil, cmd.Parent().PersistentFlags())
conf, _, err := pkg.NewRootConfig(ctx, cmd.Parent().PersistentFlags())
if err != nil {
return err
}
Expand Down
43 changes: 19 additions & 24 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,17 @@ type RootConfig struct {

func NewRootConfig(
ctx context.Context,
configFile *pathlib.Path,
flags *pflag.FlagSet,
) (*RootConfig, *koanf.Koanf, error) {
var configFile *pathlib.Path

log := zerolog.Ctx(ctx)
var err error
strPtr := func(s string) *string {
return &s
}
defaultConfig := &Config{
Dir: strPtr("{{.InterfaceDir}}"),
FileName: strPtr("mocks_test.go"),
Formatter: strPtr("goimports"),
MockName: strPtr("Mock{{.InterfaceName}}"),
PkgName: strPtr("{{.SrcPackageName}}"),
LogLevel: strPtr("info"),
}
// Set all the other parameters to their respective zero-values. Need to use

conf := &Config{}
// Set all parameters to their respective zero-values. Need to use
// reflection for this sadly.
v := reflect.ValueOf(defaultConfig).Elem()
v := reflect.ValueOf(conf).Elem()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if field.Kind() != reflect.Pointer {
Expand All @@ -69,26 +61,29 @@ func NewRootConfig(
}

var rootConfig RootConfig = RootConfig{
Config: defaultConfig,
Config: conf,
}
k := koanf.New("|")
rootConfig.koanf = k
if configFile != nil {
log.Debug().Str("config-file-args", configFile.String()).Msg("config file from args")
}
k.Set("dir", "{{.InterfaceDir}}")
k.Set("filename", "mocks_test.go")
k.Set("formatter", "goimports")
k.Set("mockname", "Mock{{.InterfaceName}}")
k.Set("pkgname", "{{.SrcPackageName}}")
k.Set("log-level", "info")

if configFile == nil {
configFileFromEnv := os.Getenv("MOCKERY_CONFIG")
if configFileFromEnv != "" {
configFile = pathlib.NewPath(configFileFromEnv)
}
configFileFromEnv := os.Getenv("MOCKERY_CONFIG")
if configFileFromEnv != "" {
configFile = pathlib.NewPath(configFileFromEnv)
}
if configFile == nil {
configFileFromFlags, err := flags.GetString("config")
if err != nil {
return nil, nil, fmt.Errorf("getting --config from flags: %w", err)
}
configFile = pathlib.NewPath(configFileFromFlags)
if configFileFromFlags != "" {
configFile = pathlib.NewPath(configFileFromFlags)
}
}
if configFile == nil {
log.Debug().Msg("config file not specified, searching")
Expand Down

0 comments on commit 7b76134

Please sign in to comment.