Skip to content

Commit

Permalink
Add versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
mizzy committed Nov 29, 2016
1 parent 13fb011 commit 7908211
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"os"

log "github.com/Sirupsen/logrus"
Expand All @@ -12,10 +13,21 @@ import (
func main() {
const defaultConfigFile = "pipeline.yml"

var configFile string
var (
configFile string
version bool
)

flag.StringVar(&configFile, "c", defaultConfigFile, "file which define pipeline")
flag.BoolVar(&version, "v", false, "print version string")

flag.Parse()

if version {
fmt.Println(OutputVersion())
os.Exit(0)
}

p, err := pipeline.LoadFromFile(configFile)
if err != nil {
log.Fatal(err)
Expand Down
63 changes: 63 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"bytes"
"fmt"
"time"

log "github.com/Sirupsen/logrus"

latest "github.com/tcnksm/go-latest"
)

// Name is application name
const Name = "walter"

// Version is application version
const Version string = "v2.0.0beta1"

// GitCommit describes latest commit hash.
// This is automatically extracted by git describe --always.
var GitCommit string

const defaultCheckTimeout = 2 * time.Second

func OutputVersion() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s version %s", Name, Version)
if len(GitCommit) != 0 {
fmt.Fprintf(&buf, " (%s)", GitCommit)
}
fmt.Fprintf(&buf, "\n")

// Check latest version is release or not.
verCheckCh := make(chan *latest.CheckResponse)
go func() {
fixFunc := latest.DeleteFrontV()
githubTag := &latest.GithubTag{
Owner: "walter-cd",
Repository: "walter",
FixVersionStrFunc: fixFunc,
}

res, err := latest.Check(githubTag, fixFunc(Version))
if err != nil {
// Don't return error
log.Debugf("[ERROR] Check lastet version is failed: %s", err)
return
}
verCheckCh <- res
}()

select {
case <-time.After(defaultCheckTimeout):
case res := <-verCheckCh:
if res.Outdated {
fmt.Fprintf(&buf,
"Latest version of walter is v%s, please upgrade!\n",
res.Current)
}
}

return buf.String()
}

0 comments on commit 7908211

Please sign in to comment.