2019-05-28 09:24:58 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-28 11:50:58 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2019-05-28 09:24:58 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gocolly/colly"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-05-28 10:47:28 -04:00
|
|
|
DocsUrl = "http://127.0.0.1:4567/docs/"
|
2019-05-28 09:24:58 -04:00
|
|
|
CacheDir = "cache/"
|
|
|
|
)
|
|
|
|
|
2019-05-28 11:50:58 -04:00
|
|
|
func commentize(text string) string {
|
|
|
|
text = text[strings.Index(text, ") -")+len(") -"):]
|
|
|
|
text = strings.ReplaceAll(text, "\n", "\n // ")
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
2019-05-28 09:24:58 -04:00
|
|
|
func main() {
|
|
|
|
c := colly.NewCollector()
|
|
|
|
|
|
|
|
// Find and visit all doc pages
|
|
|
|
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
|
|
|
|
url := e.Attr("href")
|
|
|
|
if !strings.HasPrefix(url, "/docs/builders") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e.Request.Visit(url)
|
|
|
|
})
|
2019-05-28 11:50:58 -04:00
|
|
|
for _, required := range []bool{true, false} {
|
|
|
|
required := required
|
|
|
|
sel := "#required- + ul a[name]"
|
|
|
|
if !required {
|
|
|
|
sel = "#optional- + ul a[name]"
|
|
|
|
}
|
|
|
|
c.OnHTML(sel, func(e *colly.HTMLElement) {
|
2019-05-28 09:24:58 -04:00
|
|
|
|
2019-05-28 11:50:58 -04:00
|
|
|
name := e.Attr("name")
|
2019-05-28 09:24:58 -04:00
|
|
|
|
2019-05-28 11:50:58 -04:00
|
|
|
builder := e.Request.URL.Path[strings.Index(e.Request.URL.Path, "/builders/")+len("/builders/"):]
|
|
|
|
builder = strings.TrimSuffix(builder, ".html")
|
2019-05-28 09:24:58 -04:00
|
|
|
|
2019-05-28 11:50:58 -04:00
|
|
|
fieldDoc := e.DOM.Parent()
|
|
|
|
text := fieldDoc.Text()
|
2019-05-28 09:24:58 -04:00
|
|
|
|
2019-05-28 11:50:58 -04:00
|
|
|
builderPath := strings.Split(builder, "-")[0]
|
|
|
|
if name == "vpc_filter" {
|
|
|
|
fmt.Printf("required: %25s builderPath: %20s text: %20s\n", name, builderPath, text)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := filepath.Walk("./builder/"+builderPath, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil || info.IsDir() || filepath.Ext(path) != ".go" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
regex := regexp.MustCompile(fmt.Sprintf(`(\n\s+//.*)*\n+(\s*)([A-Z].*mapstructure:"%s")(\s+required:"%t")?(.*)`, name, required))
|
|
|
|
|
|
|
|
replaced := regex.ReplaceAll(body, []byte(fmt.Sprintf("\n$2//%s\n"+`$2$3 required:"%t"$5`, commentize(text), required)))
|
|
|
|
|
|
|
|
if string(replaced) == string(body) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(path, replaced, 0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-05-28 09:24:58 -04:00
|
|
|
|
|
|
|
c.CacheDir = CacheDir
|
|
|
|
|
|
|
|
c.Visit(DocsUrl)
|
|
|
|
}
|