-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhook.go
66 lines (54 loc) · 1.17 KB
/
hook.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"github.com/codegangsta/cli"
)
var gemfileRegexp = regexp.MustCompile(`^(\s*|)ruby\s*("|')(?P<version>.*)("|')(\s|)$`)
func GetHook(c *cli.Context) {
var candidate string
db, err := getDB()
if err != nil {
Print(c, "Cannot open database file:", dbPath)
os.Exit(1)
}
defer db.Close()
tx, err := db.Begin(false)
if err != nil {
Print(c, err.Error())
os.Exit(1)
}
defer tx.Rollback()
dir, err := os.Getwd()
if err != nil {
Print(c, err.Error())
os.Exit(1)
}
gemfilePath := fmt.Sprintf("%s/%s", dir, "Gemfile")
if _, err := os.Stat(fmt.Sprintf("%s/%s", dir, "Gemfile")); err == nil {
file, err := os.Open(gemfilePath)
if err != nil {
Print(c, err.Error())
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
scanResult := gemfileRegexp.FindStringSubmatch(scanner.Text())
if len(scanResult) > 3 && len(scanResult[3]) > 0 {
candidate = scanResult[3]
break
}
}
if len(candidate) > 0 {
if err := checkCandidate(tx, candidate); err != nil {
Print(c, err.Error())
os.Exit(1)
}
printEnv(candidate)
}
}
tx.Commit()
}