64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
ContentDir string
|
|
Listen string
|
|
Title string
|
|
Author string
|
|
BaseURL string
|
|
}
|
|
|
|
type bookYAML struct {
|
|
Title string `yaml:"title"`
|
|
Author string `yaml:"author"`
|
|
}
|
|
|
|
func Load() (*Config, error) {
|
|
c := &Config{}
|
|
flag.StringVar(&c.ContentDir, "content", ".", "path to markdown content directory")
|
|
flag.StringVar(&c.Listen, "listen", ":8080", "listen address")
|
|
flag.StringVar(&c.Title, "title", "", "site title (overrides book.yaml)")
|
|
flag.StringVar(&c.BaseURL, "base-url", "", "base URL prefix (e.g. /docs)")
|
|
flag.Parse()
|
|
|
|
abs, err := filepath.Abs(c.ContentDir)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("content dir: %w", err)
|
|
}
|
|
c.ContentDir = abs
|
|
|
|
info, err := os.Stat(c.ContentDir)
|
|
if err != nil || !info.IsDir() {
|
|
return nil, fmt.Errorf("content dir %q is not a directory", c.ContentDir)
|
|
}
|
|
|
|
c.loadBookYAML()
|
|
return c, nil
|
|
}
|
|
|
|
func (c *Config) loadBookYAML() {
|
|
data, err := os.ReadFile(filepath.Join(c.ContentDir, "book.yaml"))
|
|
if err != nil {
|
|
return
|
|
}
|
|
var b bookYAML
|
|
if err := yaml.Unmarshal(data, &b); err != nil {
|
|
return
|
|
}
|
|
if c.Title == "" && b.Title != "" {
|
|
c.Title = b.Title
|
|
}
|
|
if c.Author == "" && b.Author != "" {
|
|
c.Author = b.Author
|
|
}
|
|
}
|