|
1 | | -/* |
2 | | -Copyright 2021 Upbound Inc. |
3 | | -*/ |
| 1 | +// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | package main |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
8 | 10 | "fmt" |
9 | 11 | "os" |
10 | 12 | "path/filepath" |
| 13 | + "sort" |
| 14 | + "strings" |
11 | 15 |
|
| 16 | + ujconfig "github.com/crossplane/upjet/pkg/config" |
12 | 17 | "github.com/crossplane/upjet/pkg/pipeline" |
| 18 | + "gopkg.in/alecthomas/kingpin.v2" |
13 | 19 |
|
14 | 20 | "github.com/crossplane-contrib/provider-openstack/config" |
15 | 21 | ) |
16 | 22 |
|
17 | 23 | func main() { |
18 | | - if len(os.Args) < 2 || os.Args[1] == "" { |
19 | | - panic("root directory is required to be given as argument") |
| 24 | + var ( |
| 25 | + app = kingpin.New("generator", "Run Upjet code generation pipelines for provider-azuread").DefaultEnvars() |
| 26 | + repoRoot = app.Arg("repo-root", "Root directory for the provider repository").Required().String() |
| 27 | + skippedResourcesCSV = app.Flag("skipped-resources-csv", "File path where a list of skipped (not-generated) Terraform resource names will be stored as a CSV").Envar("SKIPPED_RESOURCES_CSV").String() |
| 28 | + generatedResourceList = app.Flag("generated-resource-list", "File path where a list of the generated resources will be stored.").Envar("GENERATED_RESOURCE_LIST").Default("../config/generated.lst").String() |
| 29 | + ) |
| 30 | + kingpin.MustParse(app.Parse(os.Args[1:])) |
| 31 | + |
| 32 | + absRootDir, err := filepath.Abs(*repoRoot) |
| 33 | + if err != nil { |
| 34 | + panic(fmt.Sprintf("cannot calculate the absolute path with %s", *repoRoot)) |
| 35 | + } |
| 36 | + p, err := config.GetProvider(context.Background(), true) |
| 37 | + kingpin.FatalIfError(err, "Cannot initialize the provider configuration") |
| 38 | + dumpGeneratedResourceList(p, generatedResourceList) |
| 39 | + dumpSkippedResourcesCSV(p, skippedResourcesCSV) |
| 40 | + pipeline.Run(p, absRootDir) |
| 41 | +} |
| 42 | + |
| 43 | +func dumpGeneratedResourceList(p *ujconfig.Provider, targetPath *string) { |
| 44 | + if len(*targetPath) == 0 { |
| 45 | + return |
| 46 | + } |
| 47 | + generatedResources := make([]string, 0, len(p.Resources)) |
| 48 | + for name := range p.Resources { |
| 49 | + generatedResources = append(generatedResources, name) |
20 | 50 | } |
21 | | - rootDir := os.Args[1] |
22 | | - absRootDir, err := filepath.Abs(rootDir) |
| 51 | + sort.Strings(generatedResources) |
| 52 | + buff, err := json.Marshal(generatedResources) |
23 | 53 | if err != nil { |
24 | | - panic(fmt.Sprintf("cannot calculate the absolute path with %s", rootDir)) |
| 54 | + panic(fmt.Sprintf("Cannot marshal native schema versions to JSON: %s", err.Error())) |
| 55 | + } |
| 56 | + if err := os.WriteFile(*targetPath, buff, 0o600); err != nil { |
| 57 | + panic(fmt.Sprintf("Cannot write native schema versions of generated resources to file %s: %s", *targetPath, err.Error())) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func dumpSkippedResourcesCSV(p *ujconfig.Provider, targetPath *string) { |
| 62 | + if len(*targetPath) == 0 { |
| 63 | + return |
| 64 | + } |
| 65 | + skippedCount := len(p.GetSkippedResourceNames()) |
| 66 | + totalCount := skippedCount + len(p.Resources) |
| 67 | + summaryLine := fmt.Sprintf("Available, skipped, total, coverage: %d, %d, %d, %.1f%%", len(p.Resources), skippedCount, totalCount, (float64(len(p.Resources))/float64(totalCount))*100) |
| 68 | + if err := os.WriteFile(*targetPath, []byte(strings.Join(append([]string{summaryLine}, p.GetSkippedResourceNames()...), "\n")), 0o600); err != nil { |
| 69 | + panic(fmt.Sprintf("Cannot write skipped resources CSV to file %s: %s", *targetPath, err.Error())) |
25 | 70 | } |
26 | | - pipeline.Run(config.GetProvider(), absRootDir) |
27 | 71 | } |
0 commit comments