From 1f32833ec8d3d1a747325fdc787b0dc9eda1da96 Mon Sep 17 00:00:00 2001 From: Amir Abushareb Date: Sun, 12 Jun 2016 18:45:59 +0200 Subject: [PATCH] Initial commit --- main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..4bc754b --- /dev/null +++ b/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "path/filepath" + + "github.com/hashicorp/hcl" + "github.com/hashicorp/hcl/hcl/ast" + "github.com/tj/docopt" +) + +const version = "" +const usage = ` + Usage: tf-docs +` + +func main() { + args, err := docopt.Parse(usage, nil, true, version, false) + if err != nil { + log.Fatal(err) + } + + dir := args[""].(string) + names, err := filepath.Glob(fmt.Sprintf("%s/*.tf", dir)) + if err != nil { + log.Fatal(err) + } + + var values []Value + + for _, name := range names { + buf, err := ioutil.ReadFile(name) + if err != nil { + log.Fatal(err) + } + + f, err := hcl.ParseBytes(buf) + if err != nil { + log.Fatal(err) + } + + values = append(values, inputs(f)...) + } + + buf, err := json.MarshalIndent(values, "", " ") + if err != nil { + log.Fatal(err) + } + + fmt.Println(string(buf)) +} + +type Value struct { + Type string + Name string +} + +func inputs(f *ast.File) (ret []Value) { + list := f.Node.(*ast.ObjectList) + + for _, n := range list.Items { + if is(n.Keys, "variable") { + name := n.Keys[1].Token.Text + ret = append(ret, Value{ + Type: "variable", + Name: name[1 : len(name)-1], + }) + continue + } + + if is(n.Keys, "output") { + name := n.Keys[1].Token.Text + ret = append(ret, Value{ + Type: "output", + Name: name[1 : len(name)-1], + }) + continue + } + } + + return ret +} + +func is(keys []*ast.ObjectKey, t string) bool { + if len(keys) > 0 { + return keys[0].Token.Text == t + } + + return false +}