gomonop/main.go
Emmanuel BENOîT c46c9d76d9
All checks were successful
Run tests and linters / test (push) Successful in 50s
Run tests and linters / build (push) Successful in 48s
Run tests and linters / lint (push) Successful in 1m27s
feat: add the check_output_matches plugin (#5)
This PR adds the `check_output_matches` plugin, which can be used to count regexp or substring matches from either text files or command outputs and determine the final status based on the amount of matches that were found.

Reviewed-on: #5
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-20 22:57:10 +02:00

76 lines
1.5 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"nocternity.net/gomonop/cmd/matches"
"nocternity.net/gomonop/cmd/sslcert"
"nocternity.net/gomonop/cmd/zoneserial"
"nocternity.net/gomonop/pkg/plugin"
"nocternity.net/gomonop/pkg/status"
"nocternity.net/gomonop/pkg/version"
)
var (
plugins map[string]plugin.Builder = map[string]plugin.Builder{
"check_output_matches": matches.NewPlugin,
"check_ssl_certificate": sslcert.NewProgram,
"check_zone_serial": zoneserial.NewProgram,
}
)
func getPlugin() plugin.Plugin {
ownName := filepath.Base(os.Args[0])
if builder, ok := plugins[ownName]; ok {
return builder()
}
if len(os.Args) < 2 {
fmt.Printf("Syntax: %s <plugin> [arguments]\n", ownName)
fmt.Printf(" %s --plugins|-p\n", ownName)
fmt.Printf(" %s --version|-v\n", ownName)
os.Exit(1)
}
switch os.Args[1] {
case "--plugins", "-p":
for name := range plugins {
fmt.Println(name)
}
os.Exit(0)
case "--version", "-v":
fmt.Printf("%s %s\n", ownName, version.Version())
os.Exit(0)
}
if builder, ok := plugins[os.Args[1]]; ok {
os.Args = os.Args[1:]
return builder()
}
fmt.Printf("Unknown plugin: %s\n", os.Args[1])
os.Exit(1)
return nil
}
func main() {
runPlugin := getPlugin()
output := runPlugin.Results()
defer func() {
if r := recover(); r != nil {
output.SetState(status.StatusUnknown, "Internal error")
output.AddLinef("Error info: %v", r)
}
fmt.Println(output.String())
os.Exit(output.ExitCode())
}()
if runPlugin.CheckArguments() {
runPlugin.RunCheck()
}
}