diff --git a/cfg/cfg.go b/cfg/cfg.go index ea01704..6980e6d 100644 --- a/cfg/cfg.go +++ b/cfg/cfg.go @@ -83,32 +83,32 @@ func (c *config) print(value string) { _, _ = fmt.Fprintln(c.out, value) } -func getConfigFields(loader *aconfig.Loader) []configField { - res := []configField{} +func GetConfigFields(loader *aconfig.Loader) []ConfigField { + res := []ConfigField{} loader.WalkFields(func(f aconfig.Field) bool { - newField := configField{ - path: f.Name(), - defaultValue: f.Tag("default"), - usage: f.Tag("usage"), - example: f.Tag("example"), - validateParams: f.Tag("validate"), + newField := ConfigField{ + Path: f.Name(), + DefaultValue: f.Tag("default"), + Usage: f.Tag("usage"), + Example: f.Tag("example"), + ValidateParams: f.Tag("validate"), } if slices.Contains(boolTrueValues, strings.ToLower(f.Tag("required"))) { - newField.isRequired = true + newField.IsRequired = true } if slices.Contains(boolTrueValues, strings.ToLower(f.Tag("secret"))) { - newField.isSecret = true + newField.IsSecret = true } if slices.Contains(boolTrueValues, strings.ToLower(f.Tag("disable_validation"))) { - newField.disableValidation = true + newField.DisableValidation = true } - if strings.Contains(newField.validateParams, "required") { - newField.isRequired = true + if strings.Contains(newField.ValidateParams, "required") { + newField.IsRequired = true } envName := f.Tag("env") @@ -122,16 +122,16 @@ func getConfigFields(loader *aconfig.Loader) []configField { envName = fmt.Sprintf("%s_%s", field.Tag("env"), envName) - if !newField.disableValidation && + if !newField.DisableValidation && slices.Contains( boolTrueValues, strings.ToLower(field.Tag("disable_validation")), ) { - newField.disableValidation = true + newField.DisableValidation = true } } - newField.envName = envName + newField.EnvName = envName res = append(res, newField) diff --git a/cfg/loader.go b/cfg/loader.go new file mode 100644 index 0000000..7c8333b --- /dev/null +++ b/cfg/loader.go @@ -0,0 +1,38 @@ +package cfg + +import ( + "fmt" + "os" + "reflect" + + "github.com/cristalhq/aconfig" +) + +// GetConfigLoader returns aconfig loader instance +func GetConfigLoader(cfg any, opts ...Option) (*aconfig.Loader, error) { + if reflect.ValueOf(cfg).Kind() != reflect.Ptr { + return nil, fmt.Errorf("config must be a pointer") + } + + options := newOptions(opts...) + + c := config{ + out: os.Stdout, + exit: os.Exit, + args: os.Args[1:], + options: options, + } + + aconf, err := getAconfig(c) + if err != nil { + return nil, err + } + + loader := aconfig.LoaderFor(cfg, aconf) + + if err := loader.Load(); err != nil { + return nil, err + } + + return loader, nil +} diff --git a/cfg/markdown.go b/cfg/markdown.go index 1e846b0..cb78ce0 100644 --- a/cfg/markdown.go +++ b/cfg/markdown.go @@ -54,20 +54,20 @@ func (c *config) generateMarkdown(l *aconfig.Loader, filePath string) error { sizes[i] = utf8.RuneCountInString(cell) + 2 } - configFields := getConfigFields(l) + configFields := GetConfigFields(l) for _, f := range configFields { - envName := f.envName + envName := f.EnvName if c.options.loaderConfig.EnvPrefix != "" { envName = c.options.loaderConfig.EnvPrefix + "_" + envName } cell := []string{ "`" + envName + "`", - boolIcon(f.isRequired), - boolIcon(f.isSecret), - codeBlock(f.defaultValue), - f.usage, - codeBlock(f.example), + boolIcon(f.IsRequired), + boolIcon(f.IsSecret), + codeBlock(f.DefaultValue), + f.Usage, + codeBlock(f.Example), } table = append(table, cell) diff --git a/cfg/types.go b/cfg/types.go index 85c5be7..aa413f2 100644 --- a/cfg/types.go +++ b/cfg/types.go @@ -1,13 +1,13 @@ package cfg -type configField struct { - path string - envName string - defaultValue string - usage string - example string - validateParams string - isRequired bool - isSecret bool - disableValidation bool +type ConfigField struct { + Path string + EnvName string + DefaultValue string + Usage string + Example string + ValidateParams string + IsRequired bool + IsSecret bool + DisableValidation bool } diff --git a/cfg/validate.go b/cfg/validate.go index 0035e5b..5fdd316 100644 --- a/cfg/validate.go +++ b/cfg/validate.go @@ -68,23 +68,23 @@ func (c *config) validateEnvs(cfg any, loader *aconfig.Loader) error { // validate struct errs := []string{} - configFields := getConfigFields(loader) + configFields := GetConfigFields(loader) for _, f := range configFields { - if f.disableValidation || f.validateParams == "" { + if f.DisableValidation || f.ValidateParams == "" { continue } - fieldValue, err := structs.LookupString(cfg, f.path) + fieldValue, err := structs.LookupString(cfg, f.Path) if err != nil { return err } - if err := validate.Var(fieldValue.Interface(), f.validateParams); err != nil { + if err := validate.Var(fieldValue.Interface(), f.ValidateParams); err != nil { errs = append(errs, strings.ReplaceAll( err.Error(), "Key: '' Error:Field validation for ''", - fmt.Sprintf("Validate %s env error:", f.envName), + fmt.Sprintf("Validate %s env error:", f.EnvName), ), ) }