From 192518d67974e8a14a6ab1d324cea71d175ab300 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 13 Aug 2015 20:50:32 -0700 Subject: [PATCH] Add new version command --- Makefile | 2 +- cmd_test.go | 6 ++-- common-methods.go | 2 +- config-main.go | 2 +- config.go | 20 ++++++------- genversion.go | 34 ++++++++--------------- globals.go | 29 ++++++++----------- main.go | 23 +++++++++++---- session-v1.go | 2 +- session-v2.go | 2 +- session.go | 2 +- version-main.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++ version.go | 26 ++--------------- 13 files changed, 133 insertions(+), 88 deletions(-) create mode 100644 version-main.go diff --git a/Makefile b/Makefile index 3d9a02c1..204c6e47 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ gomake-all: build @mkdir -p $(HOME)/.mc release: genversion - @echo "Installing minio with new version.go:" + @echo "Installing mc with new version.go:" @go install github.com/minio/mc @mkdir -p $(HOME)/.mc diff --git a/cmd_test.go b/cmd_test.go index 996217b1..49373172 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -27,9 +27,9 @@ import ( "net/http/httptest" "github.com/minio/mc/internal/github.com/minio/cli" + "github.com/minio/mc/internal/github.com/minio/minio/pkg/quick" . "github.com/minio/mc/internal/gopkg.in/check.v1" "github.com/minio/mc/pkg/console" - "github.com/minio/mc/internal/github.com/minio/minio/pkg/quick" ) var customConfigDir string @@ -56,9 +56,9 @@ func (s *CmdTestSuite) SetUpSuite(c *C) { // For windows the path is slightly different. if runtime.GOOS == "windows" { - customConfigDir = filepath.Join(tmpDir, mcConfigWindowsDir) + customConfigDir = filepath.Join(tmpDir, globalMCConfigWindowsDir) } else { - customConfigDir = filepath.Join(tmpDir, mcConfigDir) + customConfigDir = filepath.Join(tmpDir, globalMCConfigDir) } setMcConfigDir(customConfigDir) diff --git a/common-methods.go b/common-methods.go index d6b804f4..0889353c 100644 --- a/common-methods.go +++ b/common-methods.go @@ -157,7 +157,7 @@ func getNewClient(urlStr string, auth hostConfig) (client.Client, *probe.Error) return auth.SecretAccessKey }() s3Config.AppName = "Minio" - s3Config.AppVersion = getVersion() + s3Config.AppVersion = globalMCVersion s3Config.AppComments = []string{os.Args[0], runtime.GOOS, runtime.GOARCH} s3Config.HostURL = urlStr s3Config.Debug = globalDebugFlag diff --git a/config-main.go b/config-main.go index b8a7db22..ad0ffe1e 100644 --- a/config-main.go +++ b/config-main.go @@ -21,8 +21,8 @@ import ( "github.com/minio/mc/internal/github.com/minio/cli" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" - "github.com/minio/mc/pkg/console" "github.com/minio/mc/internal/github.com/minio/minio/pkg/quick" + "github.com/minio/mc/pkg/console" ) // Configure minio client diff --git a/config.go b/config.go index ea7015a8..94716820 100644 --- a/config.go +++ b/config.go @@ -24,8 +24,8 @@ import ( "sync" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" - "github.com/minio/mc/pkg/console" "github.com/minio/mc/internal/github.com/minio/minio/pkg/quick" + "github.com/minio/mc/pkg/console" ) type configV1 struct { @@ -57,9 +57,9 @@ func getMcConfigDir() (string, *probe.Error) { // For windows the path is slightly different switch runtime.GOOS { case "windows": - return filepath.Join(u.HomeDir, mcConfigWindowsDir), nil + return filepath.Join(u.HomeDir, globalMCConfigWindowsDir), nil default: - return filepath.Join(u.HomeDir, mcConfigDir), nil + return filepath.Join(u.HomeDir, globalMCConfigDir), nil } } @@ -88,7 +88,7 @@ func getMcConfigPath() (string, *probe.Error) { if err != nil { return "", err.Trace() } - return filepath.Join(dir, mcConfigFile), nil + return filepath.Join(dir, globalMCConfigFile), nil } // mustGetMcConfigPath - similar to getMcConfigPath, ignores errors @@ -181,10 +181,10 @@ func migrateConfigV1ToV101() { conf = config.Data().(*configV1) // version is the same return - if conf.Version == mcCurrentConfigVersion { + if conf.Version == globalMCCurrentConfigVersion { return } - conf.Version = mcCurrentConfigVersion + conf.Version = globalMCCurrentConfigVersion localHostConfig := hostConfig{} localHostConfig.AccessKeyID = "" @@ -209,13 +209,13 @@ func migrateConfigV1ToV101() { perr = newConfig.Save(mustGetMcConfigPath()) fatalIf(perr) - console.Infof("Successfully migrated %s from version: %s to version: %s\n", mustGetMcConfigPath(), mcPreviousConfigVersion, mcCurrentConfigVersion) + console.Infof("Successfully migrated %s from version: %s to version: %s\n", mustGetMcConfigPath(), globalMCPreviousConfigVersion, globalMCCurrentConfigVersion) } // newConfigV1() - get new config version 1.0.0 func newConfigV1() *configV1 { conf := new(configV1) - conf.Version = mcPreviousConfigVersion + conf.Version = globalMCPreviousConfigVersion // make sure to allocate map's otherwise Golang // exits silently without providing any errors conf.Hosts = make(map[string]hostConfig) @@ -226,7 +226,7 @@ func newConfigV1() *configV1 { // newConfigV101() - get new config version 1.0.1 func newConfigV101() *configV1 { conf := new(configV1) - conf.Version = mcCurrentConfigVersion + conf.Version = globalMCCurrentConfigVersion // make sure to allocate map's otherwise Golang // exits silently without providing any errors conf.Hosts = make(map[string]hostConfig) @@ -253,7 +253,7 @@ func newConfigV101() *configV1 { dlHostConfig.AccessKeyID = "" dlHostConfig.SecretAccessKey = "" - conf.Hosts[exampleHostURL] = exampleHostConf + conf.Hosts[globalExampleHostURL] = exampleHostConf conf.Hosts["localhost:*"] = localHostConfig conf.Hosts["127.0.0.1:*"] = localHostConfig conf.Hosts["s3*.amazonaws.com"] = s3HostConf diff --git a/genversion.go b/genversion.go index 142edfe1..e5961dda 100644 --- a/genversion.go +++ b/genversion.go @@ -20,6 +20,7 @@ package main import ( "fmt" + "net/http" "os" "text/template" "time" @@ -27,37 +28,17 @@ import ( type Version struct { Date string - Tag string } func writeVersion(version Version) error { var versionTemplate = `// -------- DO NOT EDIT -------- -// this is an autogenerated file +// This file is autogenerated by genversion.go during the release process. package main -import ( - "net/http" - "time" -) - // Version autogenerated -var Version = {{if .Date}}"{{.Date}}"{{else}}""{{end}} +const Version = {{if .Date}}"{{.Date}}"{{else}}""{{end}} -// Tag is of following format -// -// RELEASE.[WeekDay]-[Month]-[Day]-[Hour]-[Min]-[Sec]-GMT-[Year] -// -var Tag = {{if .Tag}}"{{.Tag}}"{{else}}""{{end}} - -// getVersion - -func getVersion() string { - t, _ := time.Parse(time.RFC3339Nano, Version) - if t.IsZero() { - return "" - } - return t.Format(http.TimeFormat) -} ` t := template.Must(template.New("version").Parse(versionTemplate)) versionFile, err := os.OpenFile("version.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) @@ -75,6 +56,10 @@ func getVersion() string { func genVersion() { t := time.Now().UTC() date := t.Format(time.RFC3339Nano) + // Tag is of following format + // + // RELEASE.[WeekDay]-[Month]-[Day]-[Hour]-[Min]-[Sec]-GMT-[Year] + // tag := fmt.Sprintf( "RELEASE.%s-%s-%02d-%02d-%02d-%02d-GMT-%d", t.Weekday().String()[0:3], @@ -84,12 +69,15 @@ func genVersion() { t.Minute(), t.Second(), t.Year()) - version := Version{Date: date, Tag: tag} + fmt.Println("Release-Tag: " + tag) + fmt.Println("Release-Version: " + t.Format(http.TimeFormat)) + version := Version{Date: date} err := writeVersion(version) if err != nil { fmt.Print(err) os.Exit(1) } + fmt.Println("Successfully generated ‘version.go’") } func main() { diff --git a/globals.go b/globals.go index d7ab87ab..a5ac2084 100644 --- a/globals.go +++ b/globals.go @@ -22,32 +22,27 @@ var ( globalMimicFlag = false // Unix flag set via command line globalJSONFlag = false // Json flag set via command line globalDebugFlag = false // Debug flag set via command line - - mcPreviousConfigVersion = "1.0.0" - mcCurrentConfigVersion = "1.0.1" ) // mc configuration related constants. const ( - mcConfigDir = ".mc/" - mcConfigWindowsDir = "mc\\" - mcConfigFile = "config.json" -) + globalMCPreviousConfigVersion = "1.0.0" + globalMCCurrentConfigVersion = "1.0.1" + globalMCVersion = Version -// session config related constants -const ( - sessionDir = "session" -) + globalMCConfigDir = ".mc/" + globalMCConfigWindowsDir = "mc\\" + globalMCConfigFile = "config.json" -// default access and secret key -const ( + // session config related constants + globalSessionDir = "session" + + // default access and secret key // do not pass accesskeyid and secretaccesskey through cli // users should manually edit them, add a stub entry globalAccessKeyID = "YOUR-ACCESS-KEY-ID-HERE" globalSecretAccessKey = "YOUR-SECRET-ACCESS-KEY-HERE" -) -// default host -const ( - exampleHostURL = "YOUR-EXAMPLE.COM" + // default host + globalExampleHostURL = "YOUR-EXAMPLE.COM" ) diff --git a/main.go b/main.go index cf464871..7639f87d 100644 --- a/main.go +++ b/main.go @@ -18,10 +18,12 @@ package main import ( "fmt" + "net/http" "os" "os/user" "runtime" "strconv" + "time" "github.com/minio/mc/internal/github.com/minio/cli" "github.com/minio/mc/internal/github.com/minio/pb" @@ -69,7 +71,6 @@ func getSystemData() map[string]string { "PLATFORM": platform, "RUNTIME": goruntime, "MEM": mem, - "TAG": Tag, } } @@ -102,6 +103,15 @@ 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 registerApp() *cli.App { // Register all the commands registerCmd(lsCmd) // List contents of a bucket @@ -115,6 +125,7 @@ func registerApp() *cli.App { registerCmd(accessCmd) // set permissions [public, private, readonly, authenticated] for buckets and folders. registerCmd(configCmd) // generate configuration "/home/harsha/.mc/config.json" file. registerCmd(updateCmd) // update Check for new software updates + registerCmd(versionCmd) // print version // register all the flags registerFlag(configFlag) // path to config folder @@ -126,9 +137,9 @@ func registerApp() *cli.App { app := cli.NewApp() app.Usage = "Minio Client for cloud storage and filesystems" - app.Version = getVersion() + // hide --version flag, version is a command + app.HideVersion = true app.Commands = commands - app.Compiled = getVersion() app.Flags = flags app.Author = "Minio.io" app.CustomAppHelpTemplate = `NAME: @@ -144,8 +155,9 @@ GLOBAL FLAGS: {{range .Flags}}{{.}} {{end}}{{end}} VERSION: - {{if .Compiled}} - {{.Compiled}}{{end}} + +` + getFormattedVersion() + + ` {{range $key, $value := ExtraInfo}} {{$key}}: {{$value}} @@ -163,6 +175,7 @@ func main() { app := registerApp() app.Before = registerBefore + app.ExtraInfo = func() map[string]string { if globalDebugFlag { return getSystemData() diff --git a/session-v1.go b/session-v1.go index 34956ef2..41f6ab1b 100644 --- a/session-v1.go +++ b/session-v1.go @@ -25,8 +25,8 @@ import ( "time" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" - "github.com/minio/mc/pkg/console" "github.com/minio/mc/internal/github.com/minio/minio/pkg/quick" + "github.com/minio/mc/pkg/console" ) type sessionV1 struct { diff --git a/session-v2.go b/session-v2.go index 838f2339..fd778271 100644 --- a/session-v2.go +++ b/session-v2.go @@ -25,8 +25,8 @@ import ( "time" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" - "github.com/minio/mc/pkg/console" "github.com/minio/mc/internal/github.com/minio/minio/pkg/quick" + "github.com/minio/mc/pkg/console" ) // migrateSessionV1ToV2 migrates all session files from v1 to v2. diff --git a/session.go b/session.go index c7330eba..5e2f0f12 100644 --- a/session.go +++ b/session.go @@ -52,7 +52,7 @@ func getSessionDir() string { if err != nil { // TODO: revamp error handling -ab. Do not pass errors mindlessly to upper layer for a tool like mc. } - return filepath.Join(configDir, sessionDir) + return filepath.Join(configDir, globalSessionDir) } var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") diff --git a/version-main.go b/version-main.go new file mode 100644 index 00000000..39864ae7 --- /dev/null +++ b/version-main.go @@ -0,0 +1,71 @@ +/* + * 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 ( + "encoding/json" + "net/http" + "time" + + "github.com/minio/mc/internal/github.com/minio/cli" + "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" + "github.com/minio/mc/pkg/console" +) + +var versionCmd = cli.Command{ + Name: "version", + Usage: "Print version", + Action: mainVersion, + CustomHelpTemplate: `NAME: + mc {{.Name}} - {{.Usage}} + +USAGE: + mc {{.Name}} {{if .Description}} + +DESCRIPTION: + {{.Description}}{{end}}{{if .Flags}} + +FLAGS: + {{range .Flags}}{{.}} + {{end}}{{ end }} + +EXAMPLES: +`, +} + +func mainVersion(ctxx *cli.Context) { + t, _ := time.Parse(time.RFC3339Nano, Version) + if t.IsZero() { + console.Println("") + return + } + type Version struct { + Value time.Time `json:"value"` + Format string `json:"format"` + } + if globalJSONFlag { + tB, err := json.MarshalIndent(struct { + Version Version `json:"version"` + }{Version: Version{t, "RFC3339Nano"}}, "", "\t") + if err != nil { + console.Fatalln(probe.NewError(err)) + } + console.Println(console.JSON(string(tB))) + return + } + console.Println(t.Format(http.TimeFormat)) +} diff --git a/version.go b/version.go index f5d8d4fe..1d646b9f 100644 --- a/version.go +++ b/version.go @@ -1,29 +1,7 @@ // -------- DO NOT EDIT -------- -// this is an autogenerated file +// This file is autogenerated by genversion.go during the release process. package main -import ( - "net/http" - "time" -) - // Version autogenerated -var Version = "2015-06-24T21:56:09.753217354Z" - -// Tag is of following format -// -// [STRING]-[EPOCH] -// -// STRING is release string of your choice. -// EPOCH is unix seconds since Jan 1, 1970 UTC. -var Tag = "release-1435182969" - -// getVersion - -func getVersion() string { - t, _ := time.Parse(time.RFC3339Nano, Version) - if t.IsZero() { - return "" - } - return t.Format(http.TimeFormat) -} +const Version = "2015-08-14T03:23:47.250240049Z"