Skip to content

Commit

Permalink
Added grvm hook on CD
Browse files Browse the repository at this point in the history
  • Loading branch information
kzaitsev committed Mar 20, 2016
1 parent ce8164f commit cc15f83
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions grvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func main() {
Usage: "Get upgrade information",
Action: GetUpgrade,
},
{
Name: "hook",
Aliases: []string{"h"},
Usage: "Hook to read Gemfile",
Action: GetHook,
},
}

app.Run(os.Args)
Expand Down
66 changes: 66 additions & 0 deletions hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
}
6 changes: 6 additions & 0 deletions scripts/grvm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ function __grvmFn {
esac
}

function cd {
builtin cd $*
arg=`$HOME/.grvm/bin/grvm -s hook`
__grvmEval $arg
}

alias grvm=__grvmFn

__grvmFn init

0 comments on commit cc15f83

Please sign in to comment.