Created
July 25, 2021 07:56
-
-
Save aaronjwood/2be08ced7e43c23900cb86785acdc897 to your computer and use it in GitHub Desktop.
Manifest parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var separator = regexp.MustCompile(`(?m)^---$`) | |
var commentStart = regexp.MustCompile(`(?m)^#.*$`) | |
// ParseDocuments parses a collection of YAML documents out of a single string. | |
func ParseDocuments(data string) []string { | |
docs := make([]string, 0) | |
if data == "" { | |
return docs | |
} | |
append := func(doc string) { | |
if trimmedDoc := strings.TrimSpace(doc); trimmedDoc != "" { | |
docs = append(docs, trimmedDoc) | |
} | |
} | |
data = commentStart.ReplaceAllString(data, "") | |
matches := separator.FindAllStringIndex(data, -1) | |
var previous int = 0 | |
for _, match := range matches { | |
append(data[previous:match[0]]) | |
previous = match[1] | |
} | |
append(data[previous:]) | |
return docs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment