Files
RemLink/internal/config/load.go
T
qsc20001102 142e5dc7d6
ci / Go checks (ubuntu-latest) (push) Has been cancelled
ci / Go checks (windows-latest) (push) Has been cancelled
初版功能完成
2026-08-29 13:12:17 +08:00

38 lines
841 B
Go

// Package config loads only non-sensitive RemLink YAML configuration.
package config
import (
"errors"
"fmt"
"io"
"os"
"gopkg.in/yaml.v3"
)
func decodeStrict(path string, target any) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("open config %q: %w", path, err)
}
defer file.Close()
decoder := yaml.NewDecoder(file)
decoder.KnownFields(true)
if err := decoder.Decode(target); err != nil {
if errors.Is(err, io.EOF) {
return fmt.Errorf("config %q is empty", path)
}
return fmt.Errorf("decode config %q: %w", path, err)
}
var extra any
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
if err != nil {
return fmt.Errorf("decode trailing YAML document in %q: %w", path, err)
}
return fmt.Errorf("config %q must contain exactly one YAML document", path)
}
return nil
}