From 6295a1910e32e9ae3a4a28b328aa2c4f5cb48fa0 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 22 Sep 2015 14:25:52 -0700 Subject: [PATCH] Fix mcVersion and keep it in http.TimeFormat by default --- Makefile | 2 +- buildscripts/genversion.go | 77 ++++++++++++++++++++++++++++++++++++++ globals.go | 2 +- main.go | 13 +------ update-main.go | 14 +++---- version-main.go | 35 ++++++++++------- version.go | 4 +- 7 files changed, 110 insertions(+), 37 deletions(-) create mode 100644 buildscripts/genversion.go diff --git a/Makefile b/Makefile index 13644bac..3716fe0c 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,7 @@ release: genversion genversion: @echo "Generating a new version.go:" - @go run genversion.go + @GO15VENDOREXPERIMENT=1 go run buildscripts/genversion.go coverage: @GO15VENDOREXPERIMENT=1 go test -race -coverprofile=cover.out ./ diff --git a/buildscripts/genversion.go b/buildscripts/genversion.go new file mode 100644 index 00000000..b7887503 --- /dev/null +++ b/buildscripts/genversion.go @@ -0,0 +1,77 @@ +// +build ignore + +/* + * Minio Client (C) 2014, 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "fmt" + "net/http" + "os" + "strings" + "text/template" + "time" +) + +var versionTemplate = `// -------- DO NOT EDIT -------- +// This file is autogenerated by genversion.go during the release process. + +package main + +const mcVersion = {{if .Version}}"{{.Version}}"{{else}}""{{end}} +const mcReleaseTag = {{if .ReleaseTag}}"{{.ReleaseTag}}"{{else}}""{{end}} +` + +// genVersion generates ‘version.go’. +func genVersion(version string) { + t := template.Must(template.New("version").Parse(versionTemplate)) + versionFile, err := os.OpenFile("version.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + fmt.Printf("genversion: Unable to generate ‘version.go’. Error: %s.\n", err) + os.Exit(1) + } + defer versionFile.Close() + + err = t.Execute(versionFile, struct { + Version string + ReleaseTag string + }{version, genReleaseTag(version)}) + + if err != nil { + fmt.Printf("genversion: Unable to generate ‘version.go’. Error: %s.\n", err) + os.Exit(1) + } +} + +// genReleaseTag prints release tag to the console for easy git tagging. +func genReleaseTag(version string) string { + relTag := strings.Replace(version, " ", "-", -1) + relTag = strings.Replace(relTag, ":", "-", -1) + relTag = strings.Replace(relTag, ",", "", -1) + return "RELEASE." + relTag +} + +func main() { + // Version is in HTTP TimeFormat. + version := time.Now().UTC().Format(http.TimeFormat) + + // generate ‘version.go’ file. + genVersion(version) + + fmt.Println("Version: \"" + version + "\"") + fmt.Println("Release-Tag: " + genReleaseTag(version)) +} diff --git a/globals.go b/globals.go index 1b9c4d80..473836f3 100644 --- a/globals.go +++ b/globals.go @@ -27,7 +27,7 @@ var ( // mc configuration related constants. const ( globalMCConfigVersion = "5" - globalMCVersion = Version + globalMCVersion = mcVersion globalMCConfigDir = ".mc/" globalMCConfigWindowsDir = "mc\\" diff --git a/main.go b/main.go index f714887d..7ea465e6 100644 --- a/main.go +++ b/main.go @@ -18,12 +18,10 @@ package main import ( "fmt" - "net/http" "os" "os/user" "runtime" "strconv" - "time" "github.com/minio/cli" "github.com/minio/mc/pkg/console" @@ -110,15 +108,6 @@ func registerBefore(ctx *cli.Context) error { return nil } -// getFormattedVersion - -func getFormattedVersion() string { - t, _ := time.Parse(time.RFC3339Nano, Version) - if t.IsZero() { - return "" - } - return t.Format(http.TimeFormat) -} - func findClosestCommands(command string) []string { var closestCommands []string for _, value := range commandsTree.PrefixMatch(command) { @@ -170,7 +159,7 @@ GLOBAL FLAGS: {{range .Flags}}{{.}} {{end}}{{end}} VERSION: - ` + getFormattedVersion() + + ` + mcVersion + `{{ "\n"}} {{range $key, $value := ExtraInfo}} {{$key}}: {{$value}} diff --git a/update-main.go b/update-main.go index 7a293933..c9935ede 100644 --- a/update-main.go +++ b/update-main.go @@ -58,9 +58,9 @@ type Updates struct { // UpdateMessage container to hold update messages type UpdateMessage struct { - Update bool `json:"update"` - Download string `json:"downloadURL"` - Version time.Time `json:"version"` + Update bool `json:"update"` + Download string `json:"downloadURL"` + Version string `json:"version"` } // String colorized update message @@ -126,7 +126,7 @@ func getExperimentalUpdate() { data, _, err := clnt.GetObject(0, 0) fatalIf(err.Trace(mcExperimentalURL), "Unable to read from experimental URL ‘"+mcExperimentalURL+"’.") - current, e := time.Parse(time.RFC3339Nano, Version) + current, e := time.Parse(http.TimeFormat, mcVersion) fatalIf(probe.NewError(e), "Unable to parse Version string as time.") if current.IsZero() { @@ -153,7 +153,7 @@ func getExperimentalUpdate() { updateMessage := UpdateMessage{ Download: downloadURL, - Version: latest, + Version: mcVersion, } if latest.After(current) { updateMessage.Update = true @@ -168,7 +168,7 @@ func getReleaseUpdate() { data, _, err := clnt.GetObject(0, 0) fatalIf(err.Trace(mcUpdateURL), "Unable to read from update URL ‘"+mcUpdateURL+"’.") - current, e := time.Parse(time.RFC3339Nano, Version) + current, e := time.Parse(http.TimeFormat, mcVersion) fatalIf(probe.NewError(e), "Unable to parse Version string as time.") if current.IsZero() { @@ -191,7 +191,7 @@ func getReleaseUpdate() { downloadURL := mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] updateMessage := UpdateMessage{ Download: downloadURL, - Version: latest, + Version: mcVersion, } if latest.After(current) { updateMessage.Update = true diff --git a/version-main.go b/version-main.go index 7b86a589..b7638fa6 100644 --- a/version-main.go +++ b/version-main.go @@ -18,9 +18,9 @@ package main import ( "encoding/json" - "net/http" - "time" + "fmt" + "github.com/fatih/color" "github.com/minio/cli" "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/probe" @@ -44,24 +44,31 @@ func mainVersion(ctx *cli.Context) { if ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "version", 1) // last argument is exit code } - t, _ := time.Parse(time.RFC3339Nano, Version) - if t.IsZero() { - console.Println("") - return - } - type Version struct { - Value time.Time `json:"value"` - Format string `json:"format"` - } + console.SetCustomTheme(map[string]*color.Color{ + "Version": color.New(color.FgGreen, color.Bold), + }) if globalJSONFlag { tB, e := json.Marshal( struct { - Version Version `json:"version"` - }{Version: Version{t, "RFC3339Nano"}}, + Version struct { + Value string `json:"value"` + Format string `json:"format"` + } `json:"version"` + }{ + Version: struct { + Value string `json:"value"` + Format string `json:"format"` + }{ + Value: mcVersion, + Format: "RFC2616", + }, + }, ) fatalIf(probe.NewError(e), "Unable to construct version string.") console.Println(string(tB)) return } - console.Println(t.Format(http.TimeFormat)) + msg := console.Colorize("Version", fmt.Sprintf("Version: %s\n", mcVersion)) + msg += console.Colorize("Version", fmt.Sprintf("Release-Tag: %s", mcReleaseTag)) + console.Println(msg) } diff --git a/version.go b/version.go index f12fccc5..786e18f8 100644 --- a/version.go +++ b/version.go @@ -3,5 +3,5 @@ package main -// Version autogenerated -const Version = "2015-09-22T04:02:16.008862161Z" +const mcVersion = "Tue, 22 Sep 2015 21:15:45 GMT" +const mcReleaseTag = "RELEASE.Tue-22-Sep-2015-21-15-45-GMT"