Add missing commands

This commit is contained in:
Melora Hugues 2024-10-02 16:00:40 +02:00
parent bc34b2d813
commit 2bc84fecc1
3 changed files with 98 additions and 0 deletions

30
cmd/failure.go Normal file
View file

@ -0,0 +1,30 @@
package main
import (
"fmt"
"os"
gohealthchecks "git.faercol.me/faercol/go-healthchecks"
"github.com/spf13/cobra"
)
var failureCmd = &cobra.Command{
Use: "failure",
Short: "Signal the check has failed",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
check := parseCheckFlags()
clt := gohealthchecks.NewPingClient(pingHost)
err := clt.ReportFailure(cmd.Context(), check)
if err != nil {
failf("Failed to notify failure: %s\n", err)
} else {
fmt.Println("Notified check failure")
os.Exit(0)
}
},
}
func init() {
rootCmd.AddCommand(failureCmd)
}

32
cmd/log.go Normal file
View file

@ -0,0 +1,32 @@
package main
import (
"fmt"
"os"
gohealthchecks "git.faercol.me/faercol/go-healthchecks"
"github.com/spf13/cobra"
)
var logCmd = &cobra.Command{
Use: "log <message>",
Short: "Send a log for the check",
Long: ``,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
check := parseCheckFlags()
msg := []byte(args[0])
clt := gohealthchecks.NewPingClient(pingHost)
err := clt.LogMessage(cmd.Context(), check, msg)
if err != nil {
failf("Failed to send log: %s\n", err)
} else {
fmt.Println("Log sent")
os.Exit(0)
}
},
}
func init() {
rootCmd.AddCommand(logCmd)
}

36
cmd/returncode.go Normal file
View file

@ -0,0 +1,36 @@
package main
import (
"fmt"
"os"
"strconv"
gohealthchecks "git.faercol.me/faercol/go-healthchecks"
"github.com/spf13/cobra"
)
var returncodeCmd = &cobra.Command{
Use: "returncode <message>",
Short: "Notify specific returncode for the check",
Long: ``,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
check := parseCheckFlags()
returncode, err := strconv.Atoi(args[0])
if err != nil {
failf("Invalid value for returncode: %s\n", err)
}
clt := gohealthchecks.NewPingClient(pingHost)
err = clt.ReportExitCode(cmd.Context(), check, returncode)
if err != nil {
failf("Failed to send log: %s\n", err)
} else {
fmt.Println("Log sent")
os.Exit(0)
}
},
}
func init() {
rootCmd.AddCommand(returncodeCmd)
}