diff --git a/internal/config/lockfile.go b/internal/config/lockfile.go new file mode 100644 index 0000000..f1273e7 --- /dev/null +++ b/internal/config/lockfile.go @@ -0,0 +1,9 @@ +package config + +type LockFile []Lock + +type Lock struct { + Id int // unique identifier for the lock + Key string // unique identifier for the lock + Value Monitor +} diff --git a/internal/config/openstatus.go b/internal/config/openstatus.go index 0a614d8..628b928 100644 --- a/internal/config/openstatus.go +++ b/internal/config/openstatus.go @@ -7,7 +7,7 @@ import ( type Monitors map[string]Monitor -func ReadOpenStatus(path string) ([]Monitor, error) { +func ReadOpenStatus(path string) (Monitors, error) { f := file.Provider(path) // r, _:= f.ReadBytes() @@ -26,19 +26,12 @@ func ReadOpenStatus(path string) ([]Monitor, error) { err = k.Unmarshal("", &out) - for _, value := range out { - for _, assertion := range value.Assertions { - if assertion.Kind == Header || assertion.Kind == TextBody { - assertion.Target = assertion.Target.(string) - } - if assertion.Kind == StatusCode { - assertion.Target = assertion.Target.(int) - } - } - } + return out, nil +} +func TranslateMonitors(monitors Monitors) []Monitor { var monitor []Monitor - for _, value := range out { + for _, value := range monitors { for _, assertion := range value.Assertions { if assertion.Kind == Header || assertion.Kind == TextBody { assertion.Target = assertion.Target.(string) @@ -50,9 +43,5 @@ func ReadOpenStatus(path string) ([]Monitor, error) { monitor = append(monitor, value) } - if err != nil { - return nil, err - } - - return monitor, nil + return monitor } diff --git a/internal/monitors/monitor_create.go b/internal/monitors/monitor_create.go index 80839fa..f5f4931 100644 --- a/internal/monitors/monitor_create.go +++ b/internal/monitors/monitor_create.go @@ -65,16 +65,19 @@ func GetMonitorCreateCmd() *cli.Command { accept := cmd.Bool("auto-accept") - monitors, err := config.ReadOpenStatus(path) + out, err := config.ReadOpenStatus(path) if err != nil { return cli.Exit("Unable to read config file", 1) } + monitors := config.TranslateMonitors(out) + if !accept { if !confirmation.AskForConfirmation(fmt.Sprintf("You are about to create %d monitors do you want to continue", len(monitors))) { return nil } } + for _, value := range monitors { err = CreateMonitor(http.DefaultClient, cmd.String("access-token"), value) if err != nil { @@ -108,5 +111,3 @@ func GetMonitorCreateCmd() *cli.Command { } return &monitorInfoCmd } - -// os_3Za36BLXy7pN9ZY36SvSLnjD diff --git a/internal/monitors/monitors.go b/internal/monitors/monitors.go index d607433..f572b1f 100644 --- a/internal/monitors/monitors.go +++ b/internal/monitors/monitors.go @@ -101,6 +101,7 @@ func MonitorsCmd() *cli.Command { GetMonitorInfoCmd(), GetMonitorsListCmd(), GetMonitorsTriggerCmd(), + GetMonitorsApplyCmd(), }, } return &monitorsCmd diff --git a/internal/monitors/monitrors_apply.go b/internal/monitors/monitrors_apply.go new file mode 100644 index 0000000..537c48a --- /dev/null +++ b/internal/monitors/monitrors_apply.go @@ -0,0 +1,71 @@ +package monitors + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "os" + + "github.com/openstatusHQ/cli/internal/config" + "github.com/urfave/cli/v3" +) + +func GetMonitorsApplyCmd() *cli.Command { + monitorsApplyCmd := cli.Command{ + Name: "apply", + Usage: "Apply changes to monitors", + Description: "Apply changes to monitors. This command allows you to update the status of monitors.", + UsageText: "openstatus monitors apply [options]", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "config", + Usage: "The configuration file containing monitor information", + 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: 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) + } + } + + monitors, err := config.ReadOpenStatus(path) + if err != nil { + return cli.Exit("Failed to list monitors", 1) + } + + var lockFile config.LockFile + + for key, monitor := range monitors { + + n := config.Lock{ + Id: 1, + Key: key, + Value: monitor, + } + lockFile = append(lockFile, n) + } + + e, err := json.Marshal(lockFile) + if err != nil { + fmt.Println(err) + return err + } + fmt.Println(string(e)) + return nil + }, + } + return &monitorsApplyCmd +} diff --git a/openstatus.yaml b/openstatus.yaml new file mode 100644 index 0000000..0a27a3a --- /dev/null +++ b/openstatus.yaml @@ -0,0 +1,19 @@ +# yaml-language-server: $schema=https://www.openstatus.dev/schema.json + +monitor1: + name: "Test" + description: "This is a test" + frequency: "1m" + active: true + regions: ["iad"] + retry: 3 + kind: http + request: + url: https://openstat.us + method: GET + headers: + User-Agent: OpenStatus + assertions: + - kind: statusCode + compare: eq + target: 200