From ce8b7f01efd7fa19758e3cfc522ada7a52ab68b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Tue, 5 Nov 2024 11:53:17 +0100 Subject: [PATCH] dir2config: introduce #!gen --- cmd/dkl-dir2config/assemble.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/cmd/dkl-dir2config/assemble.go b/cmd/dkl-dir2config/assemble.go index 0c9f50a..12513fc 100644 --- a/cmd/dkl-dir2config/assemble.go +++ b/cmd/dkl-dir2config/assemble.go @@ -6,6 +6,7 @@ import ( "io" "log" "os" + "os/exec" "strings" "gopkg.in/yaml.v2" @@ -116,9 +117,26 @@ func eachFragment(path string, searchList []FS, walk func(io.Reader) error) (err 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 ") if !found { - continue // or break? + continue } 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) if err != nil { - err = fmt.Errorf("include %q: %w", includePath, err) - return + return fmt.Errorf("include %q: %w", includePath, err) } }