Skip to content

Commit

Permalink
generate the sidebar (#3414) (#6192)
Browse files Browse the repository at this point in the history
* generate the sidebar

* put back accidentally deleted lines

* whitespace

* empty commit

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Apr 23, 2020
1 parent a492a96 commit d3cf981
Show file tree
Hide file tree
Showing 4 changed files with 1,781 additions and 1,182 deletions.
3 changes: 3 additions & 0 deletions .changelog/3414.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none
* This release includes website changes to put data sources and resources in submenus in t he products they're part of.
```
112 changes: 112 additions & 0 deletions scripts/sidebar/sidebar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//go:generate go run sidebar.go
package main

import (
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"text/template"
)

type Entry struct {
Filename string
Product string
Resource string
}

type Entries struct {
Resources []Entry
DataSources []Entry
}

func main() {
_, scriptPath, _, ok := runtime.Caller(0)
if !ok {
log.Fatal("Could not get current working directory")
}
tpgDir := scriptPath
for !strings.HasPrefix(filepath.Base(tpgDir), "terraform-provider-") && tpgDir != "/" {
tpgDir = filepath.Clean(tpgDir + "/..")
}
if tpgDir == "/" {
log.Fatal("Script was run outside of google provider directory")
}

resourcesByProduct, err := entriesByProduct(tpgDir + "/website/docs/r")
if err != nil {
panic(err)
}
dataSourcesByProduct, err := entriesByProduct(tpgDir + "/website/docs/d")
if err != nil {
panic(err)
}
allEntriesByProduct := make(map[string]Entries)
for p, e := range resourcesByProduct {
v := allEntriesByProduct[p]
v.Resources = e
allEntriesByProduct[p] = v
}
for p, e := range dataSourcesByProduct {
v := allEntriesByProduct[p]
v.DataSources = e
allEntriesByProduct[p] = v
}

tmpl, err := template.ParseFiles(tpgDir + "/website/google.erb.tmpl")
if err != nil {
panic(err)
}
f, err := os.Create(tpgDir + "/website/google.erb")
if err != nil {
panic(err)
}
defer f.Close()
err = tmpl.Execute(f, allEntriesByProduct)
if err != nil {
panic(err)
}
}

func entriesByProduct(dir string) (map[string][]Entry, error) {
d, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}

entriesByProduct := make(map[string][]Entry)
for _, f := range d {
entry, err := getEntry(dir, f.Name())
if err != nil {
return nil, err
}
entriesByProduct[entry.Product] = append(entriesByProduct[entry.Product], entry)
}

return entriesByProduct, nil
}

func getEntry(dir, filename string) (Entry, error) {
file, err := ioutil.ReadFile(dir + "/" + filename)
if err != nil {
return Entry{}, err
}

return Entry{
Filename: strings.TrimSuffix(filename, ".markdown"),
Product: findRegex(file, `subcategory: "(.*)"`),
Resource: findRegex(file, `page_title: "Google: (.*)"`),
}, nil
}

func findRegex(contents []byte, regex string) string {
r := regexp.MustCompile(regex)
sm := r.FindStringSubmatch(string(contents))
if len(sm) > 1 {
return sm[1]
}
return ""
}
Loading

0 comments on commit d3cf981

Please sign in to comment.