-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add tool to generate services.kt file that contains mmv1 and tpgtools/DCL services #8344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
56f6e2b
Add tool to generate services.kt file that contains mmv1 and tpgtools…
SarahFrench 9ebc7a1
Fix log function call
SarahFrench 1ead2a5
Correct indentation size in service map output
SarahFrench ddc1d43
Call teamcity Make target when generating TPG/TPGB
SarahFrench 90a64d8
Add commas to generated map string
SarahFrench 2d79141
test change
shuyama1 fcc4a37
update test
shuyama1 a42b787
remove test file
shuyama1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module github.com/GoogleCloudPlatform/magic-modules/tools/teamcity-generator | ||
|
|
||
| go 1.19 | ||
|
|
||
| require golang.org/x/text v0.11.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= | ||
| golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "golang.org/x/text/cases" | ||
| "golang.org/x/text/language" | ||
| ) | ||
|
|
||
| var GA_VERSION = "ga" | ||
| var BETA_VERSION = "beta" | ||
|
|
||
| var oPath = flag.String("output", "", "path to output generated files to") | ||
| var ver = flag.String("version", "", "version name, value must be `ga` or `beta`") | ||
|
|
||
| func main() { | ||
| var version string | ||
| var outputPath string | ||
|
|
||
| flag.Parse() | ||
| outputPath = *oPath | ||
| version = *ver | ||
| if outputPath == "" { | ||
| log.Fatalf("missing output flag: provide `--output <path>` to set the path to output generated files to") | ||
| } | ||
| if version == "" { | ||
| log.Fatalf("missing version flag: provide `--version <ga|beta>` to set the path to output generated files to") | ||
| } | ||
| if version != GA_VERSION && version != BETA_VERSION { | ||
| log.Fatalf("invalid version flag value: value must be `%s` or `%s`", GA_VERSION, BETA_VERSION) | ||
| } | ||
|
|
||
| var terraformResourceDirectory string | ||
| switch version { | ||
| case GA_VERSION: | ||
| terraformResourceDirectory = "google" | ||
| case BETA_VERSION: | ||
| terraformResourceDirectory = "google-beta" | ||
| default: | ||
| log.Fatalf("invalid version flag value: value must be `%s` or `%s`", GA_VERSION, BETA_VERSION) | ||
| } | ||
|
|
||
| log.Printf("Generating TeamCity configuration service package map for `%s` provider", terraformResourceDirectory) | ||
|
|
||
| // Get a list of the service packages found in a given directory | ||
| servicesDir := fmt.Sprintf("%s/%s/services", outputPath, terraformResourceDirectory) | ||
| serviceList, err := readAllServicePackages(servicesDir) | ||
| if err != nil { | ||
| log.Fatalf("error determining service package list: %s", err) | ||
| } | ||
|
|
||
| // Create a string of the map that should be created in .teamcity/components/generated/services.kt | ||
| serviceMap, err := createMap(serviceList) | ||
| if err != nil { | ||
| log.Fatalf("error creating service package map: %s", err) | ||
| } | ||
|
|
||
| // Ensure .teamcity/components/generated/services.kt exists, create if not present | ||
| // "Create creates or truncates the named file. If the file already exists, it is truncated." | ||
| servicesKtFilePath := fmt.Sprintf("%s/.teamcity/components/generated/services.kt", outputPath) | ||
| log.Printf("Opening %s", servicesKtFilePath) | ||
| f, err := os.Create(servicesKtFilePath) | ||
|
SarahFrench marked this conversation as resolved.
|
||
| if err != nil { | ||
| log.Fatalf("error creating or truncating existing file `.teamcity/components/generated/services.kt` in output directory: %s", err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| // Save map to .teamcity/components/generated/services.kt | ||
| log.Printf("Saving service map to %s", servicesKtFilePath) | ||
| _, err = f.Write([]byte(serviceMap)) | ||
| if err != nil { | ||
| log.Fatalf("error writing to file `.teamcity/components/generated/services.kt` in output directory: %s", err) | ||
| } | ||
|
|
||
| log.Println("Finished") | ||
| } | ||
|
|
||
| func readAllServicePackages(providerDir string) ([]string, error) { | ||
| packages, err := os.ReadDir(providerDir) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var services = make([]string, 0) | ||
|
|
||
| for _, p := range packages { | ||
| services = append(services, p.Name()) | ||
| } | ||
| if len(services) == 0 { | ||
| return nil, fmt.Errorf("found 0 service packages in %s", providerDir) | ||
| } | ||
| return services, nil | ||
| } | ||
|
|
||
| func createMap(packageNames []string) (string, error) { | ||
|
|
||
| entryTemplate := " \"%s\" to \"%s\",\n" | ||
| lastEntryTemplate := " \"%s\" to \"%s\"\n" // No trailing comma | ||
| caser := cases.Title(language.English) | ||
|
|
||
| var b strings.Builder | ||
| b.WriteString("// this file is auto-generated by magic-modules/tools/teamcity-generator, any changes made here will be overwritten\n\n") | ||
| b.WriteString("var services = mapOf(\n") | ||
| for i, p := range packageNames { | ||
|
|
||
| var e string | ||
| if i < (len(packageNames) - 1) { | ||
| e = fmt.Sprintf(entryTemplate, p, caser.String(p)) | ||
| } else { | ||
| // Final entry in map doesn't have comma | ||
| e = fmt.Sprintf(lastEntryTemplate, p, caser.String(p)) | ||
| } | ||
|
|
||
| _, err := fmt.Fprint(&b, e) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| } | ||
| b.WriteString(")\n") | ||
|
|
||
| return b.String(), nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.