mirror of
https://github.com/glanceapp/glance.git
synced 2025-06-21 18:31:24 +02:00
Add support for nested includes
This commit is contained in:
parent
9df9673e84
commit
19a89645a1
@ -147,18 +147,24 @@ func formatWidgetInitError(err error, w widget) error {
|
|||||||
var includePattern = regexp.MustCompile(`(?m)^([ \t]*)(?:-[ \t]*)?(?:!|\$)include:[ \t]*(.+)$`)
|
var includePattern = regexp.MustCompile(`(?m)^([ \t]*)(?:-[ \t]*)?(?:!|\$)include:[ \t]*(.+)$`)
|
||||||
|
|
||||||
func parseYAMLIncludes(mainFilePath string) ([]byte, map[string]struct{}, error) {
|
func parseYAMLIncludes(mainFilePath string) ([]byte, map[string]struct{}, error) {
|
||||||
|
return recursiveParseYAMLIncludes(mainFilePath, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func recursiveParseYAMLIncludes(mainFilePath string, includes map[string]struct{}) ([]byte, map[string]struct{}, error) {
|
||||||
mainFileContents, err := os.ReadFile(mainFilePath)
|
mainFileContents, err := os.ReadFile(mainFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("reading main YAML file: %w", err)
|
return nil, nil, fmt.Errorf("reading %s: %w", mainFilePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mainFileAbsPath, err := filepath.Abs(mainFilePath)
|
mainFileAbsPath, err := filepath.Abs(mainFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("getting absolute path of main YAML file: %w", err)
|
return nil, nil, fmt.Errorf("getting absolute path of %s: %w", mainFilePath, err)
|
||||||
}
|
}
|
||||||
mainFileDir := filepath.Dir(mainFileAbsPath)
|
mainFileDir := filepath.Dir(mainFileAbsPath)
|
||||||
|
|
||||||
includes := make(map[string]struct{})
|
if includes == nil {
|
||||||
|
includes = make(map[string]struct{})
|
||||||
|
}
|
||||||
var includesLastErr error
|
var includesLastErr error
|
||||||
|
|
||||||
mainFileContents = includePattern.ReplaceAllFunc(mainFileContents, func(match []byte) []byte {
|
mainFileContents = includePattern.ReplaceAllFunc(mainFileContents, func(match []byte) []byte {
|
||||||
@ -181,13 +187,14 @@ func parseYAMLIncludes(mainFilePath string) ([]byte, map[string]struct{}, error)
|
|||||||
var fileContents []byte
|
var fileContents []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
fileContents, err = os.ReadFile(includeFilePath)
|
includes[includeFilePath] = struct{}{}
|
||||||
|
|
||||||
|
fileContents, includes, err = recursiveParseYAMLIncludes(includeFilePath, includes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
includesLastErr = fmt.Errorf("reading included file %s: %w", includeFilePath, err)
|
includesLastErr = fmt.Errorf("recursively parsing included file %s: %w", includeFilePath, err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
includes[includeFilePath] = struct{}{}
|
|
||||||
return []byte(prefixStringLines(indent, string(fileContents)))
|
return []byte(prefixStringLines(indent, string(fileContents)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user