Skip to content

Commit 7e4d0f9

Browse files
committed
feat(config): add configuration validation CLI
1 parent c8908a8 commit 7e4d0f9

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
.PHONY: all tidy fa fmt lint vet test pulse pulse-migrate
1+
.PHONY: all tidy fa fmt lint vet test pulse pulse-migrate pulse-validate
22

3-
SERVICES = pulse pulse-migrate
3+
SERVICES = pulse pulse-migrate pulse-validate
44

55
all: tidy fa fmt lint
66

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ Example configuration files are available in:
5757
- `examples/services.yaml`
5858
- `examples/checks/api-checks.yaml`
5959

60+
Validate configuration with:
61+
62+
```bash
63+
CONFIG_DIR=./examples go run ./cmd/pulse-validate
64+
```
65+
6066
Before starting the app, apply migrations:
6167

6268
```bash

cmd/pulse-migrate/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020

2121
func rootCommand(ctx context.Context) *cobra.Command {
2222
cmd := &cobra.Command{
23-
Use: "migrate",
23+
Use: "pulse-migrate",
2424
Short: "Migrate database",
2525
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
2626
migrationsDir = os.Getenv("POSTGRES_MIGRATIONS_DIR")

cmd/pulse-validate/main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"os"
6+
"os/signal"
7+
"syscall"
8+
9+
"github.com/joho/godotenv"
10+
"github.com/spf13/cobra"
11+
12+
"github.com/pixel365/pulse/internal/config"
13+
)
14+
15+
func main() {
16+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
17+
defer stop()
18+
19+
_ = godotenv.Load()
20+
21+
cmd := &cobra.Command{
22+
Use: "pulse-validate",
23+
Short: "Validate configuration",
24+
RunE: func(_ *cobra.Command, _ []string) error {
25+
_, err := config.Load()
26+
return err
27+
},
28+
}
29+
30+
if err := cmd.ExecuteContext(ctx); err != nil {
31+
os.Exit(1)
32+
}
33+
34+
println("ok")
35+
}

internal/config/config.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ type Config struct {
2121
}
2222

2323
func MustLoad() *Config {
24+
cfg, err := Load()
25+
if err != nil {
26+
panic(err)
27+
}
28+
29+
return cfg
30+
}
31+
32+
func Load() (*Config, error) {
2433
dir := os.Getenv("CONFIG_DIR")
2534
if dir == "" {
26-
panic("CONFIG_DIR is not set")
35+
return nil, fmt.Errorf("CONFIG_DIR is not set")
2736
}
2837

2938
cfg := &Config{
@@ -36,10 +45,10 @@ func MustLoad() *Config {
3645
TLSChecks: make(map[string]TypedCheck[TLSSpec]),
3746
}
3847
if err := cfg.load(); err != nil {
39-
panic(err)
48+
return nil, err
4049
}
4150

42-
return cfg
51+
return cfg, nil
4352
}
4453

4554
func (c *Config) load() error {

0 commit comments

Comments
 (0)