dir2config: introduce #!gen

This commit is contained in:
Mikaël Cluseau 2024-11-05 11:53:17 +01:00
parent edbe1641fd
commit ce8b7f01ef

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"log" "log"
"os" "os"
"os/exec"
"strings" "strings"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -116,9 +117,26 @@ func eachFragment(path string, searchList []FS, walk func(io.Reader) error) (err
continue continue
} }
genCmd, found := strings.CutPrefix(line, "#!gen ")
if found {
cmdArgs := strings.Fields(genCmd)
if Debug {
log.Print("#!gen ", cmdArgs)
}
cmd := "gen/" + cmdArgs[0]
args := cmdArgs[1:]
genOutput, err := exec.Command(cmd, args...).Output()
if err != nil {
return fmt.Errorf("gen %v: %w", cmdArgs, err)
}
walk(bytes.NewBuffer(genOutput))
continue
}
includePath, found := strings.CutPrefix(line, "#!include ") includePath, found := strings.CutPrefix(line, "#!include ")
if !found { if !found {
continue // or break? continue
} }
includePath = strings.TrimSpace(includePath) includePath = strings.TrimSpace(includePath)
@ -127,8 +145,7 @@ func eachFragment(path string, searchList []FS, walk func(io.Reader) error) (err
} }
err = eachFragment(includePath, searchList, walk) err = eachFragment(includePath, searchList, walk)
if err != nil { if err != nil {
err = fmt.Errorf("include %q: %w", includePath, err) return fmt.Errorf("include %q: %w", includePath, err)
return
} }
} }