Skip to content

Commit

Permalink
Expose DOCKER_CONTEXT variable
Browse files Browse the repository at this point in the history
Update segment-docker_context.go to look for DOCKER_CONTEXT environment variable, and use that when setting the value of the segment.

Signed-off-by: Scott Lowe <[email protected]>
  • Loading branch information
scottslowe committed Apr 19, 2022
1 parent 4cbdaf1 commit 829a50f
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions segment-docker_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ func segmentDockerContext(p *powerline) []pwl.Segment {
home, _ := os.LookupEnv("HOME")
contextFolder := filepath.Join(home, ".docker", "contexts")
configFile := filepath.Join(home, ".docker", "config.json")
contextEnvVar := os.Getenv("DOCKER_CONTEXT")

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
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
}
}
}
}
Expand All @@ -37,7 +42,7 @@ func segmentDockerContext(p *powerline) []pwl.Segment {

return []pwl.Segment{{
Name: "docker-context",
Content: "🐳" + context,
Content: context,
Foreground: p.theme.PlEnvFg,
Background: p.theme.PlEnvBg,
}}
Expand Down

0 comments on commit 829a50f

Please sign in to comment.