re-arrange a file pieces.

This commit is contained in:
davidnewhall2 2019-11-28 03:54:53 -08:00
parent 3444db73d4
commit 632936d772
2 changed files with 37 additions and 34 deletions

View File

@ -113,7 +113,7 @@ func (c *Config) ParseENV() error {
t := reflect.TypeOf(Config{}) // Get tag names from the Config struct.
// Loop each Config struct member; get reflect tag & env var value; update config.
for i := 0; i < t.NumField(); i++ {
tag := strings.ToUpper(t.Field(i).Tag.Get("json")) // Get the ENV variable name from "env" struct tag
tag := strings.ToUpper(t.Field(i).Tag.Get("json")) // Get the ENV variable name from capitalized "json" struct tag
env := os.Getenv(ENVConfigPrefix + tag) // Then pull value from OS.
if tag == "" || env == "" {
continue // Skip if either are empty.

View File

@ -39,37 +39,47 @@ func New() *UnifiPoller {
}
// Start begins the application from a CLI.
// Parses flags, parses config and executes Run().
// Parses cli flags, parses config file, parses env vars, sets up logging, then:
// - dumps a json payload OR - authenticates unifi controller and executes Run().
func (u *UnifiPoller) Start() error {
log.SetFlags(log.LstdFlags)
up := New()
up.Flag.Parse(os.Args[1:])
u.Flag.Parse(os.Args[1:])
if up.Flag.ShowVer {
if u.Flag.ShowVer {
fmt.Printf("%s v%s\n", appName, Version)
return nil // don't run anything else w/ version request.
}
if up.Flag.DumpJSON == "" { // do not print this when dumping JSON.
up.Logf("Loading Configuration File: %s", up.Flag.ConfigFile)
if u.Flag.DumpJSON == "" { // do not print this when dumping JSON.
u.Logf("Loading Configuration File: %s", u.Flag.ConfigFile)
}
// Parse config file.
if err := up.Config.ParseFile(up.Flag.ConfigFile); err != nil {
up.Flag.Usage()
if err := u.Config.ParseFile(u.Flag.ConfigFile); err != nil {
u.Flag.Usage()
return err
}
// Update Config with ENV variable overrides.
if err := up.Config.ParseENV(); err != nil {
if err := u.Config.ParseENV(); err != nil {
return err
}
if up.Flag.DumpJSON != "" {
return up.DumpJSONPayload()
if u.Flag.DumpJSON != "" {
return u.DumpJSONPayload()
}
return up.Run()
if u.Config.Debug {
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
u.LogDebugf("Debug Logging Enabled")
}
log.Printf("[INFO] UniFi Poller v%v Starting Up! PID: %d", Version, os.Getpid())
if err := u.GetUnifi(); err != nil {
return err
}
return u.Run()
}
// Parse turns CLI arguments into data structures. Called by Start() on startup.
@ -87,24 +97,25 @@ func (f *Flag) Parse(args []string) {
_ = f.FlagSet.Parse(args) // pflag.ExitOnError means this will never return error.
}
// Run invokes all the application logic and routines.
func (u *UnifiPoller) Run() (err error) {
if u.Config.Debug {
log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate)
u.LogDebugf("Debug Logging Enabled")
}
log.Printf("[INFO] UniFi Poller v%v Starting Up! PID: %d", Version, os.Getpid())
if err = u.GetUnifi(); err != nil {
return err
}
// Run picks a mode and executes the associated functions. This will do one of three things:
// 1. Start the collector routine that polls unifi and reports to influx on an interval. (default)
// 2. Run the collector one time and report the metrics to influxdb. (lambda)
// 3. Start a web server and wait for Prometheus to poll the application for metrics.
func (u *UnifiPoller) Run() error {
u.Logf("Polling UniFi Controller at %s v%s as user %s. Sites: %v",
u.Config.UnifiBase, u.Unifi.ServerVersion, u.Config.UnifiUser, u.Config.Sites)
switch strings.ToLower(u.Config.Mode) {
default:
if err := u.GetInfluxDB(); err != nil {
return err
}
u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.Config.InfluxURL, u.Config.InfluxUser)
u.Config.Mode = "influx poller"
return u.PollController()
case "influxlambda", "lambdainflux", "lambda_influx", "influx_lambda":
if err = u.GetInfluxDB(); err != nil {
if err := u.GetInfluxDB(); err != nil {
return err
}
u.Logf("Logging Measurements to InfluxDB at %s as user %s one time (lambda mode)",
@ -122,14 +133,6 @@ func (u *UnifiPoller) Run() (err error) {
ReportErrors: true, // XXX: Does this need to be configurable?
}))
return http.ListenAndServe(u.Config.HTTPListen, nil)
default:
if err = u.GetInfluxDB(); err != nil {
return err
}
u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.Config.InfluxURL, u.Config.InfluxUser)
u.Config.Mode = "influx poller"
return u.PollController()
}
}