代码拉取完成,页面将自动刷新
package main
import (
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
pwl "github.com/justjanne/powerline-go/powerline"
)
const rubyVersionFileSuffix = "/.ruby-version"
const globalVersionFileSuffix = "/.rbenv/version"
func runRbenvCommand(cmd string, args ...string) (string, error) {
command := exec.Command(cmd, args...)
out, err := command.Output()
return string(out), err
}
// check RBENV_VERSION variable
func checkEnvForRbenvVersion() (string, error) {
rbenvVersion := os.Getenv("RBENV_VERSION")
if len(rbenvVersion) <= 0 {
return "", errors.New("Not found in RBENV_VERSION")
}
return rbenvVersion, nil
}
// check existence of .ruby_version in tree until root path
func checkForRubyVersionFileInTree() (string, error) {
var (
workingDirectory string
err error
)
workingDirectory, err = os.Getwd()
if err == nil {
for workingDirectory != "/" {
rubyVersion, rubyVersionErr := ioutil.ReadFile(workingDirectory + rubyVersionFileSuffix)
if rubyVersionErr == nil {
return strings.TrimSpace(string(rubyVersion)), nil
}
workingDirectory = filepath.Dir(workingDirectory)
}
}
return "", errors.New("No .ruby_version file found in tree")
}
// check for global version
func checkForGlobalVersion() (string, error) {
homeDir, _ := os.UserHomeDir()
globalRubyVersion, err := ioutil.ReadFile(homeDir + globalVersionFileSuffix)
if err != nil {
return "", errors.New("No global version file found in tree")
}
return strings.TrimSpace(string(globalRubyVersion)), nil
}
// retrieve rbenv version output
func checkForRbenvOutput() (string, error) {
// spawn rbenv and print out version
out, err := runRbenvCommand("rbenv", "version")
if err != nil {
return "", errors.New("Not found in rbenv output")
}
items := strings.Split(out, " ")
if len(items) <= 0 {
return "", errors.New("Not found in rbenv output")
}
return items[0], nil
}
func segmentRbenv(p *powerline) []pwl.Segment {
var (
segment string
err error
)
segment, err = checkEnvForRbenvVersion()
if err != nil {
segment, err = checkForRubyVersionFileInTree()
}
if err != nil {
segment, err = checkForGlobalVersion()
}
if err != nil {
segment, err = checkForRbenvOutput()
}
if err != nil {
return []pwl.Segment{}
}
return []pwl.Segment{{
Name: "rbenv",
Content: segment,
Foreground: p.theme.TimeFg,
Background: p.theme.TimeBg,
}}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。