MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / Initialize

Function Initialize

pkg/config/config.go:151–185  ·  view source on GitHub ↗

Initialize a new config manager with the given name and defaults. The defaults should be a struct with fields that define the possible config flags by setting the struct tags. Possible struct tags are: - `name:" "`: Defines the name of the config flag, in the environment, on the command line

(name, envPrefix string, defaults any, opts ...Option)

Source from the content-addressed store, hash-verified

149// by the name of this struct, unless it is `name:",squash"` in which case
150// the names are merged into the parent struct.
151func Initialize(name, envPrefix string, defaults any, opts ...Option) *Manager {
152 m := &Manager{
153 name: name,
154 envPrefix: envPrefix,
155 viper: viper.New(),
156 flags: pflag.NewFlagSet(name, pflag.ExitOnError),
157 replacer: strings.NewReplacer(),
158 defaults: defaults,
159 }
160
161 m.viper.SetTypeByDefaultValue(true)
162 m.viper.SetConfigName(name)
163 m.viper.SetConfigType("yml")
164 m.viper.AllowEmptyEnv(true)
165 m.viper.SetEnvPrefix(envPrefix)
166 m.viper.AutomaticEnv()
167 m.viper.AddConfigPath(".")
168
169 m.flags.SetInterspersed(true)
170
171 if defaults != nil {
172 m.setDefaults("", m.flags, defaults)
173 }
174
175 for _, opt := range opts {
176 opt(m)
177 }
178
179 err := m.viper.BindPFlags(m.flags)
180 if err != nil {
181 panic(err)
182 }
183
184 return m
185}
186
187// WithConfig returns a new flagset with has the flags of the Manager as well as the additional flags defined
188// from the defaults passed along.

Callers 3

TestUnmarshalFunction · 0.70
TestUnmarshalKeyFunction · 0.70
InitializeWithDefaultsFunction · 0.70

Calls 2

setDefaultsMethod · 0.95
NewMethod · 0.65

Tested by 2

TestUnmarshalFunction · 0.56
TestUnmarshalKeyFunction · 0.56