forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
67 lines (59 loc) · 1.26 KB
/
version.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type Godep struct {
Deps []Dep
}
type Dep struct {
ImportPath string
Comment string
Rev string
}
func main() {
comment := false
args := os.Args[1:]
if len(args) == 3 {
if args[2] == "comment" {
comment = true
} else {
fmt.Fprintf(os.Stderr, "The third argument must be 'comment' or not specified.\n")
os.Exit(1)
}
args = args[:2]
}
if len(args) != 2 {
fmt.Fprintf(os.Stderr, "Expects two arguments, a path to the Godep.json file and a package to get the commit for (and optionally, 'comment' as the third option)\n")
os.Exit(1)
}
path := args[0]
pkg := args[1]
data, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read %s: %v\n", path, err)
os.Exit(1)
}
godeps := &Godep{}
if err := json.Unmarshal(data, godeps); err != nil {
fmt.Fprintf(os.Stderr, "Unable to read %s: %v\n", path, err)
os.Exit(1)
}
for _, dep := range godeps.Deps {
if dep.ImportPath != pkg {
continue
}
if len(dep.Rev) > 7 {
dep.Rev = dep.Rev[0:7]
}
if comment && len(dep.Comment) > 0 {
dep.Rev = dep.Comment
}
fmt.Fprintf(os.Stdout, dep.Rev)
return
}
fmt.Fprintf(os.Stderr, "Could not find %s in %s\n", pkg, path)
os.Exit(1)
}