-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (154 loc) · 5.48 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2025 Orange
* SPDX-License-Identifier: Mozilla Public License 2.0
*
* This software is distributed under the MPL-2.0 license.
* the text of which is available at https://www.mozilla.org/en-US/MPL/2.0/
* or see the "LICENSE" file for more details.
*/
package main
import (
"flag"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/iancoleman/strcase"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
"github.com/orange-cloudavenue/terraform-resource-templates/internal/terraform"
"github.com/orange-cloudavenue/terraform-resource-templates/pkg/file"
_ "embed"
)
func main() {
fileName := flag.String("filename", "", "filename")
flag.Parse()
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
if *fileName == "" {
log.Fatal().Msg("filename is required")
}
if !file.IsFileExists(*fileName) {
log.Fatal().Msgf("file %s not found", *fileName)
}
// if .golangci.yml exists, use it
if !file.IsFileExists(".golangci.yml") {
// read file ../.golangci.yml
golangCIFile, err := file.ReadFile(".golangci.yml")
if err != nil {
log.Fatal().Err(err).Msg("Failed to read file")
}
// parse file ../.golangci.yml
golangCI := &golangCI{}
if err := yaml.Unmarshal(golangCIFile, golangCI); err != nil {
log.Fatal().Err(err).Msg("Failed to parse file")
}
varNaming := make([]string, 0)
// get all var-naming rules
for _, rule := range golangCI.LintersSettings.Revive.Rules {
if rule.Name == "var-naming" {
for _, arg := range rule.Arguments {
varNaming = append(varNaming, arg[0].(string))
}
}
}
// configure var-naming rules
for _, v := range varNaming {
strcase.ConfigureAcronym(strings.ToUpper(v), strings.ToLower(v))
}
}
// Get the absolute path of the file
absPath, err := filepath.Abs(*fileName)
if err != nil {
log.Fatal().Err(err).Msg("error getting absolute path")
}
// Set test dir
testDir := filepath.Join(absPath, "../../../testsacc/")
// Determine the schema Path with the absolute path
schemaDir := filepath.Join(absPath, "../")
log.Info().Msgf("using schema dir %s", schemaDir)
// test if filedir exists and is a directory
dir, err := os.Stat(testDir)
if err != nil || !dir.IsDir() {
// create the directory
if err := os.MkdirAll(testDir, 0o755); err != nil {
log.Fatal().Err(err).Msgf("error creating directory %s", testDir)
}
}
log.Info().Msgf("using file %s", *fileName)
f, err := file.ToString(*fileName)
if err != nil {
log.Fatal().Err(err).Msg("error reading file")
}
tfTypes := terraform.GetTFTypes(*fileName)
if tfTypes == "" {
log.Fatal().Msgf("tf types not found. The filename must be like `my_tf_name_datasource.go` or `my_tf_name_resource.go`")
}
log.Info().Msgf("tf type : %s", tfTypes)
packageName := terraform.GetPackageName(f)
if packageName == "" {
log.Fatal().Msg("package name not found")
}
log.Info().Msgf("package name : %s", packageName)
categoryName, resourceName := terraform.GetTFName(f)
if categoryName == "" {
log.Fatal().Msg("tfname not found. Please add a comment like `// tfname: category_resource_name")
}
log.Info().Msgf("categoryName : %s", categoryName)
log.Info().Msgf("resourceName : %s", resourceName)
templateDocDir := filepath.Join(absPath, "../../../../templates/")
dir, err = os.Stat(templateDocDir)
if err != nil || !dir.IsDir() {
log.Info().Msgf("creating directory %s", templateDocDir)
// create the directory
if err := os.MkdirAll(templateDocDir, 0o755); err != nil {
log.Fatal().Err(err).Msgf("error creating directory %s", templateDocDir)
}
}
examplesDir := filepath.Join(absPath, "../../../../examples/")
dir, err = os.Stat(examplesDir)
if err != nil || !dir.IsDir() {
log.Info().Msgf("creating directory %s", templateDocDir)
// create the directory
if err := os.MkdirAll(templateDocDir, 0o755); err != nil {
log.Fatal().Err(err).Msgf("error creating directory %s", templateDocDir)
}
}
ressOrData := "resources" //nolint:goconst
if strings.Contains(*fileName, "datasource") {
ressOrData = "data-sources" //nolint:goconst
}
examplesDirSubDir := filepath.Join(examplesDir, ressOrData, "cloudavenue_"+packageName+"_"+resourceName)
subDir, err := os.Stat(examplesDirSubDir)
if err != nil || !subDir.IsDir() {
log.Info().Msgf("creating directory %s", examplesDirSubDir)
// create the directory
if err := os.MkdirAll(examplesDirSubDir, 0o755); err != nil {
log.Fatal().Err(err).Msgf("error creating directory %s", examplesDirSubDir)
}
}
t := genTemplateConf(categoryName, resourceName, packageName, testDir, *fileName, schemaDir, templateDocDir, examplesDir)
if err := t.createTemplateFiles(tfTypes); err != nil {
log.Fatal().Err(err).Msg("error creating file")
}
log.Info().Msg("Run linter")
if err := exec.Command("golangci-lint", "run", "--fix", "--config", ".golangci.yml").Run(); err != nil {
log.Error().Err(err).Msg("error running linter")
}
log.Info().Msg("Done")
}
type golangCI struct {
LintersSettings struct {
Revive struct {
Revive interface{} `yaml:"revive"`
IgnoreGeneratedHeader bool `yaml:"ignore-generated-header"` //nolint:tagliatelle
Severity string `yaml:"severity"`
Rules []struct {
Name string `yaml:"name"`
Severity string `yaml:"severity"`
Disabled bool `yaml:"disabled"`
Arguments [][]interface{} `yaml:"arguments,omitempty"`
} `yaml:"rules"`
} `yaml:"revive"`
} `yaml:"linters-settings"` //nolint:tagliatelle
}