Skip to content

Commit

Permalink
Merge pull request #351 from scottslowe/issue-350
Browse files Browse the repository at this point in the history
Add support for DOCKER_CONTEXT environment variable
  • Loading branch information
justjanne authored Apr 19, 2022
2 parents b8eeb5a + 4964354 commit 400f147
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 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 Down

0 comments on commit 400f147

Please sign in to comment.