-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull_setup_example.go
More file actions
100 lines (86 loc) · 2.47 KB
/
Copy pathfull_setup_example.go
File metadata and controls
100 lines (86 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
"time"
"github.com/MimoJanra/confkit"
"github.com/MimoJanra/confkit/schema"
)
type AppConfig struct {
Port int `toml:"port" desc:"HTTP server port" default:"8080" validate:"min=1,max=65535"`
Host string `toml:"host" desc:"HTTP server host" default:"localhost" validate:"required"`
Database struct {
URL string `toml:"url" desc:"Database connection string" validate:"required"`
MaxConn int `toml:"max_conn" default:"10" validate:"min=1,max=100"`
Timeout time.Duration `toml:"timeout" default:"30s" validate:"min=1s,max=5m"`
Password string `toml:"password" secret:"true" validate:"required"`
} `toml:"database"`
LogLevel string `toml:"log_level" desc:"Log level" default:"info" validate:"oneof=debug,info,warn,error"`
}
func main() {
cfg, err := confkit.LoadWithOptions[AppConfig](
confkit.WithSource(confkit.FromTOML("examples/config.toml")),
confkit.WithSource(confkit.FromEnv()),
confkit.WithValidator("dburl", func(v reflect.Value) error {
if v.Kind() == reflect.String {
s := v.String()
if s == "" {
return fmt.Errorf("database URL cannot be empty")
}
if !isValidDBURL(s) {
return fmt.Errorf("invalid database URL format")
}
}
return nil
}),
)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
fmt.Println("✅ Configuration loaded successfully!")
fmt.Printf("Config: %+v\n\n", cfg)
s, err := schema.GenerateSchema[AppConfig]()
if err != nil {
log.Fatalf("Failed to generate schema: %v", err)
}
schemaJSON, err := json.MarshalIndent(s, "", " ")
if err != nil {
log.Fatalf("Failed to marshal schema: %v", err)
}
fmt.Println("📋 Generated JSON Schema:")
fmt.Println(string(schemaJSON))
fmt.Println()
docs, err := schema.GenerateMarkdown[AppConfig]()
if err != nil {
log.Fatalf("Failed to generate docs: %v", err)
}
fmt.Println("📖 Generated Documentation:")
fmt.Println(docs)
fmt.Println()
help, err := schema.GenerateCLIHelp[AppConfig]()
if err != nil {
log.Fatalf("Failed to generate help: %v", err)
}
fmt.Println("❓ Generated CLI Help:")
fmt.Println(help)
}
func isValidDBURL(url string) bool {
if len(url) < 5 {
return false
}
if len(url) >= 7 && url[:7] == "postgres" {
return true
}
if len(url) >= 5 && url[:5] == "mysql" {
return true
}
if len(url) >= 7 && url[:7] == "mongodb" {
return true
}
if len(url) >= 6 && url[:6] == "sqlite" {
return true
}
return false
}