From 7ef04674dce0175e3caaf97791aba7bacf21138b Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 18 Jan 2022 20:21:43 +0800 Subject: [PATCH] change: clean code --- plugin/common/init.go | 2 +- runner.go | 45 +++++++++++++++++++------------------------ 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/plugin/common/init.go b/plugin/common/init.go index 399d38da..aa27856e 100644 --- a/plugin/common/init.go +++ b/plugin/common/init.go @@ -148,7 +148,7 @@ func Init(path string) (Plugin, error) { } var plugin Plugin - // priority: hashicorp plugin > go plugin > builtin functions + // priority: hashicorp plugin > go plugin // locate hashicorp plugin file pluginPath, err := locateFile(path, hashicorpGoPluginFile) if err == nil { diff --git a/runner.go b/runner.go index a28f28a2..8e687b48 100644 --- a/runner.go +++ b/runner.go @@ -193,41 +193,36 @@ func (r *caseRunner) run() error { } func initPlugin(path string) (plugin common.Plugin, err error) { - defer func() { - if plugin == nil { - return - } - - var pluginType string - if _, ok := plugin.(*common.GoPlugin); ok { - pluginType = "go" - } else { - pluginType = "hashicorp" - } - - // report event for initializing plugin - event := ga.EventTracking{ - Category: "InitPlugin", - Action: fmt.Sprintf("Init %s plugin", pluginType), - } - if err != nil { - event.Value = 1 // failed - } - go ga.SendEvent(event) - }() plugin, err = common.Init(path) + if plugin == nil { + return + } // catch Interrupt and SIGTERM signals to ensure plugin quitted c := make(chan os.Signal) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c - if plugin != nil { - plugin.Quit() - } + plugin.Quit() os.Exit(1) }() + // report event for initializing plugin + var pluginType string + if _, ok := plugin.(*common.GoPlugin); ok { + pluginType = "go" + } else { + pluginType = "hashicorp" + } + event := ga.EventTracking{ + Category: "InitPlugin", + Action: fmt.Sprintf("Init %s plugin", pluginType), + } + if err != nil { + event.Value = 1 // failed + } + go ga.SendEvent(event) + return }