From fd1a00a0b035dc9c8622fd141844b605d74799f5 Mon Sep 17 00:00:00 2001 From: Aleksei Karnov Date: Fri, 30 Apr 2021 21:25:57 +0300 Subject: [PATCH] terraform-workspace: use TF CLI --- segment-terraform_workspace.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/segment-terraform_workspace.go b/segment-terraform_workspace.go index cf65fbf0..1eea5c7c 100644 --- a/segment-terraform_workspace.go +++ b/segment-terraform_workspace.go @@ -1,29 +1,34 @@ package main import ( - "io/ioutil" "os" + "os/exec" + "strings" pwl "github.com/justjanne/powerline-go/powerline" ) -const wsFile = "./.terraform/environment" +const tfDirName = "./.terraform" + +func runTfCommand(cmd string, args ...string) (string, error) { + command := exec.Command(cmd, args...) + out, err := command.Output() + return string(out), err +} func segmentTerraformWorkspace(p *powerline) []pwl.Segment { - stat, err := os.Stat(wsFile) - if err != nil { + if _, err := os.Stat(tfDirName); os.IsNotExist(err) { return []pwl.Segment{} } - if stat.IsDir() { - return []pwl.Segment{} - } - workspace, err := ioutil.ReadFile(wsFile) + + workspace, err := runTfCommand("terraform", "workspace", "show") if err != nil { return []pwl.Segment{} } + return []pwl.Segment{{ Name: "terraform-workspace", - Content: string(workspace), + Content: strings.TrimSpace(workspace), Foreground: p.theme.TFWsFg, Background: p.theme.TFWsBg, }}