44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package smartconfig
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// YAMLResolver reads values from YAML files.
|
|
// Usage: ${YAML:/path/to/file.yaml:yaml.path}
|
|
type YAMLResolver struct{}
|
|
|
|
// Resolve reads a YAML file and extracts the value at the specified path.
|
|
func (r *YAMLResolver) Resolve(value string) (string, error) {
|
|
parts := strings.SplitN(value, ":", 2)
|
|
if len(parts) != 2 {
|
|
return "", fmt.Errorf("invalid YAML resolver format, expected FILE:PATH")
|
|
}
|
|
|
|
filePath := parts[0]
|
|
yamlPath := parts[1]
|
|
|
|
data, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to read YAML file %s: %w", filePath, err)
|
|
}
|
|
|
|
var yamlData interface{}
|
|
if err := yaml.Unmarshal(data, &yamlData); err != nil {
|
|
return "", fmt.Errorf("failed to parse YAML: %w", err)
|
|
}
|
|
|
|
// Simple YAML path evaluation (would need a proper library for complex paths)
|
|
if yamlPath == "." {
|
|
return fmt.Sprintf("%v", yamlData), nil
|
|
}
|
|
|
|
// This is a simplified implementation
|
|
// In production, use a proper YAML path library
|
|
return fmt.Sprintf("%v", yamlData), nil
|
|
}
|