2017-07-01 21:03:36 +02:00
|
|
|
package config
|
|
|
|
|
2017-07-17 14:00:13 +02:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2017-07-01 21:03:36 +02:00
|
|
|
// Flags are applied after the configuration file is loaded.
|
|
|
|
// They are pointers to represent optional types, to tell whether they have been set.
|
|
|
|
type Flags struct {
|
2017-07-17 14:00:13 +02:00
|
|
|
// these are pointers so we can tell whether they've been set, and override
|
|
|
|
// the config file only if
|
2017-07-23 17:25:17 +02:00
|
|
|
Destination, Host *string
|
|
|
|
Drafts, Future, Unpublished, Verbose *bool
|
|
|
|
Port *int
|
2017-07-17 14:00:13 +02:00
|
|
|
// these aren't in the config file, so they can be treated more conventionally
|
2017-07-23 17:25:17 +02:00
|
|
|
DryRun, ForcePolling, Watch bool
|
2017-07-01 21:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyFlags overwrites the configuration with values from flags.
|
2017-07-17 14:00:13 +02:00
|
|
|
func (c *Config) ApplyFlags(f Flags) {
|
|
|
|
rs, rd := reflect.ValueOf(f), reflect.ValueOf(c).Elem()
|
|
|
|
rt := rs.Type()
|
|
|
|
for i, n := 0, rs.NumField(); i < n; i++ {
|
|
|
|
field := rt.Field(i)
|
|
|
|
val := rs.Field(i)
|
|
|
|
if val.Kind() == reflect.Ptr {
|
|
|
|
if val.IsNil() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
val = val.Elem()
|
|
|
|
}
|
|
|
|
rd.FieldByName(field.Name).Set(val)
|
2017-07-15 00:40:09 +02:00
|
|
|
}
|
2017-07-01 21:03:36 +02:00
|
|
|
}
|