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