refactor(pkg): refactor Plugin
to make it easier to test
Writing the plugin's output string and exiting the program will no longer take place in the plugin status itself. It will only be done in the main program.
This commit is contained in:
parent
b3aa7dfcad
commit
5432360f0e
5 changed files with 30 additions and 19 deletions
pkg
|
@ -5,7 +5,6 @@ package plugin // import nocternity.net/gomonop/pkg/perfdata
|
|||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"nocternity.net/gomonop/pkg/perfdata"
|
||||
|
@ -70,10 +69,9 @@ func (p *Plugin) AddPerfData(pd *perfdata.PerfData) {
|
|||
p.perfData[pd.Label] = pd
|
||||
}
|
||||
|
||||
// Done generates the plugin's text output from its name, status, text data
|
||||
// and performance data, before exiting with the code corresponding to the
|
||||
// status.
|
||||
func (p *Plugin) Done() {
|
||||
// String generates the plugin's text output from its name, status, text data
|
||||
// and performance data.
|
||||
func (p *Plugin) String() string {
|
||||
var strBuilder strings.Builder
|
||||
strBuilder.WriteString(p.name)
|
||||
strBuilder.WriteString(" ")
|
||||
|
@ -99,6 +97,10 @@ func (p *Plugin) Done() {
|
|||
strBuilder.WriteString(em.Value.(string))
|
||||
}
|
||||
}
|
||||
fmt.Println(strBuilder.String())
|
||||
os.Exit(int(p.status))
|
||||
return strBuilder.String()
|
||||
}
|
||||
|
||||
// ExitCode returns the plugin's exit code.
|
||||
func (p *Plugin) ExitCode() int {
|
||||
return int(p.status)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package program // import nocternity.net/gomonop/pkg/program
|
||||
|
||||
import "nocternity.net/gomonop/pkg/plugin"
|
||||
|
||||
type Program interface {
|
||||
Output() *plugin.Plugin
|
||||
CheckArguments() bool
|
||||
RunCheck()
|
||||
Done()
|
||||
}
|
||||
|
||||
type Builder func() Program
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue