feat: add terraform workspace segment

This commit is contained in:
Mats Estensen 2020-10-16 16:25:38 +02:00 committed by Jan De Dobbeleer
parent 48559f2d1e
commit c3e81f4d75
5 changed files with 117 additions and 0 deletions

View file

@ -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"
}
```

View file

@ -30,6 +30,7 @@ module.exports = {
"session",
"shell",
"spotify",
"terraform",
"text",
"time",
]

View file

@ -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
@ -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{

24
segment_terraform.go Normal file
View file

@ -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
segment_terraform_test.go Executable file
View file

@ -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())
}