Skip to content

Commit

Permalink
feat: add generator cmd to generate or update readme.go.md file for t…
Browse files Browse the repository at this point in the history
…rack2 sdk param (#16780)
  • Loading branch information
tadelesh authored Jan 10, 2022
1 parent 9f9f02c commit bf83275
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions eng/tools/generator/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/release"
"github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/template"
automation_v2 "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/automation"
"github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/readme"
refresh_v2 "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/refresh"
release_v2 "github.com/Azure/azure-sdk-for-go/eng/tools/generator/cmd/v2/release"
"github.com/Azure/azure-sdk-for-go/eng/tools/generator/common"
Expand Down Expand Up @@ -49,6 +50,7 @@ func Command() *cobra.Command {
automation_v2.Command(),
release_v2.Command(),
refresh_v2.Command(),
readme.Command(),
)

return rootCmd
Expand Down
76 changes: 76 additions & 0 deletions eng/tools/generator/cmd/v2/readme/readmeCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package readme

import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)

// Command returns the generate-go-readme command.
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "generate-go-readme <rp readme filepath>",
Short: "Generate a go readme file or add go track2 part to go readme file according to base swagger readme file",
Args: cobra.ExactArgs(1),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.SetFlags(0) // remove the time stamp prefix
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := execute(args[0]); err != nil {
logError(err)
return err
}
return nil
},
SilenceUsage: true, // this command is used for a pipeline, the usage should never show
}

return cmd
}

func execute(rpReadmeFilepath string) error {
if _, err := os.Stat(rpReadmeFilepath); errors.Is(err, os.ErrNotExist) {
return err
}
basePath := filepath.Dir(rpReadmeFilepath)
rpName := filepath.Base(filepath.Dir(basePath))

readmeGoFile := filepath.Join(basePath, "readme.go.md")
content := ""
if _, err := os.Stat(readmeGoFile); err == nil {
b, err := ioutil.ReadFile(readmeGoFile)
if err != nil {
return err
}
content = string(b)
}

if !strings.Contains(content, "$(go) && $(track2)") {
content += fmt.Sprintf("\n\n``` yaml $(go) && $(track2)\nlicense-header: MICROSOFT_MIT_NO_VERSION\nmodule-name: sdk/resourcemanager/%s/arm%s\nmodule: github.com/Azure/azure-sdk-for-go/$(module-name)\noutput-folder: $(go-sdk-folder)/$(module-name)\nazure-arm: true\n```\n", rpName, rpName)
if err := ioutil.WriteFile(readmeGoFile, []byte(content), 0644); err != nil {
return err
}
}

log.Printf("Succeed to generate readme.go.me file from '%s'...", rpReadmeFilepath)

return nil
}

func logError(err error) {
for _, line := range strings.Split(err.Error(), "\n") {
if l := strings.TrimSpace(line); l != "" {
log.Printf("[ERROR] %s", l)
}
}
}

0 comments on commit bf83275

Please sign in to comment.