diff --git a/releasing/releasing/go.sum b/releasing/releasing/go.sum index 6cf129d9c..3585e1894 100644 --- a/releasing/releasing/go.sum +++ b/releasing/releasing/go.sum @@ -17,6 +17,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -37,6 +38,7 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= @@ -48,12 +50,16 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= +github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -72,13 +78,17 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= +github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU= github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= @@ -107,7 +117,9 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -123,5 +135,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/releasing/releasing/releasing.go b/releasing/releasing/releasing.go index 63de6c4d6..462b61a88 100644 --- a/releasing/releasing/releasing.go +++ b/releasing/releasing/releasing.go @@ -1,8 +1,16 @@ package main import ( + "bytes" "fmt" + "io/ioutil" "log" + "os" + "os/exec" + "regexp" + "sort" + "strconv" + "strings" "github.com/spf13/cobra" ) @@ -11,23 +19,28 @@ var modules = [...]string{ "kyaml", "api", "kstatus", "cmd/config", "cmd/resource", "cmd/kubectl", "pluginator", "kustomize", } -var verbose bool +var verbose bool // Enable verbose or not +var tempDir string // Temporary directory path for git worktree +var pwd string // Current working directory // === Log helper functions === -func logDebug(s string) { + +func logDebug(format string, v ...interface{}) { if verbose { - log.Println("DEBUG " + s) + log.Printf("DEBUG "+format, v...) } } -func logInfo(s string) { - log.Println("INFO " + s) +func logInfo(format string, v ...interface{}) { + log.Printf("INFO "+format, v...) } -func logFatal(s string) { - log.Fatalln("FATAL " + s) +func logFatal(format string, v ...interface{}) { + log.Fatalf("FATAL "+format, v...) } +// === Command line commands === + var rootCmd = &cobra.Command{ Use: "releasing", Short: "This go program is used to improve the modules releasing process in Kustomize repository.", @@ -37,7 +50,18 @@ var listSubCmd = &cobra.Command{ Use: "list", Short: "List current version of all covered modules", Run: func(cmd *cobra.Command, args []string) { - res := []string{} + var err error + pwd, err = os.Getwd() + if err != nil { + logFatal(err.Error()) + } + logDebug("Working directory: %s", pwd) + remote := "upstream" + // Check remotes + checkRemoteExistence(pwd, remote) + // Fetch latest tags from remote + fetchTags(pwd, remote) + res := []string{} // Store result strings for _, mod := range modules { res = append(res, fmt.Sprintf("%s/%s", mod, getModuleCurrentVersion(mod))) } @@ -47,10 +71,29 @@ var listSubCmd = &cobra.Command{ }, } +var release = &cobra.Command{ + Use: "release", + Short: "Release a new version of specified module", + PreRun: func(cmd *cobra.Command, args []string) { + logDebug("Preparing Git environemnt") + prepareGit() + }, + Run: func(cmd *cobra.Command, args []string) { + logInfo("Done") + }, + PostRun: func(cmd *cobra.Command, args []string) { + logDebug("Cleaning Git environment") + cleanGit() + }, +} + var subCmds = [...]*cobra.Command{ listSubCmd, + release, } +// === Main function === + func main() { for _, cmd := range subCmds { rootCmd.AddCommand(cmd) @@ -63,25 +106,147 @@ func main() { } func getModuleCurrentVersion(modName string) string { - mod := newModule(modName) + mod := newModule(modName, pwd) mod.updateCurrentVersion() - return mod.currentVersion + v := mod.version.toString() + logDebug("module %s version.toString => %s", mod.name, v) + return v } +func checkRemoteExistence(path string, remote string) { + logDebug("Checking remote %s in %s", remote, path) + cmd := exec.Command("git", "remote") + cmd.Dir = path + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + logFatal(err.Error()) + } + logDebug("Remotes:\n%s", out.String()) + + regString := fmt.Sprintf("(?m)^%s$", remote) + reg := regexp.MustCompile(regString) + if !reg.MatchString(out.String()) { + logFatal("Cannot find remote named %s", remote) + } + logDebug("Remote %s exists", remote) +} + +func fetchTags(path string, remote string) { + logDebug("Fetching latest tags") + cmd := exec.Command("git", "fetch", "-t", remote) + cmd.Dir = path + err := cmd.Run() + if err != nil { + logFatal(err.Error()) + } + logDebug("Finished fetching") +} + +// === module version struct and functions definition === + +type moduleVersion struct { + major int + minor int + patch int +} + +func (v moduleVersion) toString() string { + return fmt.Sprintf("v%d.%d.%d", v.major, v.minor, v.patch) +} + +func (v *moduleVersion) set(major int, minor int, patch int) { + v.major = major + v.minor = minor + v.patch = patch +} + +// === module struct and functions definition === + type module struct { - name string - path string - currentVersion string + name string + path string + version moduleVersion } -func newModule(modName string) module { +func newModule(modName string, path string) module { mod := module{ name: modName, + path: path, } - logDebug(fmt.Sprintf("Created module struct for %s", modName)) + logDebug("Created module struct for %s", modName) return mod } func (m *module) updateCurrentVersion() { - m.currentVersion = "v1.0.0" + logDebug("Getting latest tag for %s", m.name) + cmd := exec.Command("git", "tag", "-l") + var out bytes.Buffer + cmd.Stdout = &out + cmd.Dir = m.path + err := cmd.Run() + if err != nil { + logFatal(err.Error()) + } + + // Search for module tag + regString := fmt.Sprintf("(?m)^%s/v(\\d+\\.){2}\\d+$", m.name) + reg := regexp.MustCompile(regString) + tagsString := reg.FindAllString(out.String(), -1) + logDebug("Tags for module %s:\n%s", m.name, tagsString) + var versions []moduleVersion + for _, tag := range tagsString { + v := tag[len(m.name)+2:] + vs := strings.Split(v, ".") + major, err := strconv.Atoi(vs[0]) + if err != nil { + logFatal(err.Error()) + } + minor, err := strconv.Atoi(vs[1]) + if err != nil { + logFatal(err.Error()) + } + patch, err := strconv.Atoi(vs[2]) + if err != nil { + logFatal(err.Error()) + } + versions = append(versions, moduleVersion{ + major: major, + minor: minor, + patch: patch, + }) + } + // Sort to find latest tag + sort.Slice(versions, func(i, j int) bool { + if versions[i].major == versions[j].major && versions[i].minor == versions[j].minor { + return versions[i].patch > versions[j].patch + } else if versions[i].major == versions[j].major { + return versions[i].minor > versions[j].minor + } else { + return versions[i].major > versions[j].major + } + }) + + m.version = versions[0] +} + +// === Git environment functions === + +func prepareGit() { + var err error + tempDir, err = ioutil.TempDir("", "kustomize-releases") + if err != nil { + logFatal(err.Error()) + } + logDebug("Created git temp dir: " + tempDir) +} + +func cleanGit() { + logDebug("Deleting git temp dir: " + tempDir) + err := os.RemoveAll(tempDir) + if err != nil { + logFatal(err.Error()) + } + logDebug("Deleting done") } diff --git a/releasing/releasing/releasing_test.go b/releasing/releasing/releasing_test.go index 6f954e225..28c389b71 100644 --- a/releasing/releasing/releasing_test.go +++ b/releasing/releasing/releasing_test.go @@ -1,12 +1,30 @@ package main import ( + "os" + "regexp" "testing" ) func TestGetModuleCurrentVersion(t *testing.T) { - output := getModuleCurrentVersion("test") - if output != "v1.0.0" { - t.Errorf("Unexpected output: %s", output) + var err error + pwd, err = os.Getwd() + if err != nil { + t.Errorf(err.Error()) + } + remote := "upstream" + // Check remotes + checkRemoteExistence(pwd, remote) + // Fetch latest tags from remote + fetchTags(pwd, remote) + for _, mod := range modules { + v := getModuleCurrentVersion(mod) + valid, err := regexp.MatchString("^v(\\d+\\.){2}\\d+$", v) + if err != nil { + t.Errorf(err.Error()) + } + if !valid { + t.Errorf("Returned version %s is not valid", v) + } } }