Skip to content

Instantly share code, notes, and snippets.

@SkYNewZ
Created February 21, 2019 18:30
Show Gist options
  • Save SkYNewZ/37dd08bfcdf026de57d2eaeb83369ea2 to your computer and use it in GitHub Desktop.
Save SkYNewZ/37dd08bfcdf026de57d2eaeb83369ea2 to your computer and use it in GitHub Desktop.
Simple script written in Golang to extract all variables of a given .tf file
package main
import (
"regexp"
"fmt"
"io/ioutil"
"os"
)
func main() {
// Get file path
filePath := os.Args[1]
// Open file
bytes, err := ioutil.ReadFile(filePath)
// if error while openning
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// get content as string
content := string(bytes)
re, _ := regexp.Compile(`(?m)variable "([a-zA-Z_]+)"`)
for _, match := range re.FindAllStringSubmatch(content, -1) {
fmt.Println(match[1] + " =")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment