Skip to content

Commit

Permalink
Try a new version-expose scheme using go:generate
Browse files Browse the repository at this point in the history
  • Loading branch information
disq committed Dec 2, 2016
1 parent 9bb02f8 commit 09bfd15
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/peakgames/s5cmd/version"
"log"
"math"
"os"
Expand All @@ -15,8 +16,10 @@ import (
"time"
)

//go:generate go run version/cmd/generate.go
var (
GitSummary, GitBranch string
GitSummary = version.GitSummary
GitBranch = version.GitBranch
)

func printOps(name string, counter uint64, elapsed time.Duration, extra string) {
Expand Down Expand Up @@ -66,7 +69,11 @@ func main() {
flag.Parse()

if *version {
fmt.Printf("s5cmd version %s (from branch %s)\n", GitSummary, GitBranch)
fmt.Printf("s5cmd version %s", GitSummary)
if GitBranch != "" {
fmt.Printf(" (from branch %s)", GitBranch)
}
fmt.Print("\n")
os.Exit(0)
}

Expand Down
58 changes: 58 additions & 0 deletions version/cmd/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"
)

func mustRunGetResult(cmd string, arg ...string) string {
var buf bytes.Buffer

c := exec.Command(cmd)
c.Args = append(c.Args, arg...)
c.Stdout = &buf
c.Stderr = os.Stderr
err := c.Run()

if err != nil {
log.Fatal(err)
return ""
}

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"
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", " "))

timestamp := time.Now().Format(time.UnixDate)

b := bytes.NewBuffer(nil)
fmt.Fprint(b, `// This package is auto-generated using version/cmd/generate.go
package version
// AUTO-GENERATED. DO NOT EDIT
// `+timestamp+"\n"+summary+branch+"\n")
log.Printf("Writing %s...\n", destinationFile)
if err := ioutil.WriteFile(destinationFile, b.Bytes(), 0644); err != nil {
log.Fatal(err)
}
}
8 changes: 8 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This package will be auto-generated using version/cmd/generate.go
package version

// GitSummary will be the output of "git describe --tags --dirty --always"
const GitSummary = "v0.0"

// GitBranch will be the output of "git symbolic-ref -q --short HEAD"
const GitBranch = ""

0 comments on commit 09bfd15

Please sign in to comment.