From d228d2994467d8fbdd15363a2971e12e092fec0a Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 16 Jun 2022 16:10:48 -0700 Subject: [PATCH] update '-v' flag behavior to include copyRight and license (#15097) ``` ~ minio -v minio version DEVELOPMENT.2022-06-16T20-40-14Z (commit-id=e083228e2a06bfdcd006fee28d449cd2b47c542a) Runtime: go1.18.3 linux/amd64 Copyright (c) 2015-2022 MinIO, Inc. Licence AGPLv3 ``` --- buildscripts/gen-ldflags.go | 15 +++++++++++---- cmd/build-constants.go | 5 ++++- cmd/main.go | 32 ++++++++++---------------------- internal/color/color.go | 7 +++++++ 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/buildscripts/gen-ldflags.go b/buildscripts/gen-ldflags.go index c7ce900bc..a7649056f 100644 --- a/buildscripts/gen-ldflags.go +++ b/buildscripts/gen-ldflags.go @@ -24,14 +24,18 @@ import ( "fmt" "os" "os/exec" + "strconv" "strings" "time" ) func genLDFlags(version string) string { + releaseTag, date := releaseTag(version) + copyrightYear := strconv.Itoa(date.Year()) ldflagsStr := "-s -w" ldflagsStr += " -X github.com/minio/minio/cmd.Version=" + version - ldflagsStr += " -X github.com/minio/minio/cmd.ReleaseTag=" + releaseTag(version) + ldflagsStr += " -X github.com/minio/minio/cmd.CopyrightYear=" + copyrightYear + ldflagsStr += " -X github.com/minio/minio/cmd.ReleaseTag=" + releaseTag ldflagsStr += " -X github.com/minio/minio/cmd.CommitID=" + commitID() ldflagsStr += " -X github.com/minio/minio/cmd.ShortCommitID=" + commitID()[:12] ldflagsStr += " -X github.com/minio/minio/cmd.GOPATH=" + os.Getenv("GOPATH") @@ -40,7 +44,7 @@ func genLDFlags(version string) string { } // genReleaseTag prints release tag to the console for easy git tagging. -func releaseTag(version string) string { +func releaseTag(version string) (string, time.Time) { relPrefix := "DEVELOPMENT" if prefix := os.Getenv("MINIO_RELEASE"); prefix != "" { relPrefix = prefix @@ -53,14 +57,17 @@ func releaseTag(version string) string { relTag := strings.Replace(version, " ", "-", -1) relTag = strings.Replace(relTag, ":", "-", -1) + t, err := time.Parse("2006-01-02T15-04-05Z", relTag) + if err != nil { + panic(err) + } relTag = strings.Replace(relTag, ",", "", -1) relTag = relPrefix + "." + relTag - if relSuffix != "" { relTag += "." + relSuffix } - return relTag + return relTag, t } // commitID returns the abbreviated commit-id hash of the last commit. diff --git a/cmd/build-constants.go b/cmd/build-constants.go index 31a1c71dc..221bcf134 100644 --- a/cmd/build-constants.go +++ b/cmd/build-constants.go @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2021 MinIO, Inc. +// Copyright (c) 2015-2022 MinIO, Inc. // // This file is part of MinIO Object Storage stack // @@ -37,4 +37,7 @@ var ( // ShortCommitID - first 12 characters from CommitID. ShortCommitID = "DEVELOPMENT.GOGET" + + // CopyrightYear - dynamic value of the copyright end year + CopyrightYear = "0000" ) diff --git a/cmd/main.go b/cmd/main.go index 1b320b064..3981e9748 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,12 +18,14 @@ package cmd import ( + "fmt" "os" "path/filepath" "runtime" "sort" "github.com/minio/cli" + "github.com/minio/minio/internal/color" "github.com/minio/pkg/console" "github.com/minio/pkg/trie" "github.com/minio/pkg/words" @@ -69,11 +71,6 @@ var GlobalFlags = []cli.Flag{ }, } -var versionFlag = cli.BoolFlag{ - Name: "version, v", - Usage: "print version information", -} - // Help template for minio. var minioHelpTemplate = `NAME: {{.Name}} - {{.Usage}} @@ -137,20 +134,16 @@ func newApp(name string) *cli.App { Name: "help, h", Usage: "show help", } - - topLevelFlags := make([]cli.Flag, len(GlobalFlags)+1) - copy(topLevelFlags, GlobalFlags) - topLevelFlags[len(GlobalFlags)] = versionFlag + cli.VersionPrinter = printMinIOVersion app := cli.NewApp() app.Name = name app.Author = "MinIO, Inc." - app.Action = versionAndHelpAction + app.Version = ReleaseTag app.Usage = "High Performance Object Storage" app.Description = `Build high performance data infrastructure for machine learning, analytics and application data workloads with MinIO` - app.Flags = topLevelFlags + app.Flags = GlobalFlags app.HideHelpCommand = true // Hide `help, h` command, we already have `minio --help`. - app.HideVersion = true app.Commands = commands app.CustomAppHelpTemplate = minioHelpTemplate app.CommandNotFound = func(ctx *cli.Context, command string) { @@ -170,16 +163,11 @@ func newApp(name string) *cli.App { return app } -func versionAndHelpAction(ctx *cli.Context) { - if ctx.IsSet("version") { - console.Printf("%s version %s\n", ctx.App.Name, ReleaseTag) - console.Printf("commit: %s\n", CommitID) - console.Printf("go version: %s\n", runtime.Version()) - - return - } - - cli.ShowAppHelpAndExit(ctx, 1) +func printMinIOVersion(c *cli.Context) { + fmt.Fprintln(c.App.Writer, color.Greenf("%s version %s (commit-id=%s)", c.App.Name, c.App.Version, CommitID)) + fmt.Fprintln(c.App.Writer, color.Greenf("Runtime: %s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)) + fmt.Fprintln(c.App.Writer, color.Greenf("Copyright (c) 2015-%s MinIO, Inc.", CopyrightYear)) + fmt.Fprintln(c.App.Writer, color.Red("Licence AGPLv3 ")) } // Main main for minio server. diff --git a/internal/color/color.go b/internal/color/color.go index d9974a92b..006f599a5 100644 --- a/internal/color/color.go +++ b/internal/color/color.go @@ -73,6 +73,13 @@ var ( return fmt.Sprint }() + Greenf = func() func(format string, a ...interface{}) string { + if IsTerminal() { + return color.New(color.FgGreen).SprintfFunc() + } + return fmt.Sprintf + }() + GreenBold = func() func(a ...interface{}) string { if IsTerminal() { return color.New(color.FgGreen, color.Bold).SprintFunc()