From 86949813fd8943d282045a3cb09d36838aa7b278 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sun, 9 Dec 2018 16:01:47 +1030 Subject: [PATCH] cmd/revid-cli: remove global revid var --- cmd/revid-cli/main.go | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/cmd/revid-cli/main.go b/cmd/revid-cli/main.go index 3b8be214..f50fd367 100644 --- a/cmd/revid-cli/main.go +++ b/cmd/revid-cli/main.go @@ -63,9 +63,6 @@ var canProfile = true // The logger that will be used throughout var logger *smartlogger.Logger -// Globals -var rv *revid.Revid - func main() { useNetsender := flag.Bool("NetSender", false, "Are we checking vars through netsender?") runDurationPtr := flag.Duration("runDuration", defaultRunDuration, "How long do you want revid to run for?") @@ -74,13 +71,16 @@ func main() { if !*useNetsender { // run revid for the specified duration - startRevid(nil, cfg) + rv, _, err := startRevid(nil, cfg) + if err != nil { + cfg.Logger.Log(smartlogger.Fatal, pkg+"failed to start revid", err.Error()) + } time.Sleep(*runDurationPtr) - stopRevid() + stopRevid(rv) return } - err := run(cfg) + err := run(nil, cfg) if err != nil { logger.Log(smartlogger.Fatal, pkg+"failed to run revid", "error", err.Error()) os.Exit(1) @@ -281,7 +281,7 @@ func handleFlags() revid.Config { } // initialize then run the main NetSender client -func run(cfg revid.Config) error { +func run(rv *revid.Revid, cfg revid.Config) error { // initialize NetSender and use NetSender's logger //config.Logger = netsender.Logger() logger.Log(smartlogger.Info, pkg+"running in NetSender mode") @@ -298,14 +298,14 @@ func run(cfg revid.Config) error { paused = true } if !paused { - cfg, err = updateRevid(&ns, cfg, vars, false) + rv, cfg, err = updateRevid(&ns, rv, cfg, vars, false) if err != nil { return err } } for { - if err := send(&ns); err != nil { + if err := send(&ns, rv); err != nil { logger.Log(smartlogger.Error, pkg+"polling failed", "error", err.Error()) time.Sleep(netSendRetryTime) continue @@ -323,11 +323,11 @@ func run(cfg revid.Config) error { if vars["mode"] == "Paused" { if !paused { logger.Log(smartlogger.Info, pkg+"pausing revid") - stopRevid() + stopRevid(rv) paused = true } } else { - cfg, err = updateRevid(&ns, cfg, vars, !paused) + rv, cfg, err = updateRevid(&ns, rv, cfg, vars, !paused) if err != nil { return err } @@ -343,7 +343,7 @@ func run(cfg revid.Config) error { // send implements our main NetSender client and handles NetReceiver configuration // (as distinct from httpSender which just sends video data). -func send(ns *netsender.Sender) error { +func send(ns *netsender.Sender, rv *revid.Revid) error { // populate input values, if any inputs := netsender.MakePins(ns.Param("ip"), "X") if rv != nil { @@ -365,17 +365,16 @@ func send(ns *netsender.Sender) error { } // wrappers for stopping and starting revid -func startRevid(ns *netsender.Sender, cfg revid.Config) (revid.Config, error) { - var err error - rv, err = revid.New(cfg, ns) +func startRevid(ns *netsender.Sender, cfg revid.Config) (*revid.Revid, revid.Config, error) { + rv, err := revid.New(cfg, ns) if err != nil { - return cfg, err + return nil, cfg, err } rv.Start() - return cfg, nil + return rv, cfg, nil } -func stopRevid() { +func stopRevid(rv *revid.Revid) { rv.Stop() // FIXME(kortschak): Is this waiting on completion of work? @@ -383,9 +382,9 @@ func stopRevid() { time.Sleep(revidStopTime) } -func updateRevid(ns *netsender.Sender, cfg revid.Config, vars map[string]string, stop bool) (revid.Config, error) { +func updateRevid(ns *netsender.Sender, rv *revid.Revid, cfg revid.Config, vars map[string]string, stop bool) (*revid.Revid, revid.Config, error) { if stop { - stopRevid() + stopRevid(rv) } //look through the vars and update revid where needed