Replace bash release helper scripts with Go progam

This commit is contained in:
jregan
2020-10-08 10:45:12 -07:00
parent 4052cd4fd8
commit 0c169e96e5
31 changed files with 2130 additions and 176 deletions

View File

@@ -0,0 +1,126 @@
package misc
import (
"fmt"
"sigs.k8s.io/kustomize/cmd/gorepomod/internal/semver"
)
// ModFunc is a function accepting a module, and returning an error.
type ModFunc func(LaModule) error
type LaRepository interface {
// RepoPath is the import of the of repository,
// e.g. github.com/kubernetes-sigs/kustomize
// The directory {srcRoot}/{importPath} should contain a
// dotGit directory.
// This directory might be a Go module, or contain directories
// that are Go modules, or both.
RepoPath() string
// AbsPath is the full local filesystem path.
AbsPath() string
// FindModule returns a module or nil.
FindModule(ModuleShortName) LaModule
}
type LaModule interface {
// ShortName is the module's name without the repo.
ShortName() ModuleShortName
// ImportPath is the relative path below the Go src root,
// which is the same path as would be used to
// import the module.
ImportPath() string
// AbsPath is the absolute path to the module's
// go.mod file on the local file system.
AbsPath() string
// Latest version tagged locally.
VersionLocal() semver.SemVer
// Latest version tagged remotely.
VersionRemote() semver.SemVer
// Does this module depend on the argument, and
// if so at what version?
DependsOn(LaModule) (bool, semver.SemVer)
// GetReplacements returns a list of replacements.
GetReplacements() []string
}
// VersionMap holds the versions associated with modules.
type VersionMap map[ModuleShortName]semver.Versions
func (m VersionMap) Report() {
for n, versions := range m {
fmt.Println(n)
for _, v := range versions {
fmt.Print(" ")
fmt.Println(v)
}
}
}
func (m VersionMap) Latest(
n ModuleShortName) semver.SemVer {
versions := m[n]
if versions == nil {
return semver.Zero()
}
return versions[0]
}
type LesModules []LaModule
func (s LesModules) LenLongestName() (ans int) {
for _, m := range s {
l := len(m.ShortName())
if l > ans {
ans = l
}
}
return
}
func (s LesModules) Apply(f ModFunc) error {
for _, m := range s {
err := f(m)
if err != nil {
return err
}
}
return nil
}
func (s LesModules) Find(target ModuleShortName) LaModule {
for _, m := range s {
if m.ShortName() == target {
return m
}
}
return nil
}
func (s LesModules) GetAllThatDependOn(
target LaModule) (result TaggedModules) {
for _, m := range s {
if yes, v := m.DependsOn(target); yes {
result = append(result, TaggedModule{M: m, V: v})
}
}
return
}
func (s LesModules) InternalDeps(
target LaModule) (result TaggedModules) {
for _, m := range s {
if yes, v := target.DependsOn(m); yes {
result = append(result, TaggedModule{M: m, V: v})
}
}
return
}

View File

@@ -0,0 +1,23 @@
package misc
import (
"path/filepath"
"strings"
)
// ModuleShortName is the in-repo path to the directory holding the module
// (holding the go.mod file). It's the unique in-repo name of the module.
// It's the name used to tag the repo at a particular module version.
// E.g. "" (empty), "kyaml", "cmd/config", "plugin/example/whatever".
type ModuleShortName string
// Never used in a tag.
const ModuleAtTop = ModuleShortName("{top}")
const ModuleUnknown = ModuleShortName("{unknown}")
func (m ModuleShortName) Depth() int {
if m == ModuleAtTop || m == ModuleUnknown {
return 0
}
return strings.Count(string(m), string(filepath.Separator)) + 1
}

View File

@@ -0,0 +1,36 @@
package misc_test
import (
"testing"
"sigs.k8s.io/kustomize/cmd/gorepomod/internal/misc"
)
func TestDepth(t *testing.T) {
var testCases = map[string]struct {
path string
expectedDepth int
}{
"zero": {
path: "{top}",
expectedDepth: 0,
},
"one": {
path: "one",
expectedDepth: 1,
},
"three": {
path: "one/two/three",
expectedDepth: 3,
},
}
for n, tc := range testCases {
m := misc.ModuleShortName(tc.path)
d := m.Depth()
if d != tc.expectedDepth {
t.Fatalf(
"%s: %s, expected %d, got %d",
n, tc.path, tc.expectedDepth, d)
}
}
}

View File

@@ -0,0 +1,42 @@
package misc
import (
"fmt"
"strings"
"sigs.k8s.io/kustomize/cmd/gorepomod/internal/semver"
)
// TaggedModule is a module known to be tagged with the given version.
type TaggedModule struct {
M LaModule
V semver.SemVer
}
func (p TaggedModule) String() string {
if p.V.IsZero() {
return string(p.M.ShortName())
}
return string(p.M.ShortName()) + "/" + p.V.String()
}
type TaggedModules []TaggedModule
func (s TaggedModules) String() string {
// format := "%-"+strconv.Itoa(s.LenLongestString()+2)+"s"
var b strings.Builder
for i := range s {
b.WriteString(fmt.Sprintf("%-15s", s[i]))
}
return b.String()
}
func (s TaggedModules) LenLongestString() (ans int) {
for _, m := range s {
l := len(m.String())
if l > ans {
ans = l
}
}
return
}

View File

@@ -0,0 +1,4 @@
package misc
// TrackedRepo identifies a git remote repository.
type TrackedRepo string