forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-docker_context.go
39 lines (34 loc) · 955 Bytes
/
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
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")
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
}
}
}
return []pwl.Segment{{
Name: "docker-context",
Content: "🐳" + context,
Foreground: p.theme.PlEnvFg,
Background: p.theme.PlEnvBg,
}}
}