diff --git a/e2e/flags_test.go b/e2e/flags_test.go new file mode 100644 index 000000000..59b9c3e80 --- /dev/null +++ b/e2e/flags_test.go @@ -0,0 +1,21 @@ +package e2e + +import ( + "testing" + + "gotest.tools/v3/icmd" +) + +func TestFlagVersion(t *testing.T) { + t.Parallel() + + _, s5cmd, cleanup := setup(t) + defer cleanup() + + cmd := s5cmd("-version") + result := icmd.RunCmd(cmd) + + // make sure that -version flag works as expected: + // https://github.com/peak/s5cmd/issues/70#issuecomment-592218542 + result.Assert(t, icmd.Success) +} diff --git a/main.go b/main.go index 2def62e58..319c1893b 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,5 @@ +//go:generate go run version/cmd/generate.go + package main import ( @@ -20,12 +22,6 @@ import ( "github.com/peak/s5cmd/version" ) -//go:generate go run version/cmd/generate.go -var ( - GitSummary = version.GitSummary - GitBranch = version.GitBranch -) - func printOps(name string, counter uint64, elapsed time.Duration, extra string) { if counter == 0 { return @@ -61,10 +57,9 @@ func main() { os.Exit(0) } - // validation must be done after the completion - if err := flags.Validate(); err != nil { - log.Print(err) - os.Exit(2) + if *flags.ShowVersion { + fmt.Println(version.GetHumanVersion()) + os.Exit(0) } if *flags.EnableGops || os.Getenv("S5CMD_GOPS") != "" { @@ -73,13 +68,10 @@ func main() { } } - if *flags.ShowVersion { - fmt.Printf("s5cmd version %s", GitSummary) - if GitBranch != "" { - fmt.Printf(" (from branch %s)", GitBranch) - } - fmt.Print("\n") - os.Exit(0) + // validation must be done after the completion + if err := flags.Validate(); err != nil { + log.Print(err) + os.Exit(2) } cmd := strings.Join(flag.Args(), " ") diff --git a/version/cmd/generate.go b/version/cmd/generate.go index 7b550c2e4..d62a38089 100644 --- a/version/cmd/generate.go +++ b/version/cmd/generate.go @@ -9,10 +9,9 @@ import ( "os" "os/exec" "strings" - "time" ) -func mustRunGetResult(cmd string, arg ...string) string { +func cmdOutput(cmd string, arg ...string) string { var buf bytes.Buffer c := exec.Command(cmd) @@ -29,37 +28,34 @@ func mustRunGetResult(cmd string, arg ...string) string { return strings.Trim(buf.String(), "\n\r ") } -func commandToConst(name, command string, args []string) string { - data := mustRunGetResult(command, args...) - - ret := "\n// " + name + " is the output of \"" + command + " " + strings.Join(args, " ") + "\"\n" - switch name { - case "GitSummary": - ret += "// For release builds, manually edit this to reflect the released version tag.\n" - case "GitBranch": - ret += "// For release builds this should be left empty.\n" - } - ret += "const " + name + ` = "` + data + `"` + "\n" - - return ret -} - const destinationFile = "version/version.go" func main() { - summary := commandToConst("GitSummary", "git", strings.Split("describe --tags --dirty --always", " ")) - branch := commandToConst("GitBranch", "git", strings.Split("symbolic-ref -q --short HEAD", " ")) + gitTag := cmdOutput("git", strings.Split("describe --tags --abbrev=0", " ")...) + gitCommit := cmdOutput("git", strings.Split("rev-parse HEAD", " ")...) + gitCommit = gitCommit[:6] - timestamp := time.Now().Format(time.UnixDate) + var buf bytes.Buffer + fmt.Fprint(&buf, "// Package version is auto-generated using version/cmd/generate.go on non-release builds.\n") + fmt.Fprint(&buf, "package version\n\n") + fmt.Fprint(&buf, "// Code generated by version/cmd/generate.go. DO NOT EDIT.\n\n") + fmt.Fprintf(&buf, "import %q\n", "strings") + fmt.Fprintln(&buf, "") + fmt.Fprintf(&buf, "const Version = %q\n", gitTag) + fmt.Fprintf(&buf, "const GitCommit = %q\n", gitCommit) + fmt.Fprint(&buf, ` +func GetHumanVersion() string { + version := Version + if !strings.HasPrefix(version, "v") { + version = "v" + Version + } - b := bytes.NewBuffer(nil) - fmt.Fprint(b, `// Package version is auto-generated using version/cmd/generate.go on non-release builds. -package version + return Version + "-" + GitCommit +} +`) -// Code generated by version/cmd/generate.go. DO NOT EDIT. -// `+timestamp+"\n"+summary+branch) log.Printf("Writing %s...\n", destinationFile) - if err := ioutil.WriteFile(destinationFile, b.Bytes(), 0644); err != nil { + if err := ioutil.WriteFile(destinationFile, buf.Bytes(), 0644); err != nil { log.Fatal(err) } } diff --git a/version/version.go b/version/version.go index 6953c708b..549199194 100644 --- a/version/version.go +++ b/version/version.go @@ -1,10 +1,18 @@ -// Package version will be auto-generated using version/cmd/generate.go on non-release builds. +// Package version is auto-generated using version/cmd/generate.go on non-release builds. package version -// GitSummary will be the output of "git describe --tags --dirty --always" -// For release builds, manually edit this to reflect the released version tag. -const GitSummary = "v0.7.0" +// Code generated by version/cmd/generate.go. DO NOT EDIT. -// GitBranch will be the output of "git symbolic-ref -q --short HEAD" -// For release builds this should be left empty. -const GitBranch = "" +import "strings" + +const Version = "v0.7.0" +const GitCommit = "2a123c" + +func GetHumanVersion() string { + version := Version + if !strings.HasPrefix(version, "v") { + version = "v" + Version + } + + return Version + "-" + GitCommit +}