Skip to content

Commit 147ec04

Browse files
committed
colorized doc generating output
1 parent d8c793d commit 147ec04

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

gendoc/main.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"go/parser"
66
"go/token"
7-
"log"
87
"os"
98
"path/filepath"
109
"reflect"
@@ -14,6 +13,7 @@ import (
1413
"strings"
1514
"text/template"
1615

16+
"github.com/fatih/color"
1717
"github.com/hashicorp/hcl2/hclwrite"
1818
"github.com/hashicorp/terraform/helper/schema"
1919
cloud "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud"
@@ -37,7 +37,7 @@ func main() {
3737

3838
fname, _ := vProvider.FileLine(0)
3939
fpath := filepath.Dir(fname)
40-
log.Printf("generating doc from: %s\n", fpath)
40+
Message("generating doc from: %s\n", fpath)
4141

4242
// document for DataSources
4343
for k, v := range provider.DataSourcesMap {
@@ -68,16 +68,16 @@ func genIdx(fpath string) {
6868
sources := []Index{}
6969

7070
fname := "provider.go"
71-
log.Printf("[START]get description from file: %s\n", fname)
71+
Message("[START]get description from file: %s\n", fname)
7272
description, err := getFileDescription(fmt.Sprintf("%s/%s", fpath, fname))
7373
if err != nil {
74-
log.Printf("[SKIP!]get description failed, skip: %s", err)
74+
Message("[SKIP!]get description failed, skip: %s", err)
7575
return
7676
}
7777

7878
description = strings.TrimSpace(description)
7979
if description == "" {
80-
log.Printf("[SKIP!]description empty, skip: %s\n", fname)
80+
Message("[SKIP!]description empty, skip: %s\n", fname)
8181
return
8282
}
8383

@@ -86,7 +86,7 @@ func genIdx(fpath string) {
8686
resources = strings.TrimSpace(description[pos+16:])
8787
// description = strings.TrimSpace(description[:pos])
8888
} else {
89-
log.Printf("[SKIP!]resource list missing, skip: %s\n", fname)
89+
Message("[SKIP!]resource list missing, skip: %s\n", fname)
9090
return
9191
}
9292

@@ -98,7 +98,7 @@ func genIdx(fpath string) {
9898
}
9999
if strings.HasPrefix(v, " ") {
100100
if index.Name == "" {
101-
log.Printf("[FAIL!]no resource name found: %s", v)
101+
Message("[FAIL!]no resource name found: %s", v)
102102
return
103103
}
104104
index.Resources = append(index.Resources, []string{vv, vv[len(cloudMark)+1:]})
@@ -145,19 +145,19 @@ func genIdx(fpath string) {
145145
fname = fmt.Sprintf("%s/../%s.erb", docRoot, cloudMark)
146146
fd, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
147147
if err != nil {
148-
log.Printf("[FAIL!]open file %s failed: %s", fname, err)
148+
Message("[FAIL!]open file %s failed: %s", fname, err)
149149
os.Exit(1)
150150
}
151151

152152
defer fd.Close()
153153
t := template.Must(template.New("t").Parse(idxTPL))
154154
err = t.Execute(fd, data)
155155
if err != nil {
156-
log.Printf("[FAIL!]write file %s failed: %s", fname, err)
156+
Message("[FAIL!]write file %s failed: %s", fname, err)
157157
os.Exit(1)
158158
}
159159

160-
log.Printf("[SUCC.]write doc to file success: %s", fname)
160+
Message("[SUCC.]write doc to file success: %s", fname)
161161
}
162162

163163
// genDoc generating doc for data source and resource
@@ -175,17 +175,17 @@ func genDoc(dtype, fpath, name string, resource *schema.Resource) {
175175
}
176176

177177
fname := fmt.Sprintf("%s_%s_%s.go", dtype, cloudMarkShort, data["resource"])
178-
log.Printf("[START]get description from file: %s\n", fname)
178+
Message("[START]get description from file: %s\n", fname)
179179

180180
description, err := getFileDescription(fmt.Sprintf("%s/%s", fpath, fname))
181181
if err != nil {
182-
log.Printf("[FAIL!]get description failed: %s", err)
182+
Message("[FAIL!]get description failed: %s", err)
183183
os.Exit(1)
184184
}
185185

186186
description = strings.TrimSpace(description)
187187
if description == "" {
188-
log.Printf("[FAIL!]description empty: %s\n", fname)
188+
Message("[FAIL!]description empty: %s\n", fname)
189189
os.Exit(1)
190190
}
191191

@@ -200,7 +200,7 @@ func genDoc(dtype, fpath, name string, resource *schema.Resource) {
200200
data["example"] = formatHCL(description[pos+15:])
201201
description = strings.TrimSpace(description[:pos])
202202
} else {
203-
log.Printf("[FAIL!]example usage missing: %s\n", fname)
203+
Message("[FAIL!]example usage missing: %s\n", fname)
204204
os.Exit(1)
205205
}
206206

@@ -219,17 +219,17 @@ func genDoc(dtype, fpath, name string, resource *schema.Resource) {
219219

220220
for k, v := range resource.Schema {
221221
if v.Description == "" {
222-
log.Printf("[FAIL!]description for '%s' is missing: %s\n", k, fname)
222+
Message("[FAIL!]description for '%s' is missing: %s\n", k, fname)
223223
os.Exit(1)
224224
} else {
225225
checkDescription(k, v.Description)
226226
}
227227
if dtype == "data_source" && v.ForceNew {
228-
log.Printf("[FAIL!]Don't set ForceNew on data source: '%s'", k)
228+
Message("[FAIL!]Don't set ForceNew on data source: '%s'", k)
229229
os.Exit(1)
230230
}
231231
if v.Required && v.Optional {
232-
log.Printf("[FAIL!]Don't set Required and Optional at the same time: '%s'", k)
232+
Message("[FAIL!]Don't set Required and Optional at the same time: '%s'", k)
233233
os.Exit(1)
234234
}
235235
if v.Required {
@@ -290,19 +290,19 @@ func genDoc(dtype, fpath, name string, resource *schema.Resource) {
290290
fname = fmt.Sprintf("%s/%s/%s.html.markdown", docRoot, dtype[0:1], data["resource"])
291291
fd, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
292292
if err != nil {
293-
log.Printf("[FAIL!]open file %s failed: %s", fname, err)
293+
Message("[FAIL!]open file %s failed: %s", fname, err)
294294
os.Exit(1)
295295
}
296296

297297
defer fd.Close()
298298
t := template.Must(template.New("t").Parse(docTPL))
299299
err = t.Execute(fd, data)
300300
if err != nil {
301-
log.Printf("[FAIL!]write file %s failed: %s", fname, err)
301+
Message("[FAIL!]write file %s failed: %s", fname, err)
302302
os.Exit(1)
303303
}
304304

305-
log.Printf("[SUCC.]write doc to file success: %s", fname)
305+
Message("[SUCC.]write doc to file success: %s", fname)
306306
}
307307

308308
// getAttributes get attributes from schema
@@ -427,22 +427,22 @@ func checkDescription(k, s string) {
427427
}
428428

429429
if strings.TrimLeft(s, " ") != s {
430-
log.Printf("[FAIL!]There is space on the left of description: '%s': '%s'", k, s)
430+
Message("[FAIL!]There is space on the left of description: '%s': '%s'", k, s)
431431
os.Exit(1)
432432
}
433433

434434
if strings.TrimRight(s, " ") != s {
435-
log.Printf("[FAIL!]There is space on the right of description: '%s': '%s'", k, s)
435+
Message("[FAIL!]There is space on the right of description: '%s': '%s'", k, s)
436436
os.Exit(1)
437437
}
438438

439439
if s[len(s)-1] != '.' && s[len(s)-1] != ':' {
440-
log.Printf("[FAIL!]There is no ending charset(.|:) on the description: '%s': '%s'", k, s)
440+
Message("[FAIL!]There is no ending charset(.|:) on the description: '%s': '%s'", k, s)
441441
os.Exit(1)
442442
}
443443

444444
if c := ContainsBigSymbol(s); c != "" {
445-
log.Printf("[FAIL!]There is unexcepted symbol: '%s' on the description: '%s': '%s'", c, k, s)
445+
Message("[FAIL!]There is unexcepted symbol: '%s' on the description: '%s': '%s'", c, k, s)
446446
os.Exit(1)
447447
}
448448
}
@@ -456,3 +456,16 @@ func ContainsBigSymbol(s string) string {
456456

457457
return ""
458458
}
459+
460+
// Message print color message
461+
func Message(msg string, v ...interface{}) {
462+
if strings.Contains(msg, "FAIL") {
463+
color.Red(fmt.Sprintf(msg, v...))
464+
} else if strings.Contains(msg, "SUCC") {
465+
color.Green(fmt.Sprintf(msg, v...))
466+
} else if strings.Contains(msg, "SKIP") {
467+
color.Yellow(fmt.Sprintf(msg, v...))
468+
} else {
469+
color.White(fmt.Sprintf(msg, v...))
470+
}
471+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/aws/aws-sdk-go v1.25.21
88
github.com/bflad/tfproviderlint v0.5.0
99
github.com/client9/misspell v0.3.4
10+
github.com/fatih/color v1.7.0
1011
github.com/golangci/golangci-lint v1.21.0
1112
github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80
1213
github.com/hashicorp/terraform v0.12.12

0 commit comments

Comments
 (0)