slack-bot/pkg/check/command.go

54 lines
1.2 KiB
Go

package check
import (
"fmt"
"go/slack-bot/pkg/notify"
"os/exec"
)
//Checker representation of check set
type Checker interface {
Check(f func(stdout string) []notify.Message) []notify.Message
}
//AdvancedChecker like Checker except contain Parsing step
type AdvancedChecker interface {
Checker
Parse(stdout string) interface{}
}
//CliChecker implement checker with cli command
type CliChecker struct {
alertingMessage string
command []string
}
//NewCliChecker build new cli checker
func NewCliChecker(alertingMessage string, command ...string) Checker {
return &CliChecker{
alertingMessage: alertingMessage,
command: command,
}
}
//Check function use to run cli call to check some parameter
func (cc *CliChecker) Check(f func(stdout string) []notify.Message) []notify.Message {
message := []notify.Message{}
out, err := exec.Command(cc.command[0], cc.command[1:]...).CombinedOutput()
if err != nil {
// command fail notify with error
message = append(message,
notify.Message{
Content: fmt.Sprint(cc.alertingMessage, " : ", string(out), err),
ShouldNotify: true,
},
)
} else {
// command success check if condition required notify
message = f(string(out))
}
return message
}