change: clean code

This commit is contained in:
debugtalk
2022-01-18 20:21:43 +08:00
parent c222b22132
commit b1e405612e
2 changed files with 21 additions and 26 deletions
+1 -1
View File
@@ -148,7 +148,7 @@ func Init(path string) (Plugin, error) {
} }
var plugin Plugin var plugin Plugin
// priority: hashicorp plugin > go plugin > builtin functions // priority: hashicorp plugin > go plugin
// locate hashicorp plugin file // locate hashicorp plugin file
pluginPath, err := locateFile(path, hashicorpGoPluginFile) pluginPath, err := locateFile(path, hashicorpGoPluginFile)
if err == nil { if err == nil {
+11 -16
View File
@@ -193,19 +193,27 @@ func (r *caseRunner) run() error {
} }
func initPlugin(path string) (plugin common.Plugin, err error) { func initPlugin(path string) (plugin common.Plugin, err error) {
defer func() { plugin, err = common.Init(path)
if plugin == nil { if plugin == nil {
return 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
plugin.Quit()
os.Exit(1)
}()
// report event for initializing plugin
var pluginType string var pluginType string
if _, ok := plugin.(*common.GoPlugin); ok { if _, ok := plugin.(*common.GoPlugin); ok {
pluginType = "go" pluginType = "go"
} else { } else {
pluginType = "hashicorp" pluginType = "hashicorp"
} }
// report event for initializing plugin
event := ga.EventTracking{ event := ga.EventTracking{
Category: "InitPlugin", Category: "InitPlugin",
Action: fmt.Sprintf("Init %s plugin", pluginType), Action: fmt.Sprintf("Init %s plugin", pluginType),
@@ -214,19 +222,6 @@ func initPlugin(path string) (plugin common.Plugin, err error) {
event.Value = 1 // failed event.Value = 1 // failed
} }
go ga.SendEvent(event) go ga.SendEvent(event)
}()
plugin, err = common.Init(path)
// 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()
}
os.Exit(1)
}()
return return
} }