-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathconfig.go
29 lines (26 loc) · 1 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package serverutils
import (
"flag"
"dario.cat/mergo"
"github.com/grafana/dskit/server"
)
// MergeWithDefaults applies server.Config defaults to a given and different server.Config.
func MergeWithDefaults(config server.Config) (server.Config, error) {
// Bit of a chicken and egg problem trying to register the defaults and apply overrides from the loaded config.
// First create an empty config and set defaults.
mergee := server.Config{}
mergee.RegisterFlags(flag.NewFlagSet("empty", flag.ContinueOnError))
// Then apply any config values loaded as overrides to the defaults.
if err := mergo.Merge(&mergee, config, mergo.WithOverride); err != nil {
return server.Config{}, err
}
// The merge won't overwrite with a zero value but in the case of ports 0 value
// indicates the desire for a random port so reset these to zero if the incoming config val is 0
if config.HTTPListenPort == 0 {
mergee.HTTPListenPort = 0
}
if config.GRPCListenPort == 0 {
mergee.GRPCListenPort = 0
}
return mergee, nil
}