From b6fba0ad5ce3ff041aa6bf74d8bad02a992686be Mon Sep 17 00:00:00 2001 From: monopole Date: Fri, 30 Apr 2021 17:08:16 -0700 Subject: [PATCH] When releasing, allow certain replacements. --- cmd/gorepomod/internal/arguments/args.go | 12 +++++++++++ cmd/gorepomod/internal/repo/manager.go | 27 +++++++++++++++++++----- cmd/gorepomod/internal/utils/utils.go | 18 ++++++++++++++++ cmd/gorepomod/main.go | 3 ++- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/cmd/gorepomod/internal/arguments/args.go b/cmd/gorepomod/internal/arguments/args.go index c932229df..81074cd41 100644 --- a/cmd/gorepomod/internal/arguments/args.go +++ b/cmd/gorepomod/internal/arguments/args.go @@ -37,6 +37,10 @@ var ( "releasing", "site", } + // TODO: make this a PATH-like flag + allowedReplacements = []string { + "gopkg.in/yaml.v3", + } ) type Command int @@ -64,6 +68,14 @@ func (a *Args) GetCommand() Command { return a.cmd } +func (a *Args) AllowedReplacements() (result []string) { + // Make sure the list has no repeats. + for k := range utils.SliceToSet(allowedReplacements) { + result = append(result, k) + } + return +} + func (a *Args) Bump() semver.SvBump { return a.bump } diff --git a/cmd/gorepomod/internal/repo/manager.go b/cmd/gorepomod/internal/repo/manager.go index c54df3fe2..7d09d82dc 100644 --- a/cmd/gorepomod/internal/repo/manager.go +++ b/cmd/gorepomod/internal/repo/manager.go @@ -8,6 +8,7 @@ import ( "sigs.k8s.io/kustomize/cmd/gorepomod/internal/git" "sigs.k8s.io/kustomize/cmd/gorepomod/internal/misc" "sigs.k8s.io/kustomize/cmd/gorepomod/internal/semver" + "sigs.k8s.io/kustomize/cmd/gorepomod/internal/utils" ) // Manager manages a git repo. @@ -116,14 +117,18 @@ func (mgr *Manager) Debug(_ misc.LaModule, doIt bool) error { // // * All development happens in the branch named "master". // * Each minor release gets its own branch. -// * +// func (mgr *Manager) Release( - target misc.LaModule, bump semver.SvBump, doIt bool) error { + target misc.LaModule, bump semver.SvBump, + allowedReplacements []string, doIt bool) error { if reps := target.GetReplacements(); len(reps) > 0 { - return fmt.Errorf( - "to release %q, first pin these replacements: %v", - target.ShortName(), reps) + badReps := findDisallowedReplacements(reps, allowedReplacements) + if len(badReps) > 0 { + return fmt.Errorf( + "to release %q, first pin these replacements: %v", + target.ShortName(), badReps) + } } newVersion := target.VersionLocal().Bump(bump) @@ -182,6 +187,18 @@ func (mgr *Manager) Release( return nil } +func findDisallowedReplacements( + reps []string, allowedReplacements []string) []string { + var badReps []string + for _, r := range reps { + km := utils.ExtractModule(r) + if !utils.SliceContains(allowedReplacements, km) { + badReps = append(badReps, r) + } + } + return badReps +} + func (mgr *Manager) UnRelease(target misc.LaModule, doIt bool) error { fmt.Printf( "Unreleasing %s/%s\n", diff --git a/cmd/gorepomod/internal/utils/utils.go b/cmd/gorepomod/internal/utils/utils.go index c252373ad..6c16a149c 100644 --- a/cmd/gorepomod/internal/utils/utils.go +++ b/cmd/gorepomod/internal/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "log" "os" + "strings" ) func DirExists(name string) bool { @@ -24,3 +25,20 @@ func SliceToSet(slice []string) map[string]bool { } return result } + +func ExtractModule(m string) string { + k := strings.Index(m, " => ") + if k < 0 { + return m + } + return m[:k] +} + +func SliceContains(slice []string, target string) bool { + for _, x := range slice { + if x == target { + return true + } + } + return false +} diff --git a/cmd/gorepomod/main.go b/cmd/gorepomod/main.go index 8be9853af..ae6641e39 100644 --- a/cmd/gorepomod/main.go +++ b/cmd/gorepomod/main.go @@ -71,7 +71,8 @@ func actualMain() error { case arguments.UnPin: return mgr.UnPin(args.DoIt(), targetModule, conditionalModule) case arguments.Release: - return mgr.Release(targetModule, args.Bump(), args.DoIt()) + return mgr.Release( + targetModule, args.Bump(), args.AllowedReplacements(), args.DoIt()) case arguments.UnRelease: return mgr.UnRelease(targetModule, args.DoIt()) case arguments.Debug: