Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/cli. OpenStatus CLI www.openstatus.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202package monitors
import ( "context" "errors" "fmt" "net/http" "os"
"github.com/google/go-cmp/cmp" confirmation "github.com/openstatusHQ/cli/internal/cli" "github.com/openstatusHQ/cli/internal/config" "github.com/urfave/cli/v3" "sigs.k8s.io/yaml")
func CompareLockWithConfig(apiKey string, applyChange bool, lock config.MonitorsLock, configData config.Monitors) (config.MonitorsLock, error) {
var created, updated, deleted int // Create or update monitors for v, configValue := range configData { value, exist := lock[v]
if !exist {
if applyChange {
result, err := CreateMonitor(http.DefaultClient, apiKey, configValue) if err != nil { return nil, err } lock[v] = config.Lock{ ID: result.ID, Monitor: configValue, } }
created++
continue } if !cmp.Equal(configValue, value.Monitor) { if applyChange {
result, err := UpdateMonitor(http.DefaultClient, apiKey, value.ID, configValue) if err != nil { return nil, err } lock[v] = config.Lock{ ID: result.ID, Monitor: configValue, } } updated++ continue } }
// Delete monitors for v, value := range lock { if _, exist := configData[v]; !exist { if applyChange {
err := DeleteMonitor(http.DefaultClient, apiKey, fmt.Sprintf("%d", value.ID)) if err != nil { fmt.Println(err) } delete(lock, v) } deleted++ } }
if created == 0 && updated == 0 && deleted == 0 { fmt.Println("No change founded") return nil, nil }
if applyChange { fmt.Println("Successfully apply") // if created > 0 { // fmt.Println("Monitor Created:", created) // } // if updated > 0 { // fmt.Println("Monitor Updated:", updated) // } // if deleted > 0 { // fmt.Println("Monitor Deleted:", deleted) // }
return lock, nil } fmt.Println("This will apply the following change:") if created > 0 { fmt.Println("Monitor Create:", created) } if updated > 0 { fmt.Println("Monitor Update:", updated) } if deleted > 0 { fmt.Println("Monitor Delete:", deleted) }
if !confirmation.AskForConfirmation(("Do you want to continue?")) { return nil, nil } return lock, nil}
func GetMonitorsApplyCmd() *cli.Command { monitorsListCmd := cli.Command{ Name: "apply", Usage: "Create or update monitors", Description: "Creates or updates monitors according to the OpenStatus configuration file", UsageText: "openstatus monitors apply [options]", Flags: []cli.Flag{ &cli.StringFlag{ Name: "config", Usage: "The configuration file containing monitor information", Aliases: []string{"c"}, DefaultText: "openstatus.yaml", Value: "openstatus.yaml", }, &cli.StringFlag{ Name: "access-token", Usage: "OpenStatus API Access Token", Aliases: []string{"t"}, Sources: cli.EnvVars("OPENSTATUS_API_TOKEN"), Required: true, }, &cli.BoolFlag{ Name: "auto-accept", Usage: "Automatically accept the prompt", Aliases: []string{"y"}, Required: false, }, }, Action: func(ctx context.Context, cmd *cli.Command) error {
path := cmd.String("config")
if path != "" { if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { return cli.Exit("Config does not exist", 1) } }
// Read Config file // monitors, err := config.ReadOpenStatus(path) if err != nil { return cli.Exit("Unable to read config file", 1) }
lock, err := config.ReadLockFile("openstatus.lock")
if err != nil { return cli.Exit("Unable to read lock file", 1) }
accept := cmd.Bool("auto-accept") if !accept { r, err := CompareLockWithConfig(cmd.String("access-token"), false, lock, monitors) if err != nil { return cli.Exit("Failed to apply change", 1)
} if r == nil { return nil } }
newLock, err := CompareLockWithConfig(cmd.String("access-token"), true, lock, monitors) if err != nil { return cli.Exit("Failed to apply change", 1) } if newLock == nil { fmt.Println("No change founded") return nil } y, err := yaml.Marshal(&newLock) if err != nil { return cli.Exit("Failed to apply change", 1) } // Write Lock file file, err := os.OpenFile("openstatus.lock", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return cli.Exit("Failed to apply change", 1)
} defer file.Close()
_, err = file.Write(y) if err != nil { return cli.Exit("Failed to apply change", 1) } return nil }, } return &monitorsListCmd}