forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-rvm.go
91 lines (77 loc) · 2.06 KB
/
segment-rvm.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
package main
import (
"errors"
"os"
"os/exec"
"strings"
pwl "github.com/justjanne/powerline-go/powerline"
)
func runRvmCommand(cmd string, args ...string) (string, error) {
command := exec.Command(cmd, args...)
out, err := command.Output()
return string(out), err
}
// check RUBY_VERSION variable
func checkEnvForRubyVersion() (string, error) {
rubyVersion := os.Getenv("RUBY_VERSION")
if len(rubyVersion) <= 0 {
return "", errors.New("Not found in RUBY_VERSION")
}
return rubyVersion, nil
}
// check GEM_HOME variable for gemset information
func checkEnvForRubyGemset() (string, error) {
gemHomeSegments := strings.Split(os.Getenv("GEM_HOME"), "@")
if len(gemHomeSegments) <= 1 {
return "", errors.New("Gemset not found in GEM_HOME")
}
return gemHomeSegments[1], nil
}
// retrieve ruby version from RVM
func checkForRvmOutput() (string, error) {
// ask RVM what the current ruby version is
out, err := runRvmCommand("rvm", "current")
if err != nil {
return "", errors.New("Not found in RVM output")
}
items := strings.Split(out, " ")
if len(items) <= 0 {
return "", errors.New("Not found in RVM output")
}
return items[0], nil
}
func segmentRvm(p *powerline) []pwl.Segment {
var (
segment string
err error
)
segment, err = checkEnvForRubyVersion()
if err != nil {
segment, err = checkForRubyVersionFileInTree()
}
if err != nil {
segment, err = checkForRvmOutput()
}
if err != nil {
return []pwl.Segment{}
}
// Remove explicit "ruby-" prefix from segment because it's superfluous
segment_components := strings.Split(segment, "-")
if len(segment_components) > 1 {
segment = segment_components[1]
}
// If gemset is missing from segment, get that info from the environment
segment_components = strings.Split(segment, "@")
if len(segment_components) < 2 {
gemset, err := checkEnvForRubyGemset()
if err == nil && gemset != "" {
segment = segment + "@" + gemset
}
}
return []pwl.Segment{{
Name: "rvm",
Content: p.symbols.RvmIndicator + " " + segment,
Foreground: p.theme.RvmFg,
Background: p.theme.RvmBg,
}}
}