gomonop/main.go

77 lines
1.5 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"path/filepath"
2024-07-20 20:47:42 +02:00
"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{
2024-07-20 20:47:42 +02:00
"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)
2024-07-20 11:32:39 +02:00
fmt.Printf(" %s --plugins|-p\n", ownName)
2024-07-20 10:11:31 +02:00
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()
}
}