forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-docker_context.go
49 lines (43 loc) · 1.14 KB
/
segment-docker_context.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
package main
import (
"encoding/json"
pwl "github.com/justjanne/powerline-go/powerline"
"io/ioutil"
"os"
"path/filepath"
)
type DockerContextConfig struct {
CurrentContext string `json:"currentContext"`
}
func segmentDockerContext(p *powerline) []pwl.Segment {
context := "default"
home, _ := os.LookupEnv("HOME")
contextFolder := filepath.Join(home, ".docker", "contexts")
configFile := filepath.Join(home, ".docker", "config.json")
contextEnvVar := os.Getenv("DOCKER_CONTEXT")
if contextEnvVar != "" {
context = contextEnvVar
} else {
stat, err := os.Stat(contextFolder)
if err == nil && stat.IsDir() {
dockerConfigFile, err := ioutil.ReadFile(configFile)
if err == nil {
var dockerConfig DockerContextConfig
err = json.Unmarshal(dockerConfigFile, &dockerConfig)
if err == nil && dockerConfig.CurrentContext != "" {
context = dockerConfig.CurrentContext
}
}
}
}
// Don’t show the default context
if context == "default" {
return []pwl.Segment{}
}
return []pwl.Segment{{
Name: "docker-context",
Content: "🐳" + context,
Foreground: p.theme.PlEnvFg,
Background: p.theme.PlEnvBg,
}}
}