Skip to content

Instantly share code, notes, and snippets.

@kszarek
Created October 25, 2017 14:02
Show Gist options
  • Save kszarek/64efc292b9f39872915c0fded0a5c730 to your computer and use it in GitHub Desktop.
Save kszarek/64efc292b9f39872915c0fded0a5c730 to your computer and use it in GitHub Desktop.
Read TF comment
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
comment := false
for scanner.Scan() {
// skip empty lines
if len(scanner.Text()) < 2 {
continue
}
// end of command/comment
if scanner.Text()[:2] == "*/"[:2] {
comment = false
continue
}
if comment {
for _, word := range strings.Fields(scanner.Text()) {
if word != "\\" {
lines = append(lines, word)
}
}
continue
}
// lets read everything what started from `tf plan...`
if len(scanner.Text()) < 7 {
continue
}
if scanner.Text()[:7] == "tf plan"[:7] {
for _, word := range strings.Fields(scanner.Text()) {
if word != "\\" {
lines = append(lines, word)
}
}
comment = true
continue
}
}
return lines, scanner.Err()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment