|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "regexp" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + changlogPath = "./terraform-provider-alicloud/CHANGELOG.md" |
| 13 | + providerDocsPath = "./terraform-provider-alicloud/website/docs/r/" |
| 14 | + quickstartsPath = "./quickstarts/" |
| 15 | + |
| 16 | + exampleStart = "```terraform" |
| 17 | + exampleEnd = "```" |
| 18 | + |
| 19 | + alicloudProvider = "terraform {\n" + |
| 20 | + " required_providers {\n" + |
| 21 | + " alicloud = {\n" + |
| 22 | + " source = \"aliyun/alicloud\"\n" + |
| 23 | + " }\n" + |
| 24 | + " }\n" + |
| 25 | + "}" |
| 26 | + |
| 27 | + header = "## Introduction\n\nThis example is used to create a `%s` resource.\n\n<!-- BEGIN_TF_DOCS -->\n\n<!-- END_TF_DOCS -->\n" |
| 28 | +) |
| 29 | + |
| 30 | +func main() { |
| 31 | + |
| 32 | + if len(os.Args) < 2 { |
| 33 | + fmt.Println("please input the provider version") |
| 34 | + os.Exit(1) |
| 35 | + } |
| 36 | + |
| 37 | + version := os.Args[1] |
| 38 | + newResource, err := getNewResource(version) |
| 39 | + if err != nil { |
| 40 | + fmt.Println(err) |
| 41 | + os.Exit(1) |
| 42 | + } |
| 43 | + |
| 44 | + resourceDocList := []string{} |
| 45 | + files, _ := os.ReadDir(providerDocsPath) |
| 46 | + for _, file := range files { |
| 47 | + if file.IsDir() { |
| 48 | + continue |
| 49 | + } else { |
| 50 | + resourceDocList = append(resourceDocList, file.Name()) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + for _, resourceDoc := range resourceDocList { |
| 55 | + doc, err := os.ReadFile(providerDocsPath + resourceDoc) |
| 56 | + if err != nil { |
| 57 | + fmt.Printf("fail to open file %s. Error: %s\n", resourceDoc, err) |
| 58 | + } |
| 59 | + docContent := string(doc) |
| 60 | + if strings.Contains(docContent, "**DEPRECATED:**") { |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + subcategoryIndex := strings.Index(docContent, "subcategory: ") + len("subcategory: ") |
| 65 | + subcategoryEndIndex := strings.Index(docContent[subcategoryIndex:], "\n") |
| 66 | + subcategory := docContent[subcategoryIndex : subcategoryIndex+subcategoryEndIndex] |
| 67 | + subcategory = strings.ReplaceAll(subcategory, "\"", "") |
| 68 | + subcategory = strings.ReplaceAll(subcategory, " ", "_") |
| 69 | + subcategory = strings.ReplaceAll(subcategory, "_(", "(") |
| 70 | + |
| 71 | + exampleList := []string{} |
| 72 | + for { |
| 73 | + index := strings.Index(docContent, exampleStart) |
| 74 | + if index == -1 { |
| 75 | + break |
| 76 | + } |
| 77 | + |
| 78 | + docContent = docContent[index+len(exampleStart):] |
| 79 | + endIndex := strings.Index(docContent, exampleEnd) |
| 80 | + exampleStr := docContent[:endIndex] |
| 81 | + exampleStr = strings.TrimSpace(exampleStr) |
| 82 | + exampleList = append(exampleList, exampleStr) |
| 83 | + docContent = docContent[endIndex+len(exampleEnd):] |
| 84 | + } |
| 85 | + |
| 86 | + resourceName := "alicloud_" + strings.TrimSuffix(resourceDoc, ".html.markdown") |
| 87 | + targetFileName := strings.ReplaceAll(strings.ReplaceAll(resourceName, "alicloud", "101"), "_", "-") + "-docs-Example" |
| 88 | + examplePath := quickstartsPath + subcategory + "/" |
| 89 | + |
| 90 | + for i, exp := range exampleList { |
| 91 | + |
| 92 | + exampleName := targetFileName |
| 93 | + if len(exampleList) != 1 { |
| 94 | + if i < 9 { |
| 95 | + exampleName += "-0" + strconv.Itoa(i+1) |
| 96 | + } else { |
| 97 | + exampleName += "-" + strconv.Itoa(i+1) |
| 98 | + } |
| 99 | + } |
| 100 | + exampleName = examplePath + exampleName |
| 101 | + |
| 102 | + _, err := os.Stat(exampleName + "/main.tf") |
| 103 | + // the example exists |
| 104 | + if err == nil { |
| 105 | + expFile, err := os.OpenFile(exampleName+"/main.tf", |
| 106 | + os.O_RDWR|os.O_TRUNC, 0644) |
| 107 | + if err != nil { |
| 108 | + continue |
| 109 | + } |
| 110 | + expFile.WriteString(exp) |
| 111 | + expFile.Close() |
| 112 | + } |
| 113 | + // the example does not exist |
| 114 | + if err != nil && os.IsNotExist(err) { |
| 115 | + if _, ok := newResource[resourceName]; !ok { |
| 116 | + continue |
| 117 | + } |
| 118 | + |
| 119 | + err := os.MkdirAll(exampleName, 0755) |
| 120 | + if err != nil { |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + expFile, err := os.OpenFile(exampleName+"/main.tf", |
| 125 | + os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
| 126 | + if err != nil { |
| 127 | + continue |
| 128 | + } |
| 129 | + expFile.WriteString(exp) |
| 130 | + expFile.Close() |
| 131 | + |
| 132 | + providerFile, err := os.OpenFile(exampleName+"/provider.tf", |
| 133 | + os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
| 134 | + if err != nil { |
| 135 | + continue |
| 136 | + } |
| 137 | + providerFile.WriteString(alicloudProvider) |
| 138 | + providerFile.Close() |
| 139 | + |
| 140 | + headerFile, err := os.OpenFile(exampleName+"/README.md", |
| 141 | + os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) |
| 142 | + if err != nil { |
| 143 | + continue |
| 144 | + } |
| 145 | + headerFile.WriteString(fmt.Sprintf(header, resourceName)) |
| 146 | + headerFile.Close() |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
| 152 | + |
| 153 | +func getNewResource(version string) (map[string]struct{}, error) { |
| 154 | + newResource := map[string]struct{}{} |
| 155 | + content, err := os.ReadFile(changlogPath) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + changelog := string(content) |
| 160 | + |
| 161 | + logStart := strings.Index(changelog, strings.TrimPrefix(version, "v")) |
| 162 | + logEnd := strings.Index(changelog[logStart:], "##") |
| 163 | + |
| 164 | + latestLog := changelog[logStart : logStart+logEnd] |
| 165 | + |
| 166 | + re := regexp.MustCompile("`(.*?)`") |
| 167 | + parts := strings.Split(latestLog, "\n") |
| 168 | + for _, v := range parts { |
| 169 | + if strings.Contains(v, "**New Resource:**") { |
| 170 | + matches := re.FindStringSubmatch(v) |
| 171 | + if len(matches) > 1 { |
| 172 | + newResource[matches[1]] = struct{}{} |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return newResource, nil |
| 178 | +} |
0 commit comments