Skip to content

Commit

Permalink
feat: add terraform workspace segment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mats Estensen authored and JanDeDobbeleer committed Oct 19, 2020
1 parent 48559f2 commit c3e81f4
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/docs/segment-terraform.md
@@ -0,0 +1,25 @@
---
id: terraform
title: Terraform Context
sidebar_label: Terraform
---

## What

Display the currently active Terraform Workspace name.

Note:
- Will need a terraform binary in your PATH
- Will only be displayed in directories that contain a `.terraform` subdirectory

## Sample Configuration

```json
{
"type": "terraform",
"style": "powerline",
"powerline_symbol": "\uE0B0",
"foreground": "#000000",
"background": "#ebcc34"
}
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Expand Up @@ -30,6 +30,7 @@ module.exports = {
"session",
"shell",
"spotify",
"terraform",
"text",
"time",
]
Expand Down
3 changes: 3 additions & 0 deletions segment.go
Expand Up @@ -69,6 +69,8 @@ const (
Kubectl SegmentType = "kubectl"
//Dotnet writes which dotnet version is currently active
Dotnet SegmentType = "dotnet"
//Terraform writes the terraform workspace we're currently in
Terraform SegmentType = "terraform"
//Powerline writes it Powerline style
Powerline SegmentStyle = "powerline"
//Plain writes it without ornaments
Expand Down Expand Up @@ -126,6 +128,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
Az: &az{},
Kubectl: &kubectl{},
Dotnet: &dotnet{},
Terraform: &terraform{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{
Expand Down
24 changes: 24 additions & 0 deletions segment_terraform.go
@@ -0,0 +1,24 @@
package main

type terraform struct {
props *properties
env environmentInfo
workspaceName string
}

func (tf *terraform) string() string {
return tf.workspaceName
}

func (tf *terraform) init(props *properties, env environmentInfo) {
tf.props = props
tf.env = env
}

func (tf *terraform) enabled() bool {
if !tf.env.hasCommand("terraform") || !tf.env.hasFolder(".terraform") {
return false
}
tf.workspaceName, _ = tf.env.runCommand("terraform", "workspace", "show")
return true
}
64 changes: 64 additions & 0 deletions segment_terraform_test.go
@@ -0,0 +1,64 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

type terraformArgs struct {
hasTfCommand bool
hasTfFolder bool
workspaceName string
}

func bootStrapTerraformTest(args *terraformArgs) *terraform {
env := new(MockedEnvironment)
env.On("hasCommand", "terraform").Return(args.hasTfCommand)
env.On("hasFolder", ".terraform").Return(args.hasTfFolder)
env.On("runCommand", "terraform", []string{"workspace", "show"}).Return(args.workspaceName, nil)
k := &terraform{
env: env,
props: &properties{},
}
return k
}

func TestTerraformWriterDisabled(t *testing.T) {
args := &terraformArgs{
hasTfCommand: false,
hasTfFolder: false,
}
terraform := bootStrapTerraformTest(args)
assert.False(t, terraform.enabled())
}

func TestTerraformMissingDir(t *testing.T) {
args := &terraformArgs{
hasTfCommand: true,
hasTfFolder: false,
}
terraform := bootStrapTerraformTest(args)
assert.False(t, terraform.enabled())
}

func TestTerraformMissingBinary(t *testing.T) {
args := &terraformArgs{
hasTfCommand: false,
hasTfFolder: true,
}
terraform := bootStrapTerraformTest(args)
assert.False(t, terraform.enabled())
}

func TestTerraformEnabled(t *testing.T) {
expected := "default"
args := &terraformArgs{
hasTfCommand: true,
hasTfFolder: true,
workspaceName: expected,
}
terraform := bootStrapTerraformTest(args)
assert.True(t, terraform.enabled())
assert.Equal(t, expected, terraform.string())
}

0 comments on commit c3e81f4

Please sign in to comment.