Browse Source

Simplify command-line argument logic

tags/v1.1
Moritz Marquardt 8 months ago
parent
commit
d35948714e
1 changed files with 29 additions and 33 deletions
  1. +29
    -33
      cmd/main.go

+ 29
- 33
cmd/main.go View File

@@ -6,12 +6,10 @@ import (
"strconv"
"time"

"codeberg.org/momar/ternary"

"codeberg.org/momar/chihuahua"
"codeberg.org/momar/chihuahua/types"

"codeberg.org/momar/logg"
"codeberg.org/momar/ternary"
"github.com/teris-io/cli"
"gopkg.in/yaml.v2"
)
@@ -19,47 +17,45 @@ import (
func main() {
app := cli.New("the smallest watchdog on earth")

app.WithCommand(cli.NewCommand("watch", "start webserver and watchdog, and repeatedly run monitoring checks (by default every 5 minutes)").
WithOption(cli.NewOption("config", "path to the configuration file").WithChar('c').WithType(cli.TypeString)).
app.WithOption(cli.NewOption("config", "path to the configuration file").WithChar('c').WithType(cli.TypeString)).
WithOption(cli.NewOption("interval", "check interval in seconds").WithChar('i').WithType(cli.TypeInt)).
WithOption(cli.NewOption("once", "only run checks once and print the result - doesn't start the webserver").WithType(cli.TypeBool)).
WithOption(cli.NewOption("silent", "disables notifications; use with care!").WithType(cli.TypeBool)).
WithAction(func(args []string, options map[string]string) int {
// Parse the config
cfg := getConfig(options["config"])
servers := cfg.GetServers()

// Parse the interval
i, _ := strconv.Atoi(options["interval"])
if i <= 0 {
i = 300
}
id := time.Duration(ternary.Default(i, 300).(int)) * time.Second

go chihuahua.Api(servers)
for {
chihuahua.RunAllChecks(servers)
chihuahua.Notify(cfg.Notifications, servers)
logg.Debug("Waiting for next check at %s...", time.Now().Add(id).UTC().Format("2006-01-02 15:04:05"))
time.Sleep(id)
first := true
if options["once"] == "" {
// Start the webserver
go chihuahua.Api(servers)
}
return 0
}))
for options["once"] == "" || first {
// Run the actual checks
chihuahua.RunAllChecks(servers)

app.WithCommand(cli.NewCommand("once", "run monitoring checks once and print the result (doesn't start the webserver, but will send notifications for everything that's not OK)").
WithOption(cli.NewOption("config", "path to the configuration file").WithChar('c').WithType(cli.TypeString)).
WithAction(func(args []string, options map[string]string) int {
cfg := getConfig(options["config"])
servers := cfg.GetServers()
chihuahua.RunAllChecks(servers)
chihuahua.Notify(cfg.Notifications, servers)
chihuahua.Print(servers)
return 0
}))
if options["silent"] == "" {
// Send notifications
chihuahua.Notify(cfg.Notifications, servers)
}

/*app.WithCommand(cli.NewCommand("show", "print the results from a remote chihuaua server").
WithArg(cli.NewArg("server", "address of the server").WithType(cli.TypeString)).
WithArg(cli.NewArg("filter", "filter checks by their name using a regular expression").WithType(cli.TypeString).AsOptional()).
WithAction(func(args []string, options map[string]string) int {
// do something
return 0
}))*/
if options["once"] == "" {
logg.Debug("Waiting for next check at %s...", time.Now().Add(id).UTC().Format("2006-01-02 15:04:05"))
time.Sleep(id)
}
first = false
}
if options["once"] != "" {
// Print the output if --once is set
chihuahua.Print(servers)
}
return 0
})

os.Exit(app.Run(os.Args, os.Stdout))
}

Loading…
Cancel
Save