osaet/backend/internal/cli/config.go
yarnom b78f4b39c9 Initialize blog scaffold
Add the CLI, site, and sample content so the project can run locally.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 16:58:30 +08:00

117 lines
2.4 KiB
Go

package cli
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
func runConfig(root string, args []string) error {
if len(args) == 0 {
return errors.New("missing config subcommand")
}
switch args[0] {
case "import":
return runConfigImport(root, args[1:])
case "export":
return runConfigExport(root, args[1:])
case "diff":
return runConfigDiff(root, args[1:])
case "sync":
return runConfigSync(root, args[1:])
default:
return fmt.Errorf("unknown config subcommand %q", args[0])
}
}
func loadLocalConfig(root string) (localConfig, error) {
var config localConfig
path := filepath.Join(root, "config/local.yaml")
data, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return config, nil
}
return config, err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return config, fmt.Errorf("%s: %w", path, err)
}
return config, nil
}
func readSiteConfig(root string) (siteConfigFile, error) {
var config siteConfigFile
path := filepath.Join(root, "config/site.yaml")
data, err := os.ReadFile(path)
if err != nil {
return config, err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return config, fmt.Errorf("%s: %w", path, err)
}
return config, nil
}
func writeSiteConfig(root string, config siteConfigFile) error {
var out bytes.Buffer
encoder := yaml.NewEncoder(&out)
encoder.SetIndent(2)
if err := encoder.Encode(config); err != nil {
return err
}
if err := encoder.Close(); err != nil {
return err
}
path := filepath.Join(root, "config/site.yaml")
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
tmp := path + ".tmp"
if err := os.WriteFile(tmp, out.Bytes(), 0o644); err != nil {
return err
}
return os.Rename(tmp, path)
}
func defaultSiteConfig() string {
return `meta:
config_version: 1
updated_at: "2026-05-28T12:00:00+08:00"
updated_by: "cli"
site:
title: "Osaet"
description: "Personal blog"
base_url: "http://localhost:4321"
language: "zh-CN"
timezone: "Asia/Shanghai"
content:
posts_dir: "content/posts"
assets_dir: "content/assets"
build:
astro_project: "frontend/site"
output_dir: "dist/site"
`
}
func defaultLocalExampleConfig() string {
return `database:
driver: "sqlite"
sqlite_path: ".osaet/osaet.db"
postgres_dsn: ""
deepseek:
api_key: ""
api_key_env: "DEEPSEEK_API_KEY"
base_url: "https://api.deepseek.com"
model: "deepseek-v4-pro"
`
}