-
Notifications
You must be signed in to change notification settings - Fork 0
feat(config): adding a configuration loader #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
| "time" | ||
| ) | ||
|
|
||
| // Loader reads configuration values from environment variables, | ||
| // optionally resolving GCP secrets. Errors are accumulated and | ||
| // surfaced via Err(). | ||
| type Loader struct { | ||
| ctx context.Context | ||
| err error | ||
| } | ||
|
|
||
| // NewLoader returns a new Loader bound to the given context. | ||
| func NewLoader(ctx context.Context) *Loader { | ||
| return &Loader{ctx: ctx} | ||
| } | ||
|
|
||
| // Err returns the first error encountered during loading, if any. | ||
| func (l *Loader) Err() error { | ||
| return l.err | ||
| } | ||
|
|
||
| // MustErr panics if any error was encountered during loading. | ||
| // Useful for fail-fast initialisation in main(). | ||
| func (l *Loader) MustErr() { | ||
| if l.err != nil { | ||
| panic(l.err) | ||
| } | ||
| } | ||
|
|
||
| // Str returns the string value of key, or fallback if unset. | ||
| func (l *Loader) Str(key, fallback string) string { | ||
| return load(l, key, fallback, castString) | ||
| } | ||
|
|
||
| // Int returns the int value of key, or fallback if unset. | ||
| func (l *Loader) Int(key string, fallback int) int { | ||
| return load(l, key, fallback, castInt) | ||
| } | ||
|
|
||
| // Int64 returns the int64 value of key, or fallback if unset. | ||
| func (l *Loader) Int64(key string, fallback int64) int64 { | ||
| return load(l, key, fallback, castInt64) | ||
| } | ||
|
|
||
| // Float64 returns the float64 value of key, or fallback if unset. | ||
| func (l *Loader) Float64(key string, fallback float64) float64 { | ||
| return load(l, key, fallback, castFloat64) | ||
| } | ||
|
|
||
| // Bool returns the boolean value of key, or fallback if unset. | ||
| // Accepted truthy values: "1", "t", "true" (case-insensitive). | ||
| func (l *Loader) Bool(key string, fallback bool) bool { | ||
| return load(l, key, fallback, castBool) | ||
| } | ||
|
|
||
| // Duration returns the time.Duration value of key, or fallback if unset. | ||
| // Values must be valid Go duration strings, e.g. "5s", "1m30s". | ||
| func (l *Loader) Duration(key string, fallback time.Duration) time.Duration { | ||
| return load(l, key, fallback, castDuration) | ||
| } | ||
|
|
||
| // Required returns the string value of key. If the key is unset, it | ||
| // records an error and returns an empty string. Use when there is no | ||
| // sensible fallback. | ||
| func (l *Loader) Required(key string) string { | ||
|
Comment on lines
+69
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pas de "Required" pour autre chose que les string ? Ah, l'idée est peut-être de faire Je me demande si il n'y a pas une meilleure interface possible 🤔 |
||
| if l.err != nil { | ||
| return "" | ||
| } | ||
| raw := os.Getenv(key) | ||
| if raw == "" { | ||
| l.err = fmt.Errorf("config: required key %q is not set", key) | ||
| return "" | ||
| } | ||
| resolved, err := ResolveSecretFromGCP(l.ctx, raw) | ||
| if err != nil { | ||
| l.err = fmt.Errorf("config: resolve %q: %w", key, err) | ||
| return "" | ||
| } | ||
| return resolved | ||
| } | ||
|
|
||
| // load is the generic backbone used by all typed accessors. | ||
| // It resolves the env var through GCP if needed, then casts it. | ||
| func load[T any](l *Loader, key string, fallback T, cast func(string) (T, error)) T { | ||
| if l.err != nil { | ||
| return fallback | ||
| } | ||
|
Comment on lines
+92
to
+94
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on s'arrête à la première erreur ? On pourrait stocker une slice puis les |
||
| raw := os.Getenv(key) | ||
| if raw == "" { | ||
| return fallback | ||
| } | ||
| resolved, err := ResolveSecretFromGCP(l.ctx, raw) | ||
| if err != nil { | ||
| l.err = fmt.Errorf("config: resolve %q: %w", key, err) | ||
| return fallback | ||
| } | ||
| val, err := cast(resolved) | ||
| if err != nil { | ||
| l.err = fmt.Errorf("config: cast %q=%q: %w", key, resolved, err) | ||
| return fallback | ||
| } | ||
| return val | ||
| } | ||
|
|
||
| func castString(s string) (string, error) { | ||
| return s, nil | ||
| } | ||
|
|
||
| func castInt(s string) (int, error) { | ||
| v, err := strconv.Atoi(s) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("expected int, got %q", s) | ||
| } | ||
| return v, nil | ||
| } | ||
|
|
||
| func castInt64(s string) (int64, error) { | ||
| v, err := strconv.ParseInt(s, 10, 64) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("expected int64, got %q", s) | ||
| } | ||
| return v, nil | ||
| } | ||
|
|
||
| func castFloat64(s string) (float64, error) { | ||
| v, err := strconv.ParseFloat(s, 64) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("expected float64, got %q", s) | ||
| } | ||
| return v, nil | ||
| } | ||
|
|
||
| func castBool(s string) (bool, error) { | ||
| v, err := strconv.ParseBool(s) | ||
| if err != nil { | ||
| return false, fmt.Errorf("expected bool, got %q", s) | ||
| } | ||
| return v, nil | ||
| } | ||
|
|
||
| func castDuration(s string) (time.Duration, error) { | ||
| v, err := time.ParseDuration(s) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("expected duration (e.g. \"5s\"), got %q", s) | ||
| } | ||
| return v, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pas fan du naming
Peut-être just
Must?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pour me justifier :
mustest normalement suivi d'un verbeDu coup ma suggestion n'est pas terrible non plus, mais je ne trouve pas le bon verbe
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
j'aime bien juste Must, l'habitude avec les uuid google sûrement
https://github.com/google/uuid/blob/master/uuid.go#L210