Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add terraform workspace segment
- Loading branch information
1 parent
48559f2
commit c3e81f4
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ module.exports = { | |
"session", | ||
"shell", | ||
"spotify", | ||
"terraform", | ||
"text", | ||
"time", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |