bd sync: 2025-12-27 15:56:42
This commit is contained in:
@@ -7,16 +7,10 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// Formula file extensions. TOML is preferred, JSON is legacy fallback.
|
||||
const (
|
||||
FormulaExtTOML = ".formula.toml"
|
||||
FormulaExtJSON = ".formula.json"
|
||||
FormulaExt = FormulaExtJSON // Legacy alias for backwards compatibility
|
||||
)
|
||||
// FormulaExt is the file extension for formula files.
|
||||
const FormulaExt = ".formula.json"
|
||||
|
||||
// Parser handles loading and resolving formulas.
|
||||
//
|
||||
@@ -74,7 +68,6 @@ func defaultSearchPaths() []string {
|
||||
}
|
||||
|
||||
// ParseFile parses a formula from a file path.
|
||||
// Detects format from extension: .formula.toml or .formula.json
|
||||
func (p *Parser) ParseFile(path string) (*Formula, error) {
|
||||
// Check cache first
|
||||
absPath, err := filepath.Abs(path)
|
||||
@@ -93,13 +86,7 @@ func (p *Parser) ParseFile(path string) (*Formula, error) {
|
||||
return nil, fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
|
||||
// Detect format from extension
|
||||
var formula *Formula
|
||||
if strings.HasSuffix(path, FormulaExtTOML) {
|
||||
formula, err = p.ParseTOML(data)
|
||||
} else {
|
||||
formula, err = p.Parse(data)
|
||||
}
|
||||
formula, err := p.Parse(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse %s: %w", path, err)
|
||||
}
|
||||
@@ -135,24 +122,6 @@ func (p *Parser) Parse(data []byte) (*Formula, error) {
|
||||
return &formula, nil
|
||||
}
|
||||
|
||||
// ParseTOML parses a formula from TOML bytes.
|
||||
func (p *Parser) ParseTOML(data []byte) (*Formula, error) {
|
||||
var formula Formula
|
||||
if err := toml.Unmarshal(data, &formula); err != nil {
|
||||
return nil, fmt.Errorf("toml: %w", err)
|
||||
}
|
||||
|
||||
// Set defaults
|
||||
if formula.Version == 0 {
|
||||
formula.Version = 1
|
||||
}
|
||||
if formula.Type == "" {
|
||||
formula.Type = TypeWorkflow
|
||||
}
|
||||
|
||||
return &formula, nil
|
||||
}
|
||||
|
||||
// Resolve fully resolves a formula, processing extends and expansions.
|
||||
// Returns a new formula with all inheritance applied.
|
||||
func (p *Parser) Resolve(formula *Formula) (*Formula, error) {
|
||||
@@ -236,21 +205,18 @@ func (p *Parser) Resolve(formula *Formula) (*Formula, error) {
|
||||
}
|
||||
|
||||
// loadFormula loads a formula by name from search paths.
|
||||
// Tries TOML first (.formula.toml), then falls back to JSON (.formula.json).
|
||||
func (p *Parser) loadFormula(name string) (*Formula, error) {
|
||||
// Check cache first
|
||||
if cached, ok := p.cache[name]; ok {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
// Search for the formula file - try TOML first, then JSON
|
||||
extensions := []string{FormulaExtTOML, FormulaExtJSON}
|
||||
// Search for the formula file
|
||||
filename := name + FormulaExt
|
||||
for _, dir := range p.searchPaths {
|
||||
for _, ext := range extensions {
|
||||
path := filepath.Join(dir, name+ext)
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return p.ParseFile(path)
|
||||
}
|
||||
path := filepath.Join(dir, filename)
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return p.ParseFile(path)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user