Skip to content

Commit a6c3c2d

Browse files
committed
Fix main generator
Signed-off-by: Jan Dittrich <jan.dittrich@cgm.com>
1 parent db98b3f commit a6c3c2d

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

cmd/generator/main.go

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,71 @@
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
44

55
package main
66

77
import (
8+
"context"
9+
"encoding/json"
810
"fmt"
911
"os"
1012
"path/filepath"
13+
"sort"
14+
"strings"
1115

16+
ujconfig "github.com/crossplane/upjet/pkg/config"
1217
"github.com/crossplane/upjet/pkg/pipeline"
18+
"gopkg.in/alecthomas/kingpin.v2"
1319

1420
"github.com/crossplane-contrib/provider-openstack/config"
1521
)
1622

1723
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)
2050
}
21-
rootDir := os.Args[1]
22-
absRootDir, err := filepath.Abs(rootDir)
51+
sort.Strings(generatedResources)
52+
buff, err := json.Marshal(generatedResources)
2353
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()))
2570
}
26-
pipeline.Run(config.GetProvider(), absRootDir)
2771
}

0 commit comments

Comments
 (0)