Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/cli. OpenStatus CLI www.openstatus.dev
Something went wrong. Try again.
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275package run
import ( "context" "encoding/json" "errors" "fmt" "io" "net/http" "os" "strings" "sync" "time"
"github.com/fatih/color" "github.com/logrusorgru/aurora/v4" "github.com/rodaine/table" "github.com/urfave/cli/v3"
"github.com/openstatusHQ/cli/internal/api" "github.com/openstatusHQ/cli/internal/auth" output "github.com/openstatusHQ/cli/internal/cli" "github.com/openstatusHQ/cli/internal/config" "github.com/openstatusHQ/cli/internal/monitors")
type runRegionResult struct { Region string `json:"region"` Latency int64 `json:"latency"` Status string `json:"status"` Error string `json:"error,omitempty"`}
type runMonitorResult struct { MonitorID string `json:"monitor_id"` Results []runRegionResult `json:"results"`}
// MonitorTrigger triggers a monitor run and returns the results without printing.func MonitorTrigger(ctx context.Context, httpClient *http.Client, apiKey string, monitorId string) (runMonitorResult, error) { if monitorId == "" { return runMonitorResult{}, fmt.Errorf("monitor ID is required") }
url := fmt.Sprintf("%s/monitor/%s/run", api.APIBaseURL, monitorId)
client := &http.Client{ Timeout: 2 * time.Minute, Transport: httpClient.Transport, }
payload := strings.NewReader("{}")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, payload) if err != nil { return runMonitorResult{}, err } req.Header.Add("x-openstatus-key", apiKey) res, err := client.Do(req) if err != nil { return runMonitorResult{}, err } defer res.Body.Close()
if res.StatusCode != http.StatusOK { return runMonitorResult{}, fmt.Errorf("failed to trigger monitor test") } body, err := io.ReadAll(res.Body) if err != nil { return runMonitorResult{}, fmt.Errorf("failed to read response body: %w", err) }
var result []json.RawMessage err = json.Unmarshal(body, &result) if err != nil { return runMonitorResult{}, err }
var regionResults []runRegionResult for _, r := range result { rr := monitors.RunResult{}
if err := json.Unmarshal(r, &rr); err != nil { return runMonitorResult{}, fmt.Errorf("unable to unmarshal: %w", err) }
entry := runRegionResult{ Region: rr.Region, Latency: rr.Latency, Status: "pass", }
switch rr.JobType { case "tcp": var tcp monitors.TCPRunResult if err := json.Unmarshal(r, &tcp); err != nil { return runMonitorResult{}, fmt.Errorf("unable to unmarshal: %w", err) } if tcp.ErrorMessage != "" { entry.Status = "fail" entry.Error = tcp.ErrorMessage } case "icmp": var icmpResult monitors.ICMPRunResult if err := json.Unmarshal(r, &icmpResult); err != nil { return runMonitorResult{}, fmt.Errorf("unable to unmarshal: %w", err) } if icmpResult.ErrorMessage != "" { entry.Status = "fail" entry.Error = icmpResult.ErrorMessage } case "http": var httpResult monitors.HTTPRunResult if err := json.Unmarshal(r, &httpResult); err != nil { return runMonitorResult{}, fmt.Errorf("unable to unmarshal: %w", err) } if httpResult.Error != "" { entry.Status = "fail" entry.Error = httpResult.Error } default: entry.Status = "unknown" entry.Error = fmt.Sprintf("unknown job type: %s", rr.JobType) }
regionResults = append(regionResults, entry) }
return runMonitorResult{ MonitorID: monitorId, Results: regionResults, }, nil}
// printMonitorResult prints a single monitor's results to stdout.func printMonitorResult(res runMonitorResult) bool { var inError bool fmt.Println(aurora.Bold(fmt.Sprintf("Monitor: %s", res.MonitorID))) headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc() columnFmt := color.New(color.FgYellow).SprintfFunc()
tbl := table.New("Region", "Latency (ms)", "Status") tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
for _, entry := range res.Results { if entry.Status == "fail" { tbl.AddRow(entry.Region, entry.Latency, color.RedString("fail")) inError = true } else { tbl.AddRow(entry.Region, entry.Latency, color.GreenString("pass")) } } tbl.Print()
if inError { fmt.Println(color.RedString("Some regions failed")) } else { fmt.Println(color.GreenString("All regions passed")) } fmt.Println() return inError}
func RunCmd() *cli.Command { runCmd := cli.Command{ Name: "run", Aliases: []string{"r"}, Usage: "Run your uptime tests", UsageText: `openstatus run openstatus run --config custom-config.yaml`, Description: `Run the uptime tests defined in the config.openstatus.yaml.The config file should be in the following format:
tests: ids: - monitor-id-1 - monitor-id-2
`, Action: func(ctx context.Context, cmd *cli.Command) error { apiKey, err := auth.ResolveAccessToken(cmd) if err != nil { return cli.Exit(err.Error(), 1) }
path := cmd.String("config") if path != "" { if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { return cli.Exit("Config does not exist", 1) } }
conf, err := config.ReadConfig(path) if err != nil { return err } size := len(conf.Tests.Ids)
if !output.IsQuiet() && !output.IsJSONOutput() { fmt.Print("Tests are running\n\n") }
type indexedResult struct { index int result runMonitorResult err error }
results := make([]indexedResult, size) var wg sync.WaitGroup var mu sync.Mutex
for i, id := range conf.Tests.Ids { wg.Add(1) go func(idx, id int) { defer wg.Done() res, err := MonitorTrigger(ctx, api.DefaultHTTPClient, apiKey, fmt.Sprintf("%d", id)) mu.Lock() results[idx] = indexedResult{index: idx, result: res, err: err} mu.Unlock() }(i, id) } wg.Wait()
// Print results sequentially to avoid interleaved output var hasErrors bool
if output.IsJSONOutput() { var allResults []runMonitorResult for _, r := range results { if r.err != nil { hasErrors = true continue } allResults = append(allResults, r.result) } if err := output.PrintJSON(allResults); err != nil { return err } } else { for _, r := range results { if r.err != nil { hasErrors = true fmt.Printf("Monitor %d: %v\n\n", conf.Tests.Ids[r.index], r.err) continue } if printMonitorResult(r.result) { hasErrors = true } } }
if hasErrors { return cli.Exit("Some tests failed", 1) } return nil }, Flags: []cli.Flag{ &cli.StringFlag{ Name: "config", Usage: "The configuration file", DefaultText: "config.openstatus.yaml", Value: "config.openstatus.yaml", }, &cli.StringFlag{ Name: "access-token", Usage: "OpenStatus API Access Token", Aliases: []string{"t"}, Sources: cli.EnvVars("OPENSTATUS_API_TOKEN"), }, }, } return &runCmd}