import ( "encoding/json" "fmt" "io" "log" "net/http" "os" "os/exec" "path/filepath" "strings" "github.com/pkg/errors" ) type TFDownload struct { Data []DataItem `json:"data"` Included []IncludedItem `json:"included"` } type IncludedItem struct { Id string `json:"id"` Attributes Attributes `json:"attributes"` } type DataItem struct { Attributes Attributes `json:"attributes"` Relationships Relationships `json:"relationships"` } type Relationships struct { LatestVersion RelationshipLatestVersion `json:"latest-version"` } type RelationshipLatestVersion struct { Data RelationshipData `json:"data"` } type RelationshipData struct { Id string `json:"id"` } var errNoVariables = errors.New("failed to find main.tf or variables.tf in Terraform configurations") type Attributes struct { Name string `json:"name"` Downloads int `json:"downloads"` Source string `json:"source"` Description string `json:"description"` Verified bool `json:"verified"` } func main() { if len(os.Args) < 2 { fmt.Println("Please provide the cloud provider name and an official Terraform modules URL") os.Exit(1) } providerName := os.Args[1] terraformModulesUrl := os.Args[2] resp, err := http.Get(terraformModulesUrl) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) } var modules TFDownload if err := json.Unmarshal(body, &modules); err != nil { fmt.Println(err.Error()) } if _, err = os.Stat(providerName); err == nil { if err := os.RemoveAll(providerName); err != nil { } fmt.Printf("Successfully deleted existed directory %s\n", providerName) } if _, err = os.Stat(providerName); os.IsNotExist(err) { if err := os.Mkdir(providerName, 0755); err != nil { if !os.IsExist(err) { } fmt.Printf("Successfully created directory %s\n", providerName) } for _, module := range modules.Data { var description string for _, attr := range modules.Included { if module.Relationships.LatestVersion.Data.Id == attr.Id { description = attr.Attributes.Description } if description == "" { description = strings.ToUpper(providerName) + " " + strings.Title(module.Attributes.Name) } outputFile := fmt.Sprintf("%s/terraform-%s-%s.yaml", providerName, providerName, module.Attributes.Name) if _, err := os.Stat(outputFile); !os.IsNotExist(err) { continue } if providerName == "aws" && (module.Attributes.Name == "rds" || module.Attributes.Name == "s3-bucket" || module.Attributes.Name == "subnet" || module.Attributes.Name == "vpc") { } if err := generateDefinition(providerName, module.Attributes.Name, module.Attributes.Source, "", description); err != nil { } func generateDefinition(provider, name, gitURL, path, description string) error { defYaml := filepath.Join(provider, fmt.Sprintf("terraform-%s-%s.yaml", provider, name)) cmd := fmt.Sprintf("vela def init %s --type component --provider %s --git %s.git --desc \"%s\" -o %s", name, provider, gitURL, description, defYaml) if path != "" { cmd = fmt.Sprintf("%s --path %s", cmd, path) } fmt.Println(cmd) stdout, err := exec.Command("bash", "-c", cmd).CombinedOutput() return errors.Wrap(err, string(stdout)) } fmt.Println(string(stdout)) return nil
`执行命令:```bash go run gen.go aws "https://registry.terraform.io/v2/modules?filter%5Bprovider%5D=aws&include=latest-version&page%5Bsize%5D=50&page%5Bnumber%5D=1"
1.4 代码简要说明
解析云资源数据
访问用户传入的 URL,将返回的 json 数据解析为 Go 中的结构体。 资源对应的 json 格式如下: