forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (150 loc) · 4.75 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/provider"
)
// Packages in this list are deprecated and cannot be run due to breaking API changes
// this should only be used as a last resort - since all acctests should run nightly.
var packagesToSkip = map[string]struct{}{
"devspace": {},
// force-deprecated and will be removed by Azure on 2021-04-28
// new clusters cannot be cretaed - so there's no point trying to run the tests
"servicefabricmesh": {},
}
func main() {
filePath := flag.String("path", "", "The relative path to the root directory")
showHelp := flag.Bool("help", false, "Display this message")
flag.Parse()
if *showHelp {
flag.Usage()
return
}
generators := []generator{
teamCityServicesListGenerator{},
websiteCategoriesGenerator{},
}
for _, value := range generators {
outputFile := value.outputPath(*filePath)
if err := value.run(outputFile, packagesToSkip); err != nil {
panic(err)
}
}
}
type generator interface {
outputPath(rootDirectory string) string
run(outputFileName string, packagesToSkip map[string]struct{}) error
}
type teamCityServicesListGenerator struct{}
func (teamCityServicesListGenerator) outputPath(rootDirectory string) string {
return fmt.Sprintf("%s/.teamcity/components/generated/services.kt", rootDirectory)
}
func (teamCityServicesListGenerator) run(outputFileName string, packagesToSkip map[string]struct{}) error {
template := `// NOTE: this is Generated from the Service Definitions - manual changes will be lost
// to re-generate this file, run 'make generate' in the root of the repository
var services = mapOf(
%s
)`
items := make([]string, 0)
services := make(map[string]string)
serviceNames := make([]string, 0)
// combine and unique these
for _, service := range provider.SupportedTypedServices() {
info := reflect.TypeOf(service)
packageSegments := strings.Split(info.PkgPath(), "/")
packageName := packageSegments[len(packageSegments)-1]
serviceName := service.Name()
// Service Registrations are reused across Typed and Untyped Services now
if _, exists := services[serviceName]; exists {
continue
}
services[serviceName] = packageName
serviceNames = append(serviceNames, serviceName)
}
for _, service := range provider.SupportedUntypedServices() {
info := reflect.TypeOf(service)
packageSegments := strings.Split(info.PkgPath(), "/")
packageName := packageSegments[len(packageSegments)-1]
serviceName := service.Name()
// Service Registrations are reused across Typed and Untyped Services now
if _, exists := services[serviceName]; exists {
continue
}
services[serviceName] = packageName
serviceNames = append(serviceNames, serviceName)
}
// then ensure these are sorted so they're alphabetical
sort.Strings(serviceNames)
for _, serviceName := range serviceNames {
packageName := services[serviceName]
if _, shouldSkip := packagesToSkip[packageName]; shouldSkip {
continue
}
item := fmt.Sprintf(" %q to %q", packageName, serviceName)
items = append(items, item)
}
formatted := fmt.Sprintf(template, strings.Join(items, ",\n"))
return writeToFile(outputFileName, formatted)
}
type websiteCategoriesGenerator struct{}
func (websiteCategoriesGenerator) outputPath(rootDirectory string) string {
return fmt.Sprintf("%s/website/allowed-subcategories", rootDirectory)
}
func (websiteCategoriesGenerator) run(outputFileName string, _ map[string]struct{}) error {
websiteCategories := make([]string, 0)
// get a distinct list
for _, service := range provider.SupportedTypedServices() {
for _, category := range service.WebsiteCategories() {
if contains(websiteCategories, category) {
continue
}
websiteCategories = append(websiteCategories, category)
}
}
for _, service := range provider.SupportedUntypedServices() {
for _, category := range service.WebsiteCategories() {
if contains(websiteCategories, category) {
continue
}
websiteCategories = append(websiteCategories, category)
}
}
// sort them
sort.Strings(websiteCategories)
fileContents := strings.Join(websiteCategories, "\n")
return writeToFile(outputFileName, fileContents)
}
func writeToFile(filePath string, contents string) error {
outputPath, err := filepath.Abs(filePath)
if err != nil {
return err
}
// output that string to the file
file, err := os.Create(outputPath)
if err != nil {
return err
}
if os.IsExist(err) {
os.Remove(outputPath)
file, err = os.Create(outputPath)
if err != nil {
return err
}
}
defer file.Close()
_, _ = file.WriteString(contents)
return file.Sync()
}
func contains(input []string, value string) bool {
for _, v := range input {
if v == value {
return true
}
}
return false
}