forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
60 lines (52 loc) · 1.41 KB
/
env.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
package env
import (
"errors"
"os"
"path"
"path/filepath"
"strings"
)
const (
DefaultVtDataRoot = "/vt"
)
// VtRoot returns $VTROOT or tries to guess its value if it's not set.
// This is the root for the 'vt' distribution, which contains bin/vtaction
// for instance.
func VtRoot() (root string, err error) {
if root = os.Getenv("VTROOT"); root != "" {
return root, nil
}
command, err := filepath.Abs(os.Args[0])
if err != nil {
return
}
dir := path.Dir(command)
if strings.HasSuffix(dir, "/bin") {
return path.Dir(dir), nil
}
err = errors.New("VTROOT could not be guessed from the executable location. Please set $VTROOT.")
return
}
// VtDataRoot returns $VTDATAROOT or the default if $VTDATAROOT is not
// set. VtDataRoot does not check if the directory exists and is
// writable.
func VtDataRoot() string {
if dataRoot := os.Getenv("VTDATAROOT"); dataRoot != "" {
return dataRoot
}
return DefaultVtDataRoot
}
// VtMysqlRoot returns the root for the mysql distribution, which
// contains bin/mysql CLI for instance.
func VtMysqlRoot() (string, error) {
// if the environment variable is set, use that
if root := os.Getenv("VT_MYSQL_ROOT"); root != "" {
return root, nil
}
// otherwise let's use VTROOT
root, err := VtRoot()
if err != nil {
return "", errors.New("VT_MYSQL_ROOT is not set and could not be guessed from the executable location. Please set $VT_MYSQL_ROOT.")
}
return root, nil
}