|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "regexp" |
| 8 | + "text/template" |
| 9 | + "text/template/parse" |
| 10 | + |
| 11 | + "github.com/Masterminds/sprig" |
| 12 | + "github.com/banzaicloud/logging-operator/pkg/resources/plugins" |
| 13 | +) |
| 14 | + |
| 15 | +//TODO handle parameters |
| 16 | +func main() { |
| 17 | + pluginMap := plugins.GetAll() |
| 18 | + var indexPage bytes.Buffer |
| 19 | + indexPage.WriteString("# List of ") |
| 20 | + for name, plugin := range pluginMap { |
| 21 | + var data bytes.Buffer |
| 22 | + data.WriteString(fmt.Sprintf("# Plugin %s\n", name)) |
| 23 | + t := template.New("PluginTemplate").Funcs(sprig.TxtFuncMap()) |
| 24 | + t, err := t.Parse(plugin.Template) |
| 25 | + if err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | + data.WriteString("## Variables\n") |
| 29 | + data.WriteString("| Variable name | Default | Applied function |\n") |
| 30 | + data.WriteString(fmt.Sprintf("|---|---|---|\n")) |
| 31 | + for _, item := range listTemplateFields(t) { |
| 32 | + regExp, err := regexp.Compile(`{{(?P<Function>\w*)?\s*.(?P<Variable>.*)}}`) |
| 33 | + if err != nil { |
| 34 | + panic(err) |
| 35 | + } |
| 36 | + matches := regExp.FindStringSubmatch(item) |
| 37 | + vairableName := matches[2] |
| 38 | + variableFunc := matches[1] |
| 39 | + defaultValue, ok := plugin.DefaultValues[matches[2]] |
| 40 | + if !ok { |
| 41 | + defaultValue = "-" |
| 42 | + } |
| 43 | + data.WriteString(fmt.Sprintf("| %s | %s | %s |\n", vairableName, defaultValue, variableFunc)) |
| 44 | + |
| 45 | + } |
| 46 | + data.WriteString("## Plugin template\n") |
| 47 | + data.WriteString("```" + plugin.Template + "\n```") |
| 48 | + err = ioutil.WriteFile("docs/plugins/"+name+".md", data.Bytes(), 0644) |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func listTemplateFields(t *template.Template) []string { |
| 56 | + return listNodeFields(t.Tree.Root, nil) |
| 57 | +} |
| 58 | + |
| 59 | +func listNodeFields(node parse.Node, res []string) []string { |
| 60 | + if node.Type() == parse.NodeAction { |
| 61 | + res = append(res, node.String()) |
| 62 | + } |
| 63 | + |
| 64 | + if ln, ok := node.(*parse.ListNode); ok { |
| 65 | + for _, n := range ln.Nodes { |
| 66 | + res = listNodeFields(n, res) |
| 67 | + } |
| 68 | + } |
| 69 | + return res |
| 70 | +} |
0 commit comments