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
|
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
pwl "github.com/justjanne/powerline-go/powerline"
)
const gcloudCoreSectionHeader = "\n[core]\n"
func getCloudConfigDir() (string, error) {
p, err := os.UserHomeDir()
if err != nil {
return "", err
}
if runtime.GOOS != "windows" {
p += "/.config"
}
p += "/gcloud"
return p, nil
}
func getActiveGCloudConfig(configDir string) (string, error) {
activeConfigPath := configDir + "/active_config"
stat, err := os.Stat(activeConfigPath)
if (err == nil && os.IsNotExist(err)) || (err == nil && stat.IsDir()) {
return "default", nil
} else if err != nil {
return "", err
}
contents, err := ioutil.ReadFile(activeConfigPath)
if err != nil {
return "", err
}
config := strings.TrimSpace(string(contents))
if config == "" {
config = "default"
}
return config, nil
}
func getGCPProjectFromGCloud() (string, error) {
out, err := exec.Command("gcloud", "config", "list", "project", "--format", "value(core.project)").Output()
if err != nil {
return "", err
}
return strings.TrimSuffix(string(out), "\n"), nil
}
func getGCPProjectFromFile() (string, error) {
configDir, err := getCloudConfigDir()
if err != nil {
return "", err
}
activeConfig, err := getActiveGCloudConfig(configDir)
if err != nil {
return "", err
}
configPath := configDir + "/configurations/config_" + activeConfig
stat, err := os.Stat(configPath)
if err != nil {
return "", err
} else if stat.IsDir() {
return "", fmt.Errorf("%s is a directory", configPath)
}
b, err := ioutil.ReadFile(configPath)
if err != nil {
return "", err
}
b = append([]byte("\n"), b...)
coreStart := bytes.Index(b, []byte(gcloudCoreSectionHeader))
if coreStart == -1 {
return "", fmt.Errorf("could not find [core] section in %s", configPath)
}
b = b[coreStart+len(gcloudCoreSectionHeader):]
coreEnd := bytes.Index(b, []byte("\n["))
if coreEnd != -1 {
b = b[:coreEnd]
}
lines := bytes.Split(b[coreStart+len(gcloudCoreSectionHeader):coreEnd], []byte("\n"))
for _, line := range lines {
parts := bytes.Split(line, []byte("="))
if len(parts) == 2 {
if strings.TrimSpace(string(parts[0])) == "project" {
return strings.TrimSpace(string(parts[1])), nil
}
}
}
return "", nil
}
func getGCPProject() (string, error) {
if project, err := getGCPProjectFromFile(); err == nil {
return project, nil
} else {
return getGCPProjectFromGCloud()
}
}
func segmentGCP(p *powerline) []pwl.Segment {
project, err := getGCPProject()
if err != nil {
log.Fatal(err)
}
if project == "" {
return []pwl.Segment{}
}
return []pwl.Segment{{
Name: "gcp",
Content: project,
Foreground: p.theme.GCPFg,
Background: p.theme.GCPBg,
}}
}
|