From f3ee372631dbdff2b508fa16bc22092e8dbbad96 Mon Sep 17 00:00:00 2001 From: Thibault Le Ouay Date: Tue, 15 Jul 2025 22:02:25 +0200 Subject: [PATCH] lock init --- internal/config/lock.go | 38 +++++++++++++++++ internal/config/openstatus.go | 6 --- internal/monitors/monitor_apply.go | 66 +++++++++++++++++++++++++++++ internal/monitors/monitor_create.go | 2 - internal/monitors/monitor_export.go | 7 +-- internal/monitors/monitor_info.go | 2 +- internal/monitors/monitors.go | 1 + 7 files changed, 108 insertions(+), 14 deletions(-) create mode 100644 internal/config/lock.go create mode 100644 internal/monitors/monitor_apply.go diff --git a/internal/config/lock.go b/internal/config/lock.go new file mode 100644 index 0000000..fe0a72b --- /dev/null +++ b/internal/config/lock.go @@ -0,0 +1,38 @@ +package config + +import ( + "errors" + "os" + + "github.com/knadh/koanf/parsers/yaml" + "github.com/knadh/koanf/providers/file" +) + +var lock = "openstatus.lock" + +func ReadLockFile() (*Config, error) { + + var out Config + + + if _, err := os.Stat(lock); errors.Is(err, os.ErrNotExist) { + return &Config{}, nil + } + + file := file.Provider(lock) + + err := k.Load(file, yaml.Parser()) + + if err != nil { + return nil, err + } + + + err = k.Unmarshal("", &out) + if err != nil { + return nil, err + } + + return &out, nil + +} diff --git a/internal/config/openstatus.go b/internal/config/openstatus.go index 0a614d8..7392cb1 100644 --- a/internal/config/openstatus.go +++ b/internal/config/openstatus.go @@ -10,12 +10,6 @@ type Monitors map[string]Monitor func ReadOpenStatus(path string) ([]Monitor, error) { f := file.Provider(path) - // r, _:= f.ReadBytes() - - // fmt.Printf("%v", string(r)) - // for _, line := range string(r) { - // fmt.Println(line) - // } err := k.Load(f, yaml.Parser()) if err != nil { diff --git a/internal/monitors/monitor_apply.go b/internal/monitors/monitor_apply.go new file mode 100644 index 0000000..78c4669 --- /dev/null +++ b/internal/monitors/monitor_apply.go @@ -0,0 +1,66 @@ +package monitors + +import ( + "context" + "errors" + "fmt" + "os" + + "github.com/openstatusHQ/cli/internal/config" + "github.com/urfave/cli/v3" +) + + +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", + 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, + }, + }, + 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() + if err != nil { + return cli.Exit("Unable to read lock file", 1) + } + + fmt.Println(monitors,lock) + + // Read Lock file + // Compare Config file with Lock file + // If changes detected, apply changes + return nil + }, + } + return &monitorsListCmd +} diff --git a/internal/monitors/monitor_create.go b/internal/monitors/monitor_create.go index 80839fa..13efe69 100644 --- a/internal/monitors/monitor_create.go +++ b/internal/monitors/monitor_create.go @@ -108,5 +108,3 @@ func GetMonitorCreateCmd() *cli.Command { } return &monitorInfoCmd } - -// os_3Za36BLXy7pN9ZY36SvSLnjD diff --git a/internal/monitors/monitor_export.go b/internal/monitors/monitor_export.go index 7138450..27f03b6 100644 --- a/internal/monitors/monitor_export.go +++ b/internal/monitors/monitor_export.go @@ -87,7 +87,6 @@ func ExportMonitor(httpClient *http.Client, apiKey string, path string) error { Body: monitor.Body, Headers: headers, } - break case "tcp": uri := strings.Split(monitor.URL, ":") @@ -96,7 +95,7 @@ func ExportMonitor(httpClient *http.Client, apiKey string, path string) error { Host: uri[0], Port: int64(port), } - break + default: return fmt.Errorf("unknown job type: %s", monitor.JobType) } @@ -108,7 +107,6 @@ func ExportMonitor(httpClient *http.Client, apiKey string, path string) error { Description: monitor.Description, DegradedAfter: int64(monitor.DegradedAfter), Frequency: config.Frequency(monitor.Periodicity), - // Regions: monitor.Regions, Request: request, Kind: config.CoordinateKind(monitor.JobType), Retry: int64(monitor.Retry), @@ -116,7 +114,6 @@ func ExportMonitor(httpClient *http.Client, apiKey string, path string) error { Assertions: assertions, } } - // defer file.Close() y, err := yaml.Marshal(&t) if err != nil { return err @@ -138,7 +135,7 @@ func GetMonitorExportCmd() *cli.Command { monitorInfoCmd := cli.Command{ Name: "export", Usage: "Export all your monitors", - UsageText: "openstatus monitor export [options]", + UsageText: "openstatus monitors export [options]", Description: "Export all your monitors to YAML", Action: func(ctx context.Context, cmd *cli.Command) error { // monitorId := cmd.Args().Get(0) diff --git a/internal/monitors/monitor_info.go b/internal/monitors/monitor_info.go index af190e7..ebaef9d 100644 --- a/internal/monitors/monitor_info.go +++ b/internal/monitors/monitor_info.go @@ -110,7 +110,7 @@ func GetMonitorInfoCmd() *cli.Command { monitorInfoCmd := cli.Command{ Name: "info", Usage: "Get a monitor information", - UsageText: "openstatus monitor info [MonitorID]", + UsageText: "openstatus monitors info [MonitorID]", Description: "Fetch the monitor information. The monitor information includes details such as name, description, endpoint, method, frequency, locations, active status, public status, timeout, degraded after, and body. The body is truncated to 40 characters.", Action: func(ctx context.Context, cmd *cli.Command) error { monitorId := cmd.Args().Get(0) diff --git a/internal/monitors/monitors.go b/internal/monitors/monitors.go index d607433..158a7da 100644 --- a/internal/monitors/monitors.go +++ b/internal/monitors/monitors.go @@ -95,6 +95,7 @@ func MonitorsCmd() *cli.Command { Usage: "Manage your monitors", Commands: []*cli.Command{ + GetMonitorsApplyCmd(), GetMonitorCreateCmd(), GetMonitorDeleteCmd(), GetMonitorExportCmd(), -- knotmirror 0.0.1