初版功能完成
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user