43 lines
942 B
Go
43 lines
942 B
Go
|
|
package persona
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
|
||
|
|
"gopkg.in/yaml.v3"
|
||
|
|
)
|
||
|
|
|
||
|
|
// yamlLoader loads personas from YAML files.
|
||
|
|
type yamlLoader struct{}
|
||
|
|
|
||
|
|
// NewYAMLLoader returns a Loader that reads from YAML files.
|
||
|
|
func NewYAMLLoader() Loader {
|
||
|
|
return &yamlLoader{}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *yamlLoader) Load(ctx context.Context, configPath string) (Persona, error) {
|
||
|
|
data, err := os.ReadFile(configPath)
|
||
|
|
if err != nil {
|
||
|
|
return Persona{}, fmt.Errorf("reading persona file: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
var p Persona
|
||
|
|
if err := yaml.Unmarshal(data, &p); err != nil {
|
||
|
|
return Persona{}, fmt.Errorf("parsing YAML: %w", err)
|
||
|
|
}
|
||
|
|
return p, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *yamlLoader) Discover(ctx context.Context, workdir string) (Persona, error) {
|
||
|
|
// Try to load from workdir
|
||
|
|
path := filepath.Join(workdir, "persona.yaml")
|
||
|
|
if _, err := os.Stat(path); err == nil {
|
||
|
|
return l.Load(ctx, path)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Return default if no persona file found
|
||
|
|
return DefaultPersona(), nil
|
||
|
|
}
|