4
4
"fmt"
5
5
"go/parser"
6
6
"go/token"
7
- "log"
8
7
"os"
9
8
"path/filepath"
10
9
"reflect"
@@ -14,6 +13,7 @@ import (
14
13
"strings"
15
14
"text/template"
16
15
16
+ "github.com/fatih/color"
17
17
"github.com/hashicorp/hcl2/hclwrite"
18
18
"github.com/hashicorp/terraform/helper/schema"
19
19
cloud "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud"
@@ -37,7 +37,7 @@ func main() {
37
37
38
38
fname , _ := vProvider .FileLine (0 )
39
39
fpath := filepath .Dir (fname )
40
- log . Printf ("generating doc from: %s\n " , fpath )
40
+ Message ("generating doc from: %s\n " , fpath )
41
41
42
42
// document for DataSources
43
43
for k , v := range provider .DataSourcesMap {
@@ -68,16 +68,16 @@ func genIdx(fpath string) {
68
68
sources := []Index {}
69
69
70
70
fname := "provider.go"
71
- log . Printf ("[START]get description from file: %s\n " , fname )
71
+ Message ("[START]get description from file: %s\n " , fname )
72
72
description , err := getFileDescription (fmt .Sprintf ("%s/%s" , fpath , fname ))
73
73
if err != nil {
74
- log . Printf ("[SKIP!]get description failed, skip: %s" , err )
74
+ Message ("[SKIP!]get description failed, skip: %s" , err )
75
75
return
76
76
}
77
77
78
78
description = strings .TrimSpace (description )
79
79
if description == "" {
80
- log . Printf ("[SKIP!]description empty, skip: %s\n " , fname )
80
+ Message ("[SKIP!]description empty, skip: %s\n " , fname )
81
81
return
82
82
}
83
83
@@ -86,7 +86,7 @@ func genIdx(fpath string) {
86
86
resources = strings .TrimSpace (description [pos + 16 :])
87
87
// description = strings.TrimSpace(description[:pos])
88
88
} else {
89
- log . Printf ("[SKIP!]resource list missing, skip: %s\n " , fname )
89
+ Message ("[SKIP!]resource list missing, skip: %s\n " , fname )
90
90
return
91
91
}
92
92
@@ -98,7 +98,7 @@ func genIdx(fpath string) {
98
98
}
99
99
if strings .HasPrefix (v , " " ) {
100
100
if index .Name == "" {
101
- log . Printf ("[FAIL!]no resource name found: %s" , v )
101
+ Message ("[FAIL!]no resource name found: %s" , v )
102
102
return
103
103
}
104
104
index .Resources = append (index .Resources , []string {vv , vv [len (cloudMark )+ 1 :]})
@@ -145,19 +145,19 @@ func genIdx(fpath string) {
145
145
fname = fmt .Sprintf ("%s/../%s.erb" , docRoot , cloudMark )
146
146
fd , err := os .OpenFile (fname , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0644 )
147
147
if err != nil {
148
- log . Printf ("[FAIL!]open file %s failed: %s" , fname , err )
148
+ Message ("[FAIL!]open file %s failed: %s" , fname , err )
149
149
os .Exit (1 )
150
150
}
151
151
152
152
defer fd .Close ()
153
153
t := template .Must (template .New ("t" ).Parse (idxTPL ))
154
154
err = t .Execute (fd , data )
155
155
if err != nil {
156
- log . Printf ("[FAIL!]write file %s failed: %s" , fname , err )
156
+ Message ("[FAIL!]write file %s failed: %s" , fname , err )
157
157
os .Exit (1 )
158
158
}
159
159
160
- log . Printf ("[SUCC.]write doc to file success: %s" , fname )
160
+ Message ("[SUCC.]write doc to file success: %s" , fname )
161
161
}
162
162
163
163
// genDoc generating doc for data source and resource
@@ -175,17 +175,17 @@ func genDoc(dtype, fpath, name string, resource *schema.Resource) {
175
175
}
176
176
177
177
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 )
179
179
180
180
description , err := getFileDescription (fmt .Sprintf ("%s/%s" , fpath , fname ))
181
181
if err != nil {
182
- log . Printf ("[FAIL!]get description failed: %s" , err )
182
+ Message ("[FAIL!]get description failed: %s" , err )
183
183
os .Exit (1 )
184
184
}
185
185
186
186
description = strings .TrimSpace (description )
187
187
if description == "" {
188
- log . Printf ("[FAIL!]description empty: %s\n " , fname )
188
+ Message ("[FAIL!]description empty: %s\n " , fname )
189
189
os .Exit (1 )
190
190
}
191
191
@@ -200,7 +200,7 @@ func genDoc(dtype, fpath, name string, resource *schema.Resource) {
200
200
data ["example" ] = formatHCL (description [pos + 15 :])
201
201
description = strings .TrimSpace (description [:pos ])
202
202
} else {
203
- log . Printf ("[FAIL!]example usage missing: %s\n " , fname )
203
+ Message ("[FAIL!]example usage missing: %s\n " , fname )
204
204
os .Exit (1 )
205
205
}
206
206
@@ -219,17 +219,17 @@ func genDoc(dtype, fpath, name string, resource *schema.Resource) {
219
219
220
220
for k , v := range resource .Schema {
221
221
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 )
223
223
os .Exit (1 )
224
224
} else {
225
225
checkDescription (k , v .Description )
226
226
}
227
227
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 )
229
229
os .Exit (1 )
230
230
}
231
231
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 )
233
233
os .Exit (1 )
234
234
}
235
235
if v .Required {
@@ -290,19 +290,19 @@ func genDoc(dtype, fpath, name string, resource *schema.Resource) {
290
290
fname = fmt .Sprintf ("%s/%s/%s.html.markdown" , docRoot , dtype [0 :1 ], data ["resource" ])
291
291
fd , err := os .OpenFile (fname , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0644 )
292
292
if err != nil {
293
- log . Printf ("[FAIL!]open file %s failed: %s" , fname , err )
293
+ Message ("[FAIL!]open file %s failed: %s" , fname , err )
294
294
os .Exit (1 )
295
295
}
296
296
297
297
defer fd .Close ()
298
298
t := template .Must (template .New ("t" ).Parse (docTPL ))
299
299
err = t .Execute (fd , data )
300
300
if err != nil {
301
- log . Printf ("[FAIL!]write file %s failed: %s" , fname , err )
301
+ Message ("[FAIL!]write file %s failed: %s" , fname , err )
302
302
os .Exit (1 )
303
303
}
304
304
305
- log . Printf ("[SUCC.]write doc to file success: %s" , fname )
305
+ Message ("[SUCC.]write doc to file success: %s" , fname )
306
306
}
307
307
308
308
// getAttributes get attributes from schema
@@ -427,22 +427,22 @@ func checkDescription(k, s string) {
427
427
}
428
428
429
429
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 )
431
431
os .Exit (1 )
432
432
}
433
433
434
434
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 )
436
436
os .Exit (1 )
437
437
}
438
438
439
439
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 )
441
441
os .Exit (1 )
442
442
}
443
443
444
444
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 )
446
446
os .Exit (1 )
447
447
}
448
448
}
@@ -456,3 +456,16 @@ func ContainsBigSymbol(s string) string {
456
456
457
457
return ""
458
458
}
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
+ }
0 commit comments