package cli import ( "encoding/json" "fmt" "github.com/spf13/cobra" "gitea.mynodes.xyz/ilker/llmgw/internal/notify" ) func newNotifyCmd(client func() *ctlClient, asJSON *bool) *cobra.Command { c := &cobra.Command{ Use: "notify", Short: "The webhooks told about parked models, lost nodes, spent budgets and new weights", } c.AddCommand(&cobra.Command{ Use: "test", Short: "Send a test notification to every webhook in server.yaml's notify section", Long: "Sends one event of kind \"test\" to each webhook, once, and says how each\n" + "answered: the way to check an address, a secret and a firewall before\n" + "something real has to travel.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { var res []notify.Result if err := client().post(cmd.Context(), "/api/v1/notify/test", nil, &res); err != nil { return err } if *asJSON { return json.NewEncoder(cmd.OutOrStdout()).Encode(res) } failed := 0 for _, r := range res { if r.Err != "" { failed++ fmt.Fprintf(cmd.OutOrStdout(), "%s failed: %s\n", r.URL, r.Err) } else { fmt.Fprintf(cmd.OutOrStdout(), "%s delivered\n", r.URL) } } if failed > 0 { return fmt.Errorf("%d of %d webhooks were not reached", failed, len(res)) } return nil }, }) return c }