From 5cae48f458b985aeec1ebee9feb3811274128fad Mon Sep 17 00:00:00 2001 From: Amir Abushareb Date: Wed, 15 Jun 2016 14:50:45 +0200 Subject: [PATCH] use /** comment for module commnet terraform doesnt like multiline // comments i guess --- _example/main.tf | 17 +++++++++-------- doc/doc.go | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/_example/main.tf b/_example/main.tf index ba4a487..651ccb9 100644 --- a/_example/main.tf +++ b/_example/main.tf @@ -1,11 +1,12 @@ -// -// Module usage: -// -// module "foo" { -// source = "github.com/foo/baz" -// subnet_ids = "${join(",", subnet.*.id)}" -// } -// +/** + * Module usage: + * + * module "foo" { + * source = "github.com/foo/baz" + * subnet_ids = "${join(",", subnet.*.id)}" + * } + * + */ variable "subnet_ids" { description = "a comma-separated list of subnet IDs" diff --git a/doc/doc.go b/doc/doc.go index 5657d74..cc57e47 100644 --- a/doc/doc.go +++ b/doc/doc.go @@ -42,15 +42,7 @@ func Create(files map[string]*ast.File) *Doc { comments := f.Comments if filename == "main.tf" && len(comments) > 0 { - if c := comments[0]; c.Pos().Line == 1 { - for _, item := range c.List { - if !strings.HasPrefix(item.Text, "//") { - break - } - - doc.Comment += strings.TrimPrefix(item.Text, "//") + "\n" - } - } + doc.Comment = header(comments[0]) } } @@ -144,3 +136,35 @@ func comment(l []*ast.Comment) string { return ret } + +// Header returns the header comment from the list +// or an empty comment. The head comment must start +// at line 1 and start with `/**`. +func header(c *ast.CommentGroup) (comment string) { + if len(c.List) == 0 { + return comment + } + + if c.Pos().Line != 1 { + return comment + } + + cm := strings.TrimSpace(c.List[0].Text) + + if strings.HasPrefix(cm, "/**") { + lines := strings.Split(cm, "\n") + + if len(lines) < 2 { + return comment + } + + lines = lines[1 : len(lines)-1] + for _, l := range lines { + l = strings.TrimSpace(l) + l = strings.TrimPrefix(l, "*") + comment += l + "\n" + } + } + + return comment +}