-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
89 lines (73 loc) · 2.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/coder/terraform-provider-coder/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/xerrors"
)
// This script patches Markdown docs generated by `terraform-plugin-docs` to expose the original deprecation message.
const docsDir = "docs" // FIXME expose as flag?
var reDeprecatedProperty = regexp.MustCompile("`([^`]+)` \\(([^,\\)]+), Deprecated\\) ([^\n]+)")
func main() {
p := provider.New()
err := exposeDeprecationMessage(p)
if err != nil {
log.Fatal(err)
}
}
func exposeDeprecationMessage(p *schema.Provider) error {
// Patch data-sources
for dataSourceName, dataSource := range p.DataSourcesMap {
docFile := filepath.Join(docsDir, "data-sources", strings.Replace(dataSourceName, "coder_", "", 1)+".md")
err := adjustDocFile(docFile, dataSource.Schema)
if err != nil {
return xerrors.Errorf("unable to adjust data-source doc file (data-source: %s): %w", dataSourceName, err)
}
}
// Patch resources
for resourceName, resource := range p.ResourcesMap {
docFile := filepath.Join(docsDir, "resources", strings.Replace(resourceName, "coder_", "", 1)+".md")
err := adjustDocFile(docFile, resource.Schema)
if err != nil {
return xerrors.Errorf("unable to adjust resource doc file (resource: %s): %w", resourceName, err)
}
}
// Patch index
docFile := filepath.Join(docsDir, "index.md")
err := adjustDocFile(docFile, p.Schema)
if err != nil {
return xerrors.Errorf("unable to adjust index doc file: %w", err)
}
return nil
}
func adjustDocFile(docPath string, schemas map[string]*schema.Schema) error {
doc, err := os.ReadFile(docPath)
if err != nil {
return xerrors.Errorf("can't read the source doc file: %w", err)
}
result := writeDeprecationMessage(doc, schemas)
err = os.WriteFile(docPath, result, 0644)
if err != nil {
return xerrors.Errorf("can't write modified doc file: %w", err)
}
return nil
}
func writeDeprecationMessage(doc []byte, schemas map[string]*schema.Schema) []byte {
return reDeprecatedProperty.ReplaceAllFunc(doc, func(m []byte) []byte {
matches := reDeprecatedProperty.FindSubmatch(m)
propertyName := matches[1]
description := matches[3]
sch := schemas[string(propertyName)]
if string(description) != sch.Description {
log.Printf("warn: same property name `%s` but description does not match, most likely a different property", propertyName)
return m
}
return bytes.Replace(m, []byte("Deprecated"), []byte(fmt.Sprintf("Deprecated: %s", sch.Deprecated)), 1)
})
}