Skip to content

Commit

Permalink
tools/docgen: implement plugin documentation generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dobarx committed Feb 12, 2024
1 parent 45dac35 commit 002ae1a
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.1
github.com/sanity-io/litter v1.5.5
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.27.0
github.com/testcontainers/testcontainers-go/modules/elasticsearch v0.27.0
Expand Down Expand Up @@ -82,7 +83,6 @@ require (
github.com/shirou/gopsutil/v3 v3.23.11 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion internal/builtin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func Plugin(version string) *plugin.Schema {
return &plugin.Schema{
Name: "builtin",
Name: "blackstork/builtin",
Version: version,
DataSources: plugin.DataSources{
"csv": makeCSVDataSource(),
Expand Down
2 changes: 1 addition & 1 deletion internal/builtin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestPluginSchema(t *testing.T) {
schema := Plugin("1.2.3")
assert.Equal(t, "builtin", schema.Name)
assert.Equal(t, "blackstork/builtin", schema.Name)
assert.Equal(t, "1.2.3", schema.Version)
assert.NotNil(t, schema.DataSources["csv"])
assert.NotNil(t, schema.DataSources["txt"])
Expand Down
5 changes: 4 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ test-all:
go test -timeout 5m -race -v ./...

generate:
go generate ./...
go generate ./...

generate-docs:
go run ./tools/docgen --version v0.0.0-dev --output ./dist/docs
95 changes: 95 additions & 0 deletions tools/docgen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
_ "embed"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/hashicorp/hcl/v2/hcldec"
"github.com/spf13/pflag"

"github.com/blackstork-io/fabric/internal/builtin"
"github.com/blackstork-io/fabric/internal/elasticsearch"
"github.com/blackstork-io/fabric/internal/github"
"github.com/blackstork-io/fabric/internal/graphql"
"github.com/blackstork-io/fabric/internal/openai"
"github.com/blackstork-io/fabric/internal/postgresql"
"github.com/blackstork-io/fabric/internal/sqlite"
"github.com/blackstork-io/fabric/internal/terraform"
"github.com/blackstork-io/fabric/plugin"
)

var (
version string
outputDir string
)

//go:embed markdown.gotempl
var markdownTempl string

var templ *template.Template

func main() {
// parse flags
flags := pflag.NewFlagSet("docgen", pflag.ExitOnError)
flags.StringVar(&version, "version", "v0.0.0-dev", "version of the build")
flags.StringVar(&outputDir, "output", "./dist/docs", "output directory")
flags.Parse(os.Args[1:])
// ensure output directory exists
if err := os.MkdirAll(outputDir, 0o755); err != nil {
panic(err)
}
// load all plugins
plugins := []*plugin.Schema{
builtin.Plugin(version),
elasticsearch.Plugin(version),
github.Plugin(version, nil),
graphql.Plugin(version),
openai.Plugin(version, nil),
postgresql.Plugin(version),
sqlite.Plugin(version),
terraform.Plugin(version),
}
// generate markdown for each plugin
for _, p := range plugins {
fp := filepath.Join(outputDir, fmt.Sprintf("%s.md", shortname(p.Name)))
fmt.Printf("Generating '%s': '%s'\n", p.Name, fp)
if err := generate(p, fp); err != nil {
panic(err)
}
}
}

func generate(schema *plugin.Schema, fp string) error {
f, err := os.Create(fp)
if err != nil {
return err
}
defer f.Close()
return templ.Execute(f, schema)
}

func shortname(name string) string {
parts := strings.SplitN(name, "/", 2)
if len(parts) == 2 {
return parts[1]
}
return name
}

func init() {
templ = template.Must(template.New("markdown").Funcs(template.FuncMap{
"shortname": shortname,
"attrType": func(val hcldec.Spec) string {
switch v := val.(type) {
case *hcldec.AttrSpec:
return v.Type.FriendlyName()
default:
return "unknown"
}
},
}).Parse(markdownTempl))
}
101 changes: 101 additions & 0 deletions tools/docgen/markdown.gotempl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{{- if eq .Name "blackstork/builtin" -}}
# Fabric built-in plugins
{{- else -}}
# Fabric `{{ .Name | shortname }}` plugin
{{- end }}

This document describes the `{{ .Name | shortname }}` plugin for the Fabric CLI tool.

## Installation

{{ if eq .Name "blackstork/builtin" -}}
The built-in plugins are available by default.
{{ else -}}
To install the plugin, add the following to your global fabric configuration:

```hcl
fabric {
plugin_versions = {
"{{ .Name }}" = "=> {{ .Version }}"
}
}
```
{{ end }}

{{ with .DataSources -}}

## Data sources

{{ if eq .Name "blackstork/builtin" -}}
The following data sources are available by default:
{{ else -}}
This plugin has the following data sources available:
{{ end }}
{{ range $name, $ds := . -}}
### `{{ $name }}`

{{with $ds.Config -}}
#### Configuration

```hcl
config data {{ $name }} {
{{- range $key, $value := . }}
{{ $key }} = {{ $value | attrType }} // {{if $value.Required}}required{{else}}optional{{end}}
{{- end }}
}
```
{{ end }}

{{with $ds.Args -}}
#### Usage

```hcl
data {{ $name }} {
{{- range $key, $value := . }}
{{ $key }} = {{ $value | attrType }} // {{if $value.Required}}required{{else}}optional{{end}}
{{- end }}
}
```
{{ end }}
{{ end }}
{{ end }}


{{ with .ContentProviders -}}

## Content providers

{{ if eq .Name "blackstork/builtin" -}}
The following content providers are available by default:
{{ else -}}
This plugin has the following content providers available:
{{ end }}
{{ range $name, $provider := . -}}
### `{{ $name }}`

{{with $provider.Config -}}
#### Configuration

```hcl
config content {{ $name }} {
{{- range $key, $value := . }}
{{ $key }} = {{ $value | attrType }} // {{if $value.Required}}required{{else}}optional{{end}}
{{- end }}
}
```
{{ end }}

{{with $provider.Args -}}
#### Usage

```hcl
content {{ $name }} {
{{- range $key, $value := . }}
{{ $key }} = {{ $value | attrType }} // {{if $value.Required}}required{{else}}optional{{end}}
{{- end }}
}
```
{{ end }}
{{ end }}
{{ end }}

0 comments on commit 002ae1a

Please sign in to comment.