Allow app to import example files without renaming.

This commit is contained in:
David Newhall II 2019-06-21 18:48:20 -07:00
parent 9a853b8141
commit c930f328db
2 changed files with 12 additions and 13 deletions

View File

@ -40,7 +40,7 @@ type UnifiPoller struct {
*Config
}
// Metrics contains all the data from the controller.
// Metrics contains all the data from the controller and an influx endpoint to send them to.
type Metrics struct {
unifi.Sites
unifi.Clients
@ -51,7 +51,7 @@ type Metrics struct {
// Config represents the data needed to poll a controller and report to influxdb.
type Config struct {
MaxErrors int `json:"max_errors,_omitempty" toml:"max_errors,_omitempty" xml:"max_errors" yaml:"max_errors"`
Interval Dur `json:"interval,_omitempty" toml:"interval,_omitempty" xml:"interval" yaml:"interval"`
Interval Duration `json:"interval,_omitempty" toml:"interval,_omitempty" xml:"interval" yaml:"interval"`
Debug bool `json:"debug" toml:"debug" xml:"debug" yaml:"debug"`
Quiet bool `json:"quiet,_omitempty" toml:"quiet,_omitempty" xml:"quiet" yaml:"quiet"`
VerifySSL bool `json:"verify_ssl" toml:"verify_ssl" xml:"verify_ssl" yaml:"verify_ssl"`
@ -65,11 +65,11 @@ type Config struct {
Sites []string `json:"sites,_omitempty" toml:"sites,_omitempty" xml:"sites" yaml:"sites"`
}
// Dur is used to UnmarshalTOML into a time.Duration value.
type Dur struct{ time.Duration }
// Duration is used to UnmarshalTOML into a time.Duration value.
type Duration struct{ time.Duration }
// UnmarshalText parses a duration type from a config file.
func (d *Dur) UnmarshalText(data []byte) (err error) {
func (d *Duration) UnmarshalText(data []byte) (err error) {
d.Duration, err = time.ParseDuration(string(data))
return
}

View File

@ -25,14 +25,14 @@ func (u *UnifiPoller) ParseFlags(args []string) {
u.Flag.PrintDefaults()
}
u.Flag.StringVarP(&u.DumpJSON, "dumpjson", "j", "",
"This debug option prints the json payload for a device and exits.")
"This debug option prints a json payload and exits. See man page for more.")
u.Flag.StringVarP(&u.ConfigFile, "config", "c", defaultConfFile, "Poller Config File (TOML Format)")
u.Flag.BoolVarP(&u.ShowVer, "version", "v", false, "Print the version and exit")
_ = u.Flag.Parse(args)
}
// GetConfig parses and returns our configuration data.
func (u *UnifiPoller) GetConfig() (err error) {
func (u *UnifiPoller) GetConfig() error {
// Preload our defaults.
u.Config = &Config{
InfluxURL: defaultInfxURL,
@ -42,20 +42,19 @@ func (u *UnifiPoller) GetConfig() (err error) {
UnifiUser: defaultUnifUser,
UnifiPass: os.Getenv("UNIFI_PASSWORD"),
UnifiBase: defaultUnifURL,
Interval: Dur{defaultInterval},
Interval: Duration{defaultInterval},
Sites: []string{"default"},
Quiet: u.DumpJSON != "",
}
u.Logf("Loading Configuration File: %s", u.ConfigFile)
var buf []byte
switch buf, err = ioutil.ReadFile(u.ConfigFile); {
switch buf, err := ioutil.ReadFile(u.ConfigFile); {
case err != nil:
return err
case strings.HasSuffix(u.ConfigFile, ".json"):
case strings.Contains(u.ConfigFile, ".json"):
return json.Unmarshal(buf, u.Config)
case strings.HasSuffix(u.ConfigFile, ".xml"):
case strings.Contains(u.ConfigFile, ".xml"):
return xml.Unmarshal(buf, u.Config)
case strings.HasSuffix(u.ConfigFile, ".yaml"):
case strings.Contains(u.ConfigFile, ".yaml"):
return yaml.Unmarshal(buf, u.Config)
default:
return toml.Unmarshal(buf, u.Config)