/
travis_cli.go
90 lines (79 loc) · 2.05 KB
/
travis_cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"flag"
"log"
"os"
"github.com/timonv/travis-cli/adapter"
"github.com/timonv/travis-cli/githelper"
)
func main() {
owner := flag.String("owner", "", "owner of the repository")
repo := flag.String("repo", "", "name of the repository")
branch := flag.String("branch", "", "name of the branch")
flag.Parse()
gh := githelper.NewGitHelper()
r := fixOwnerRepo(*owner, *repo, gh)
b := fixBranch(*branch, gh)
if r.Owner != "" || r.Name != "" {
adapter := adapter.NewAdapter(r.Owner, r.Name)
builds := adapter.GetBuilds()
if len(builds) > 0 {
build := getCorrectBuild(builds, b.Name)
if build.Branch == "" {
log.Fatal("Could not get build")
}
printBuild(build)
} else {
log.Fatal("Could not get build status")
}
}
}
func getCorrectBuild(builds []adapter.Build, branch string) adapter.Build {
var correct adapter.Build
for _, build := range builds {
if build.Finished_at != "" && build.Branch == branch {
correct = build
break
}
}
return correct
}
func fixOwnerRepo(o string, r string, gh githelper.GitHelper) githelper.GitRepo {
var repo githelper.GitRepo
if o != "" && r != "" {
repo = githelper.GitRepo{Owner: o, Name: r}
} else {
repo = gh.GetRepo()
}
return repo
}
func fixBranch(b string, gh githelper.GitHelper) githelper.GitBranch {
var branch githelper.GitBranch
if b != "" {
branch = githelper.GitBranch{Name: b}
} else {
branch = gh.CurrentBranch()
}
return branch
}
func printBuild(build adapter.Build) {
red :="\x1b[31m"
green := "\x1b[32m"
reset := "\x1b[0m"
result := build.HumanResult()
if result == "Passed" {
result = green + result + reset
} else {
result = red + result + reset
}
os.Stdout.Write([]byte(result))
os.Stdout.Write([]byte("\t"))
os.Stdout.Write([]byte(build.Branch))
os.Stdout.Write([]byte("\t"))
os.Stdout.Write([]byte(build.HumanCommit()))
os.Stdout.Write([]byte("\t"))
os.Stdout.Write([]byte(build.Finished_at))
os.Stdout.Write([]byte("\t"))
os.Stdout.Write([]byte(build.Finished_at))
os.Stdout.Write([]byte("\n"))
}