-
Notifications
You must be signed in to change notification settings - Fork 33
/
init.go
74 lines (66 loc) · 1.7 KB
/
init.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/LK4D4/vndr/godl"
)
func gitDep(root string) (string, error) {
revCmd := exec.Command("git", "rev-parse", "HEAD")
revCmd.Dir = root
out, err := revCmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error get revision: %v, out: %s", err, out)
}
return strings.TrimSpace(string(out)), nil
}
func hgDep(root string) (string, error) {
revCmd := exec.Command("hg", "parent", "--template", "'{node}'")
revCmd.Dir = root
out, err := revCmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error get revision: %v, out: %s", err, out)
}
return strings.TrimSpace(string(out)), nil
}
func svnDep(root string) (string, error) {
revCmd := exec.Command("svnversion")
revCmd.Dir = root
out, err := revCmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error get revision: %v, out: %s", err, out)
}
return strings.TrimSpace(string(out)), nil
}
func bzrDep(root string) (string, error) {
revCmd := exec.Command("bzr", "revno")
revCmd.Dir = root
out, err := revCmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error get revision: %v, out: %s", err, out)
}
return strings.TrimSpace(string(out)), nil
}
func getRev(v *godl.VCS) (string, error) {
switch v.Type {
case "git":
return gitDep(v.Root)
case "hg":
return hgDep(v.Root)
case "svn":
return svnDep(v.Root)
case "bzr":
return bzrDep(v.Root)
}
return "", errors.New("unknown vcs type")
}
func writeConfig(deps []depEntry, cfgFile string) error {
var lines []string
for _, d := range deps {
lines = append(lines, d.String())
}
return ioutil.WriteFile(cfgFile, []byte(strings.Join(lines, "")), os.FileMode(0666))
}