forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tools/generator] add the template command (Azure#15206)
* adds the template command * fix package name of readDir * add a new comment * resolve linter issues * add autorest.md template * update the template accordingly
- Loading branch information
1 parent
3a682e7
commit 3e86b48
Showing
17 changed files
with
323 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,169 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
package template | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/tools/generator/flags" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// Command returns the template command | ||
func Command() *cobra.Command { | ||
templateCmd := &cobra.Command{ | ||
Use: "template (<rpName> <packageName>) | <packagePath>", | ||
Short: "Onboards new RP with the template", | ||
Args: cobra.RangeArgs(1, 2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var rpName, packageName string | ||
if len(args) == 1 { | ||
segments := strings.Split(args[0], "/") | ||
if len(segments) != 2 { | ||
return fmt.Errorf("%s is not a valid package path. Please assign the package path using `rpName/packageName` format", args[0]) | ||
} | ||
rpName = segments[0] | ||
packageName = segments[1] | ||
} else { | ||
rpName = args[0] | ||
packageName = args[1] | ||
} | ||
|
||
return GeneratePackageByTemplate(rpName, packageName, ParseFlags(cmd.Flags())) | ||
}, | ||
} | ||
|
||
BindFlags(templateCmd.Flags()) | ||
if err := templateCmd.MarkFlagRequired("package-title"); err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := templateCmd.MarkFlagRequired("commit"); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return templateCmd | ||
} | ||
|
||
// BindFlags binds the flags to this command | ||
func BindFlags(flagSet *pflag.FlagSet) { | ||
flagSet.String("go-sdk-folder", ".", "Specifies the path of root of azure-sdk-for-go") | ||
flagSet.String("template-path", "tools/generator/template/rpName/packageName", "Specifies the path of the template") | ||
flagSet.String("package-title", "", "Specifies the title of this package") | ||
flagSet.String("commit", "", "Specifies the commit hash of azure-rest-api-specs") | ||
} | ||
|
||
// ParseFlags parses the flags to a Flags struct | ||
func ParseFlags(flagSet *pflag.FlagSet) Flags { | ||
return Flags{ | ||
SDKRoot: flags.GetString(flagSet, "go-sdk-folder"), | ||
TemplatePath: flags.GetString(flagSet, "template-path"), | ||
PackageTitle: flags.GetString(flagSet, "package-title"), | ||
Commit: flags.GetString(flagSet, "commit"), | ||
} | ||
} | ||
|
||
// Flags ... | ||
type Flags struct { | ||
SDKRoot string | ||
TemplatePath string | ||
PackageTitle string | ||
Commit string | ||
} | ||
|
||
// GeneratePackageByTemplate creates a new set of files based on the things in template directory | ||
func GeneratePackageByTemplate(rpName, packageName string, flags Flags) error { | ||
root, err := filepath.Abs(flags.SDKRoot) | ||
if err != nil { | ||
return fmt.Errorf("cannot get the root of azure-sdk-for-go from '%s': %+v", flags.SDKRoot, err) | ||
} | ||
var absTemplateDir string | ||
if filepath.IsAbs(flags.TemplatePath) { | ||
absTemplateDir = flags.TemplatePath | ||
} else { | ||
absTemplateDir = filepath.Join(root, flags.TemplatePath) | ||
} | ||
fileList, err := ioutil.ReadDir(absTemplateDir) | ||
if err != nil { | ||
return fmt.Errorf("cannot read the directory '%s': %+v", absTemplateDir, err) | ||
} | ||
|
||
// build the replaceMap | ||
buildReplaceMap(rpName, packageName, flags.PackageTitle, flags.Commit) | ||
|
||
// copy everything to destination directory | ||
for _, file := range fileList { | ||
path := filepath.Join(absTemplateDir, file.Name()) | ||
content, err := readAndReplace(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dirPath := filepath.Join(root, "sdk", rpName, packageName) | ||
if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { | ||
return fmt.Errorf("cannot create directory '%s': %+v", dirPath, err) | ||
} | ||
|
||
newFilePath := filepath.Join(dirPath, strings.TrimSuffix(file.Name(), FilenameSuffix)) | ||
if err := createNewFile(newFilePath, content); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func buildReplaceMap(rpName, packageName, packageTitle, commitID string) { | ||
replaceMap = make(map[string]string) | ||
|
||
replaceMap[RPNameKey] = rpName | ||
replaceMap[PackageNameKey] = packageName | ||
replaceMap[PackageTitleKey] = packageTitle | ||
replaceMap[CommitIDKey] = commitID | ||
} | ||
|
||
func readAndReplace(path string) (string, error) { | ||
b, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return "", fmt.Errorf("cannot read from file '%s': %+v", path, err) | ||
} | ||
|
||
content := string(b) | ||
for k, v := range replaceMap { | ||
content = strings.ReplaceAll(content, k, v) | ||
} | ||
|
||
return content, nil | ||
} | ||
|
||
func createNewFile(path, content string) error { | ||
file, err := os.Create(path) | ||
if err != nil { | ||
return fmt.Errorf("cannot create file '%s': %+v", path, err) | ||
} | ||
defer file.Close() | ||
|
||
if _, err := file.WriteString(content); err != nil { | ||
return fmt.Errorf("cannot write to file '%s': %+v", path, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var ( | ||
replaceMap map[string]string | ||
) | ||
|
||
const ( | ||
RPNameKey = "{{rpName}}" | ||
PackageNameKey = "{{packageName}}" | ||
PackageTitleKey = "{{PackageTitle}}" | ||
CommitIDKey = "{{commitID}}" | ||
FilenameSuffix = ".tpl" | ||
) |
This file contains 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,3 @@ | ||
# Release History | ||
|
||
## v0.1.0 (released) |
21 changes: 21 additions & 0 deletions
21
tools/generator/template/rpName/packageName/LICENSE.txt.tpl
This file contains 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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 Microsoft | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,76 @@ | ||
# Azure {{PackageTitle}} Module for Go | ||
|
||
[![PkgGoDev](https://pkg.go.dev/badge/github.com/Azure/azure-sdk-for-go/sdk/{{rpName}}/{{packageName}})](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/{{rpName}}/{{packageName}}) | ||
|
||
The `{{packageName}}` module provides operations for working with Azure {{PackageTitle}}. | ||
|
||
[Source code](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/{{rpName}}/{{packageName}}) | ||
|
||
# Getting started | ||
|
||
## Prerequisites | ||
|
||
- an [Azure subscription](https://azure.microsoft.com/free/) | ||
- Go 1.13 or above | ||
|
||
## Install the package | ||
|
||
This project uses [Go modules](https://github.com/golang/go/wiki/Modules) for versioning and dependency management. | ||
|
||
Install the Azure AgFood module: | ||
|
||
```sh | ||
go get github.com/Azure/azure-sdk-for-go/sdk/{{rpName}}/{{packageName}} | ||
``` | ||
|
||
## Authorization | ||
|
||
When creating a client, you will need to provide a credential for authenticating with Azure {{PackageTitle}}. The `azidentity` module provides facilities for various ways of authenticating with Azure including client/secret, certificate, managed identity, and more. | ||
|
||
```go | ||
cred, err := azidentity.NewDefaultAzureCredential(nil) | ||
``` | ||
|
||
For more information on authentication, please see the documentation for `azidentity` at [pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity). | ||
|
||
## Connecting to Azure {{PackageTitle}} | ||
|
||
Once you have a credential, create a connection to the desired ARM endpoint. The `armcore` module provides facilities for connecting with ARM endpoints including public and sovereign clouds as well as Azure Stack. | ||
|
||
```go | ||
con := armcore.NewDefaultConnection(cred, nil) | ||
``` | ||
|
||
For more information on ARM connections, please see the documentation for `armcore` at [pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/armcore](https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/armcore). | ||
|
||
## Clients | ||
|
||
Azure {{PackageTitle}} modules consist of one or more clients. A client groups a set of related APIs, providing access to its functionality within the specified subscription. Create one or more clients to access the APIs you require using your `armcore.Connection`. | ||
|
||
```go | ||
client := {{packageName}}.{{NewClientMethod}}(con, "<subscription ID>") | ||
``` | ||
|
||
## Provide Feedback | ||
|
||
If you encounter bugs or have suggestions, please | ||
[open an issue](https://github.com/Azure/azure-sdk-for-go/issues) and assign the `{{PackageTitle}}` label. | ||
|
||
# Contributing | ||
|
||
This project welcomes contributions and suggestions. Most contributions require | ||
you to agree to a Contributor License Agreement (CLA) declaring that you have | ||
the right to, and actually do, grant us the rights to use your contribution. | ||
For details, visit [https://cla.microsoft.com](https://cla.microsoft.com). | ||
|
||
When you submit a pull request, a CLA-bot will automatically determine whether | ||
you need to provide a CLA and decorate the PR appropriately (e.g., label, | ||
comment). Simply follow the instructions provided by the bot. You will only | ||
need to do this once across all repos using our CLA. | ||
|
||
This project has adopted the | ||
[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). | ||
For more information, see the | ||
[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) | ||
or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any | ||
additional questions or comments. |
10 changes: 10 additions & 0 deletions
10
tools/generator/template/rpName/packageName/autorest.md.tpl
This file contains 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,10 @@ | ||
### AutoRest Configuration | ||
|
||
> see https://aka.ms/autorest | ||
|
||
``` yaml | ||
require: | ||
- https://github.com/Azure/azure-rest-api-specs/blob/{{commitID}}/specification/{{rpName}}/resource-manager/readme.md | ||
- https://github.com/Azure/azure-rest-api-specs/blob/{{commitID}}/specification/{{rpName}}/resource-manager/readme.go.md | ||
module-version: 0.1.0 | ||
``` |
This file contains 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,7 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
// This file enables 'go generate' to regenerate this specific SDK | ||
//go:generate pwsh.exe ../../../eng/scripts/build.ps1 -skipBuild -format -tidy -generate {{packageName}} | ||
|
||
package {{packageName}} |
Oops, something went wrong.