docs(cabana,compass,conga): compile README Go examples via src= (quick-261118-qsd)
- cabana: the hand-written controller fence points at Post, PostsController and BlogPlugin declarations; TestDocsDeclarations now calls each of them - compass: mailSettings and ExampleOpen; the YAML block matches the directory ExampleOpen writes - conga: job, dispatch and progress fences reuse the Jobs page targets; new reindex and worker regions run in TestDocsDispatch (Docker), and the schedule fence is FeedPlugin.Schedule, checked by TestDocsSchedule
This commit is contained in:
@@ -37,47 +37,83 @@ Use cases:
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
package blog
|
||||
|
||||
import (
|
||||
"git.golem15.com/golem15/summercms/modules/compass"
|
||||
)
|
||||
|
||||
```go src=modules/compass/example_test.go#mailSettings
|
||||
type mailSettings struct {
|
||||
Host string `koanf:"host"`
|
||||
Port int `koanf:"port"`
|
||||
}
|
||||
|
||||
func loadConfig() (*compass.Config, error) {
|
||||
cfg, err := compass.Open(compass.Options{Dir: "config", Env: "development"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
name := cfg.String("app.name")
|
||||
perPage := cfg.Int("acme.blog.posts_per_page")
|
||||
_, _ = name, perPage
|
||||
|
||||
var mail mailSettings
|
||||
if err := cfg.LoadSection("mail", &mail); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Store a runtime override and write it to config/env/development/overrides.yaml.
|
||||
if err := cfg.Set("acme.blog.posts_per_page", 25); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cfg, cfg.Persist()
|
||||
Driver string `koanf:"driver"`
|
||||
From string `koanf:"from"`
|
||||
}
|
||||
```
|
||||
|
||||
A matching configuration directory:
|
||||
```go src=modules/compass/example_test.go#ExampleOpen
|
||||
dir, err := os.MkdirTemp("", "acme-config")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
if err := writeConfig(dir); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Environ stands in for the process environment (nil reads os.Environ).
|
||||
cfg, err := compass.Open(compass.Options{
|
||||
Dir: dir,
|
||||
Env: "development",
|
||||
Environ: []string{"SUMMER_MAIL__DRIVER=smtp"},
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// A plugin's embedded config/config.yaml becomes its defaults.
|
||||
plugin := fstest.MapFS{"config/config.yaml": {Data: []byte("posts_per_page: 10\n")}}
|
||||
if err := cfg.MergePlugin("acme.blog", plugin); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(cfg.String("app.name"), cfg.Bool("app.debug"), cfg.Int("acme.blog.posts_per_page"))
|
||||
var mail mailSettings
|
||||
if err := cfg.LoadSection("mail", &mail); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(mail.Driver, mail.From)
|
||||
_, found := cfg.Lookup("app.timezone")
|
||||
fmt.Println(cfg.Environment(), found)
|
||||
|
||||
// A runtime override, saved to env/development/overrides.yaml.
|
||||
if err := cfg.Set("acme.blog.posts_per_page", 25); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if err := cfg.Persist(); err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
saved, _ := os.ReadFile(filepath.Join(dir, "env", "development", "overrides.yaml"))
|
||||
fmt.Print(string(saved))
|
||||
// Output:
|
||||
// Acme true 10
|
||||
// smtp blog@example.com
|
||||
// development false
|
||||
// acme:
|
||||
// blog:
|
||||
// posts_per_page: 25
|
||||
```
|
||||
|
||||
The configuration directory the example writes:
|
||||
|
||||
```yaml
|
||||
# config/app.yaml
|
||||
name: Acme
|
||||
url: http://localhost:8080
|
||||
debug: false
|
||||
|
||||
# config/mail.yaml
|
||||
driver: log
|
||||
from: blog@example.com
|
||||
|
||||
# config/env/development/app.yaml
|
||||
debug: true
|
||||
|
||||
Reference in New Issue
Block a user