-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
183 lines (144 loc) · 3.59 KB
/
helpers.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/boltdb/bolt"
"github.com/codegangsta/cli"
)
func getDB() (*bolt.DB, error) {
return bolt.Open(dbPath, 0600, nil)
}
func Print(c *cli.Context, args ...string) {
if c.GlobalBool("shell") {
fmt.Println(fmt.Sprintf("echo %q", strings.Join(args, " ")))
} else {
fmt.Println(strings.Join(args, " "))
}
}
func Export(key, value string) string {
return fmt.Sprintf("export %s=%s", key, value)
}
func printEnv(rubyVersion string) {
newPaths := rebuildPaths()
if rubyVersion == "system" {
fmt.Println(Export("PATH", newPaths))
} else {
gemsPath := fmt.Sprintf("%s/%s", gemsDirectory, rubyVersion)
fmt.Println(Export("GEM_HOME", gemsPath))
fmt.Println(Export("GEM_PATH", gemsPath))
currentRubyBin := fmt.Sprintf("%s/%s/bin", rubiesDirectory, rubyVersion)
currentGemsBin := fmt.Sprintf("%s/bin", gemsPath)
path := fmt.Sprintf("%s:%s:%s", currentRubyBin, currentGemsBin, newPaths)
fmt.Println(Export("PATH", path))
}
fmt.Println(Export("grvm_ruby", rubyVersion))
}
func rebuildPaths() string {
paths := strings.Split(currentPathEnv, ":")
var currentPath = fmt.Sprintf("%s/%s", currentHomeEnv, ".grvm")
var newPaths []string
for _, p := range paths {
if !strings.HasPrefix(p, currentPath) {
newPaths = append(newPaths, p)
}
}
return strings.Join(newPaths, ":")
}
func updateRubiesBucket(b *bolt.Bucket) error {
buffer := bytes.NewBuffer(make([]byte, 0))
cmd := exec.Command(rubyBuildExecutable, "--definitions")
cmd.Stdout = buffer
cmd.Stderr = buffer
if err := cmd.Run(); err != nil {
return err
}
rubies := strings.Split(string(buffer.Bytes()), "\n")
for _, ruby := range rubies {
if len(ruby) != 0 {
rubyDirectory := fmt.Sprintf("%s/%s", rubiesDirectory, ruby)
if _, err := os.Stat(rubyDirectory); err == nil {
b.Put([]byte(ruby), []byte(rubyDirectory))
} else {
b.Put([]byte(ruby), make([]byte, 0))
}
}
}
return nil
}
func updateAvailableRubiesWithTx(tx *bolt.Tx) error {
b, err := getBucket(tx, []byte("rubies"))
if err != nil {
return err
}
return updateRubiesBucket(b)
}
func updateAvailableRubies() error {
db, err := getDB()
if err != nil {
return err
}
defer db.Close()
tx, err := db.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
if err := updateAvailableRubiesWithTx(tx); err != nil {
return err
}
return tx.Commit()
}
func getDefaultRuby(c *cli.Context) (string, error) {
db, err := getDB()
if err != nil {
return "", err
}
defer db.Close()
tx, err := db.Begin(true)
if err != nil {
return "", err
}
defer tx.Rollback()
b, err := getBucket(tx, []byte("settings"))
if err != nil {
return "", err
}
defaultRuby := b.Get([]byte("default"))
if defaultRuby == nil {
return "system", nil
} else if len(defaultRuby) == 0 {
return "system", nil
} else {
candidate := string(defaultRuby)
if err := checkCandidate(tx, candidate); err != nil {
return "system", nil
} else {
return candidate, nil
}
}
}
func getBucket(tx *bolt.Tx, bucket []byte) (*bolt.Bucket, error) {
var b *bolt.Bucket
var e error
b, e = tx.CreateBucket(bucket)
if e == bolt.ErrBucketExists {
return tx.Bucket(bucket), nil
} else if e == nil {
return b, e
} else {
return nil, e
}
}
func checkCandidate(tx *bolt.Tx, candidate string) error {
rubies := tx.Bucket([]byte("rubies"))
value := rubies.Get([]byte(candidate))
if value == nil {
return fmt.Errorf("No candidate to set: %s", candidate)
} else if len(value) == 0 {
return fmt.Errorf("%s not installed, please use: grvm install %s", candidate, candidate)
}
return nil
}