Created
October 25, 2017 14:02
-
-
Save kszarek/64efc292b9f39872915c0fded0a5c730 to your computer and use it in GitHub Desktop.
Read TF comment
This file contains 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
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