From cc15f832d3b41e0a454721b3df4b65627f55ee93 Mon Sep 17 00:00:00 2001 From: Kirill Zaitsev Date: Sun, 20 Mar 2016 20:55:38 +0300 Subject: [PATCH] Added grvm hook on CD --- grvm.go | 6 +++++ hook.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/grvm | 6 +++++ 3 files changed, 78 insertions(+) create mode 100644 hook.go diff --git a/grvm.go b/grvm.go index f591100..10f114c 100644 --- a/grvm.go +++ b/grvm.go @@ -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) diff --git a/hook.go b/hook.go new file mode 100644 index 0000000..842a203 --- /dev/null +++ b/hook.go @@ -0,0 +1,66 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "regexp" + + "github.com/codegangsta/cli" +) + +var gemfileRegexp = regexp.MustCompile(`^(\s*|)ruby\s*("|')(?P.*)("|')(\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() +} diff --git a/scripts/grvm b/scripts/grvm index 2c3fc9f..2a82598 100755 --- a/scripts/grvm +++ b/scripts/grvm @@ -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