@@ -143,6 +143,11 @@ type Options struct {
143
143
// CheckConfig configuration file syntax test was successful and exit.
144
144
CheckConfig bool `json:"-"`
145
145
146
+ // private fields, used to know if bool options are explicitly
147
+ // defined in config and/or command line params.
148
+ inConfig map [string ]bool
149
+ inCmdLine map [string ]bool
150
+
146
151
// private fields, used for testing
147
152
gatewaysSolicitDelay time.Duration
148
153
}
@@ -340,10 +345,13 @@ func (o *Options) ProcessConfigFile(configFile string) error {
340
345
o .Host = v .(string )
341
346
case "debug" :
342
347
o .Debug = v .(bool )
348
+ trackExplicitVal (o , & o .inConfig , "Debug" , o .Debug )
343
349
case "trace" :
344
350
o .Trace = v .(bool )
351
+ trackExplicitVal (o , & o .inConfig , "Trace" , o .Trace )
345
352
case "logtime" :
346
353
o .Logtime = v .(bool )
354
+ trackExplicitVal (o , & o .inConfig , "Logtime" , o .Logtime )
347
355
case "accounts" :
348
356
err := parseAccounts (tk , o , & errors , & warnings )
349
357
if err != nil {
@@ -424,6 +432,7 @@ func (o *Options) ProcessConfigFile(configFile string) error {
424
432
o .LogFile = v .(string )
425
433
case "syslog" :
426
434
o .Syslog = v .(bool )
435
+ trackExplicitVal (o , & o .inConfig , "Syslog" , o .Syslog )
427
436
case "remote_syslog" :
428
437
o .RemoteSyslog = v .(string )
429
438
case "pidfile" , "pid_file" :
@@ -630,6 +639,15 @@ func (o *Options) ProcessConfigFile(configFile string) error {
630
639
return nil
631
640
}
632
641
642
+ func trackExplicitVal (opts * Options , pm * map [string ]bool , name string , val bool ) {
643
+ m := * pm
644
+ if m == nil {
645
+ m = make (map [string ]bool )
646
+ * pm = m
647
+ }
648
+ m [name ] = val
649
+ }
650
+
633
651
// hostPort is simple struct to hold parsed listen/addr strings.
634
652
type hostPort struct {
635
653
host string
@@ -732,6 +750,7 @@ func parseCluster(v interface{}, opts *Options, errors *[]error, warnings *[]err
732
750
opts .Cluster .Advertise = mv .(string )
733
751
case "no_advertise" :
734
752
opts .Cluster .NoAdvertise = mv .(bool )
753
+ trackExplicitVal (opts , & opts .inConfig , "Cluster.NoAdvertise" , opts .Cluster .NoAdvertise )
735
754
case "connect_retries" :
736
755
opts .Cluster .ConnectRetries = int (mv .(int64 ))
737
756
case "permissions" :
@@ -2190,12 +2209,10 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
2190
2209
showTLSHelp bool
2191
2210
signal string
2192
2211
configFile string
2212
+ dbgAndTrace bool
2193
2213
err error
2194
2214
)
2195
2215
2196
- // IMPORTANT: If you add boolean flags and use a 'true' as the default,
2197
- // check what needs to be done for FlagSnapshot down below.
2198
- // See logtimeIsExplicitlySet for reference.
2199
2216
fs .BoolVar (& showHelp , "h" , false , "Show this message." )
2200
2217
fs .BoolVar (& showHelp , "help" , false , "Show this message." )
2201
2218
fs .IntVar (& opts .Port , "port" , 0 , "Port to listen on." )
@@ -2208,7 +2225,7 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
2208
2225
fs .BoolVar (& opts .Debug , "debug" , false , "Enable Debug logging." )
2209
2226
fs .BoolVar (& opts .Trace , "V" , false , "Enable Trace logging." )
2210
2227
fs .BoolVar (& opts .Trace , "trace" , false , "Enable Trace logging." )
2211
- fs .Bool ( "DV" , false , "Enable Debug and Trace logging." )
2228
+ fs .BoolVar ( & dbgAndTrace , "DV" , false , "Enable Debug and Trace logging." )
2212
2229
fs .BoolVar (& opts .Logtime , "T" , true , "Timestamp log entries." )
2213
2230
fs .BoolVar (& opts .Logtime , "logtime" , true , "Timestamp log entries." )
2214
2231
fs .StringVar (& opts .Username , "user" , "" , "Username required for connection." )
@@ -2247,7 +2264,6 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
2247
2264
fs .StringVar (& opts .TLSCert , "tlscert" , "" , "Server certificate file." )
2248
2265
fs .StringVar (& opts .TLSKey , "tlskey" , "" , "Private key for server certificate." )
2249
2266
fs .StringVar (& opts .TLSCaCert , "tlscacert" , "" , "Client certificate CA for verification." )
2250
- // --- See comment on top of flags definition before adding a flag ---
2251
2267
2252
2268
// The flags definition above set "default" values to some of the options.
2253
2269
// Calling Parse() here will override the default options with any value
@@ -2292,19 +2308,32 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
2292
2308
// Snapshot flag options.
2293
2309
FlagSnapshot = opts .Clone ()
2294
2310
2295
- // Before parsing the config file, we need to know if boolean
2296
- // flags are explicitly set or not, and for those which default
2297
- // to `true`, we need to reset them otherwise config reload would
2298
- // incorrectly override what is in the config file.
2299
- logtimeIsExplicitlySet := false
2311
+ // Keep track of the boolean flags that were explicitly set with their value.
2300
2312
fs .Visit (func (f * flag.Flag ) {
2301
- if f .Name == "logtime" {
2302
- logtimeIsExplicitlySet = true
2313
+ switch f .Name {
2314
+ case "DV" :
2315
+ trackExplicitVal (FlagSnapshot , & FlagSnapshot .inCmdLine , "Debug" , dbgAndTrace )
2316
+ trackExplicitVal (FlagSnapshot , & FlagSnapshot .inCmdLine , "Trace" , dbgAndTrace )
2317
+ case "D" :
2318
+ fallthrough
2319
+ case "debug" :
2320
+ trackExplicitVal (FlagSnapshot , & FlagSnapshot .inCmdLine , "Debug" , FlagSnapshot .Debug )
2321
+ case "V" :
2322
+ fallthrough
2323
+ case "trace" :
2324
+ trackExplicitVal (FlagSnapshot , & FlagSnapshot .inCmdLine , "Trace" , FlagSnapshot .Trace )
2325
+ case "T" :
2326
+ fallthrough
2327
+ case "logtime" :
2328
+ trackExplicitVal (FlagSnapshot , & FlagSnapshot .inCmdLine , "Logtime" , FlagSnapshot .Logtime )
2329
+ case "s" :
2330
+ fallthrough
2331
+ case "syslog" :
2332
+ trackExplicitVal (FlagSnapshot , & FlagSnapshot .inCmdLine , "Syslog" , FlagSnapshot .Syslog )
2333
+ case "no_advertise" :
2334
+ trackExplicitVal (FlagSnapshot , & FlagSnapshot .inCmdLine , "Cluster.NoAdvertise" , FlagSnapshot .Cluster .NoAdvertise )
2303
2335
}
2304
2336
})
2305
- if ! logtimeIsExplicitlySet {
2306
- FlagSnapshot .Logtime = false
2307
- }
2308
2337
2309
2338
// Process signal control.
2310
2339
if signal != "" {
@@ -2371,8 +2400,7 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp,
2371
2400
switch f .Name {
2372
2401
case "DV" :
2373
2402
// Check value to support -DV=false
2374
- boolValue , _ := strconv .ParseBool (f .Value .String ())
2375
- opts .Trace , opts .Debug = boolValue , boolValue
2403
+ opts .Trace , opts .Debug = dbgAndTrace , dbgAndTrace
2376
2404
case "cluster" , "cluster_listen" :
2377
2405
// Override cluster config if explicitly set via flags.
2378
2406
flagErr = overrideCluster (opts )
0 commit comments