cmd/revid-cli: using closure for readPin func so that we don't have to have global revid

This commit is contained in:
saxon 2019-02-12 11:32:02 +10:30
parent b9c53791d8
commit 69029889fe
1 changed files with 17 additions and 21 deletions

View File

@ -75,8 +75,6 @@ var canProfile = true
// The logger that will be used throughout // The logger that will be used throughout
var log *logger.Logger var log *logger.Logger
var rv *revid.Revid
const ( const (
metaPreambleKey = "copyright" metaPreambleKey = "copyright"
metaPreambleData = "ausocean.org/license/content2019" metaPreambleData = "ausocean.org/license/content2019"
@ -89,9 +87,8 @@ func main() {
runDurationPtr := flag.Duration("runDuration", defaultRunDuration, "How long do you want revid to run for?") runDurationPtr := flag.Duration("runDuration", defaultRunDuration, "How long do you want revid to run for?")
cfg := handleFlags() cfg := handleFlags()
var err error
if !*useNetsender { if !*useNetsender {
rv, err = revid.New(cfg, nil) rv, err := revid.New(cfg, nil)
if err != nil { if err != nil {
cfg.Logger.Log(logger.Fatal, pkg+"failed to initialiase revid", "error", err.Error()) cfg.Logger.Log(logger.Fatal, pkg+"failed to initialiase revid", "error", err.Error())
} }
@ -268,21 +265,34 @@ func run(cfg revid.Config) error {
log.Log(logger.Info, pkg+"running in NetSender mode") log.Log(logger.Info, pkg+"running in NetSender mode")
var vars map[string]string var vars map[string]string
var rv *revid.Revid
// initialize NetSender and use NetSender's logger // initialize NetSender and use NetSender's logger
readPin := func(pin *netsender.Pin) error {
switch {
case pin.Name == "X23":
pin.Value = rv.Bitrate()
case pin.Name[0] == 'X':
return sds.ReadSystem(pin)
default:
pin.Value = -1
}
return nil // Return error only if we want NetSender to generate an error
}
ns, err := netsender.New(log, nil, readPin, nil) ns, err := netsender.New(log, nil, readPin, nil)
if err != nil { if err != nil {
return err return err
} }
vars, _ = ns.Vars()
vs := ns.VarSum()
rv, err = revid.New(cfg, ns) rv, err = revid.New(cfg, ns)
if err != nil { if err != nil {
log.Log(logger.Fatal, pkg+"could not initialise revid", "error", err.Error()) log.Log(logger.Fatal, pkg+"could not initialise revid", "error", err.Error())
} }
vars, _ = ns.Vars()
vs := ns.VarSum()
// Update revid to get latest config settings from netreceiver. // Update revid to get latest config settings from netreceiver.
err = rv.Update(vars) err = rv.Update(vars)
if err != nil { if err != nil {
@ -357,20 +367,6 @@ func run(cfg revid.Config) error {
} }
} }
// readPin is callback func for netsender so that pins can be appropriately set
// for this circumstance.
func readPin(pin *netsender.Pin) error {
switch {
case pin.Name == "X23":
pin.Value = rv.Bitrate()
case pin.Name[0] == 'X':
return sds.ReadSystem(pin)
default:
pin.Value = -1
}
return nil // Return error only if we want NetSender to generate an error
}
// flagStrings implements an appending string set flag. // flagStrings implements an appending string set flag.
type flagStrings []string type flagStrings []string