Skip to content

Commit

Permalink
version: fix -version flag usage (peak#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
igungor committed Feb 28, 2020
1 parent 66767d7 commit f6bff1b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 50 deletions.
21 changes: 21 additions & 0 deletions e2e/flags_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
26 changes: 9 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:generate go run version/cmd/generate.go

package main

import (
Expand All @@ -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
Expand Down Expand Up @@ -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") != "" {
Expand All @@ -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(), " ")
Expand Down
48 changes: 22 additions & 26 deletions version/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
22 changes: 15 additions & 7 deletions version/version.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f6bff1b

Please sign in to comment.