Skip to content

Commit

Permalink
cfg add GetConfigLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
sxwebdev authored Oct 1, 2024
1 parent a5a8dc9 commit 7bf4b6c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 38 deletions.
32 changes: 16 additions & 16 deletions cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)

Expand Down
38 changes: 38 additions & 0 deletions cfg/loader.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 7 additions & 7 deletions cfg/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 10 additions & 10 deletions cfg/types.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 5 additions & 5 deletions cfg/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
)
}
Expand Down

0 comments on commit 7bf4b6c

Please sign in to comment.