|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + stderrors "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/KARTIKrocks/apikit/errors" |
| 8 | + "github.com/KARTIKrocks/apikit/request" |
| 9 | +) |
| 10 | + |
| 11 | +// Option configures the behavior of Load. |
| 12 | +type Option func(*options) |
| 13 | + |
| 14 | +type options struct { |
| 15 | + prefix string |
| 16 | + envFile string |
| 17 | + jsonFile string |
| 18 | + required bool |
| 19 | +} |
| 20 | + |
| 21 | +// WithPrefix sets a prefix for all environment variable lookups. |
| 22 | +// For example, WithPrefix("APP") causes field with env:"PORT" to read APP_PORT. |
| 23 | +func WithPrefix(prefix string) Option { |
| 24 | + return func(o *options) { |
| 25 | + o.prefix = prefix |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// WithEnvFile loads environment variables from a file (e.g., ".env"). |
| 30 | +// Values from the file do not override existing environment variables. |
| 31 | +func WithEnvFile(path string) Option { |
| 32 | + return func(o *options) { |
| 33 | + o.envFile = path |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// WithJSONFile loads configuration from a JSON file as the base layer. |
| 38 | +// Environment variables and .env values take precedence over JSON values. |
| 39 | +func WithJSONFile(path string) Option { |
| 40 | + return func(o *options) { |
| 41 | + o.jsonFile = path |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// WithRequired causes Load to return an error if any specified files |
| 46 | +// (env file or JSON file) do not exist. |
| 47 | +func WithRequired() Option { |
| 48 | + return func(o *options) { |
| 49 | + o.required = true |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Load populates dst from environment variables, optional .env files, and |
| 54 | +// optional JSON config files, then validates the result using struct tags. |
| 55 | +// |
| 56 | +// dst must be a non-nil pointer to a struct. |
| 57 | +// |
| 58 | +// Sources are applied in priority order (high to low): |
| 59 | +// 1. Environment variables |
| 60 | +// 2. .env file values (do not override existing env vars) |
| 61 | +// 3. JSON file values |
| 62 | +// 4. default:"..." struct tags |
| 63 | +func Load(dst any, opts ...Option) error { |
| 64 | + o := &options{} |
| 65 | + for _, opt := range opts { |
| 66 | + opt(o) |
| 67 | + } |
| 68 | + |
| 69 | + // Load JSON file (base layer). |
| 70 | + var jsonValues map[string]string |
| 71 | + if o.jsonFile != "" { |
| 72 | + var err error |
| 73 | + jsonValues, err = loadJSONFile(o.jsonFile, o.required) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Load .env file into a map (does not modify process environment). |
| 80 | + var envFileValues map[string]string |
| 81 | + if o.envFile != "" { |
| 82 | + var err error |
| 83 | + envFileValues, err = loadEnvFile(o.envFile, o.required) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Resolve all values into the struct. |
| 90 | + if err := resolve(dst, o.prefix, envFileValues, jsonValues); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + // Validate using request package tag validators. |
| 95 | + if err := request.ValidateStruct(dst); err != nil { |
| 96 | + // Wrap the validation error with config context. |
| 97 | + var apiErr *errors.Error |
| 98 | + if stderrors.As(err, &apiErr) { |
| 99 | + return errors.Validation("config: validation failed", apiErr.Fields) |
| 100 | + } |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +// MustLoad calls Load and panics if an error occurs. |
| 108 | +// It is intended for use in main() or init() functions. |
| 109 | +func MustLoad(dst any, opts ...Option) { |
| 110 | + if err := Load(dst, opts...); err != nil { |
| 111 | + panic(fmt.Sprintf("config: %v", err)) |
| 112 | + } |
| 113 | +} |
0 commit comments