Compare commits

..

No commits in common. "24c3d5c5b25a7fb4f4f80dd5aaf5ea75a7cd2a72" and "bc34b2d813f257589251303839521634a47ae4fb" have entirely different histories.

6 changed files with 12 additions and 153 deletions

View file

@ -1,7 +1,6 @@
package gohealthchecks
import (
"fmt"
"path"
"github.com/google/uuid"
@ -12,50 +11,28 @@ type Check interface {
params() map[string]string
}
type uuidCheck struct {
checkID uuid.UUID
runID uuid.UUID
}
type uuidCheck uuid.UUID
func (c uuidCheck) path() string {
return c.checkID.String()
return uuid.UUID(c).String()
}
func (c uuidCheck) params() map[string]string {
if c.runID == uuid.Nil {
return nil
}
return map[string]string{
"rid": c.runID.String(),
}
}
func NewUUIDCheck(checkRawID, runRawID string) (Check, error) {
checkID, err := uuid.Parse(checkRawID)
func NewUUIDCheck(rawUUID string) (Check, error) {
val, err := uuid.Parse(rawUUID)
if err != nil {
return nil, err
}
runID := uuid.Nil
if runRawID != "" {
runID, err = uuid.Parse(runRawID)
if err != nil {
return nil, err
}
}
return uuidCheck{
checkID: checkID,
runID: runID,
}, nil
return uuidCheck(val), nil
}
type slugCheck struct {
pingKey string
slug string
autoCreate bool
runID uuid.UUID
}
func (c slugCheck) path() string {
@ -70,27 +47,13 @@ func (c slugCheck) params() map[string]string {
}
res["create"] = createVal
if c.runID != uuid.Nil {
res["rid"] = c.runID.String()
}
return res
}
func NewSlugCheck(pingKey, slug string, autoCreate bool, runRawID string) (Check, error) {
runID := uuid.Nil
if runRawID != "" {
var err error
runID, err = uuid.Parse(runRawID)
if err != nil {
return nil, fmt.Errorf("invalid run ID: %w", err)
}
}
func NewSlugCheck(pingKey, slug string, autoCreate bool) Check {
return slugCheck{
pingKey: pingKey,
slug: slug,
autoCreate: autoCreate,
runID: runID,
}, nil
}
}

View file

@ -1,30 +0,0 @@
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)
}

View file

@ -1,32 +0,0 @@
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)
}

View file

@ -1,36 +0,0 @@
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)
}

View file

@ -19,7 +19,6 @@ var (
pingKey string
checkSlug string
autoCreate bool
runID string
)
// Execute adds all child commands to the root command and sets flags appropriately.
@ -34,7 +33,6 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().StringVar(&pingHost, "host", gohealthchecks.HealthchecksIOPingHost, "healthchecks host to use")
rootCmd.PersistentFlags().BoolVar(&autoCreate, "auto-create", false, "auto create check if it does not exist (slug only)")
rootCmd.PersistentFlags().StringVar(&runID, "run-id", "", "Optional run-id to identify the job")
rootCmd.PersistentFlags().StringVar(&checkUUID, "uuid", "", "check UUID")
rootCmd.PersistentFlags().StringVar(&pingKey, "ping-key", "", "ping key")
rootCmd.PersistentFlags().StringVar(&checkSlug, "slug", "", "check-slug")

View file

@ -7,8 +7,8 @@ import (
gohealthchecks "git.faercol.me/faercol/go-healthchecks"
)
func mustParseUUIDCheck(checkID, runID string) gohealthchecks.Check {
check, err := gohealthchecks.NewUUIDCheck(checkID, runID)
func mustParseUUIDCheck(rawVal string) gohealthchecks.Check {
check, err := gohealthchecks.NewUUIDCheck(rawVal)
if err != nil {
failf("Invalid UUID: %s\n", err)
}
@ -17,14 +17,10 @@ func mustParseUUIDCheck(checkID, runID string) gohealthchecks.Check {
func parseCheckFlags() gohealthchecks.Check {
var check gohealthchecks.Check
var err error
if checkUUID != "" {
check = mustParseUUIDCheck(checkUUID, runID)
check = mustParseUUIDCheck(checkUUID)
} else {
check, err = gohealthchecks.NewSlugCheck(pingKey, checkSlug, autoCreate, runID)
if err != nil {
failf("Invalid UUID: %s\n", err)
}
check = gohealthchecks.NewSlugCheck(pingKey, checkSlug, autoCreate)
}
return check
}