From c3f414b687575cd77c65eadacc53545092153121 Mon Sep 17 00:00:00 2001 From: FlorentP <35779988+frouioui@users.noreply.github.com> Date: Wed, 28 Sep 2022 10:10:28 +0800 Subject: [PATCH] Migrates `release-notes` to pflag (#11365) * Migrates release-notes to pflag Signed-off-by: Florent Poinsard * apply review suggestions Signed-off-by: Florent Poinsard Signed-off-by: Florent Poinsard --- Makefile | 2 +- go/tools/release-notes/release_notes.go | 31 ++++++++++++++----------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 6b556d18e8..959965fae9 100644 --- a/Makefile +++ b/Makefile @@ -491,7 +491,7 @@ generate_ci_workflows: cd test && go run ci_workflow_gen.go && cd .. release-notes: - go run ./go/tools/release-notes -from "$(FROM)" -to "$(TO)" -version "$(VERSION)" -summary "$(SUMMARY)" + go run ./go/tools/release-notes --from "$(FROM)" --to "$(TO)" --version "$(VERSION)" --summary "$(SUMMARY)" install_kubectl_kind: ./tools/get_kubectl_kind.sh diff --git a/go/tools/release-notes/release_notes.go b/go/tools/release-notes/release_notes.go index 2e1fd07e4a..61d899f370 100644 --- a/go/tools/release-notes/release_notes.go +++ b/go/tools/release-notes/release_notes.go @@ -19,7 +19,6 @@ package main import ( "bytes" "encoding/json" - "flag" "fmt" "log" "os" @@ -30,6 +29,8 @@ import ( "strings" "sync" "text/template" + + "github.com/spf13/pflag" ) type ( @@ -488,28 +489,32 @@ func groupAndStringifyPullRequest(pr []prInfo) (string, error) { } func main() { - from := flag.String("from", "", "from sha/tag/branch") - to := flag.String("to", "HEAD", "to sha/tag/branch") - versionName := flag.String("version", "", "name of the version (has to be the following format: v11.0.0)") - summaryFile := flag.String("summary", "", "readme file on which there is a summary of the release") - flag.Parse() + var ( + from, versionName, summaryFile string + to = "HEAD" + ) + pflag.StringVarP(&from, "from", "f", "", "from sha/tag/branch") + pflag.StringVarP(&to, "to", to, "t", "to sha/tag/branch") + pflag.StringVarP(&versionName, "version", "v", "", "name of the version (has to be the following format: v11.0.0)") + pflag.StringVarP(&summaryFile, "summary", "s", "", "readme file on which there is a summary of the release") + pflag.Parse() // The -version flag must be of a valid format. rx := regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)`) // There should be 4 sub-matches, input: "v14.0.0", output: ["v14.0.0", "14", "0", "0"]. - versionMatch := rx.FindStringSubmatch(*versionName) + versionMatch := rx.FindStringSubmatch(versionName) if len(versionMatch) != 4 { - log.Fatal("The -version flag must be set using a valid format. Format: 'vX.X.X'.") + log.Fatal("The --version flag must be set using a valid format. Format: 'vX.X.X'.") } releaseNotes := releaseNote{ - Version: *versionName, + Version: versionName, VersionUnderscore: fmt.Sprintf("%s_%s_%s", versionMatch[1], versionMatch[2], versionMatch[3]), // v14.0.0 -> 14_0_0, this is used to format filenames. } // summary of the release - if *summaryFile != "" { - summary, err := releaseSummary(*summaryFile) + if summaryFile != "" { + summary, err := releaseSummary(summaryFile) if err != nil { log.Fatal(err) } @@ -517,7 +522,7 @@ func main() { } // known issues - knownIssues, err := loadKnownIssues(*versionName) + knownIssues, err := loadKnownIssues(versionName) if err != nil { log.Fatal(err) } @@ -528,7 +533,7 @@ func main() { releaseNotes.KnownIssues = knownIssuesStr // changelog with pull requests - prs, authorCommits, commits, err := loadMergedPRs(*from, *to) + prs, authorCommits, commits, err := loadMergedPRs(from, to) if err != nil { log.Fatal(err) }