mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: add terraform workspace segment
This commit is contained in:
parent
48559f2d1e
commit
c3e81f4d75
25
docs/docs/segment-terraform.md
Normal file
25
docs/docs/segment-terraform.md
Normal 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"
|
||||||
|
}
|
||||||
|
```
|
|
@ -30,6 +30,7 @@ module.exports = {
|
||||||
"session",
|
"session",
|
||||||
"shell",
|
"shell",
|
||||||
"spotify",
|
"spotify",
|
||||||
|
"terraform",
|
||||||
"text",
|
"text",
|
||||||
"time",
|
"time",
|
||||||
]
|
]
|
||||||
|
|
|
@ -69,6 +69,8 @@ const (
|
||||||
Kubectl SegmentType = "kubectl"
|
Kubectl SegmentType = "kubectl"
|
||||||
//Dotnet writes which dotnet version is currently active
|
//Dotnet writes which dotnet version is currently active
|
||||||
Dotnet SegmentType = "dotnet"
|
Dotnet SegmentType = "dotnet"
|
||||||
|
//Terraform writes the terraform workspace we're currently in
|
||||||
|
Terraform SegmentType = "terraform"
|
||||||
//Powerline writes it Powerline style
|
//Powerline writes it Powerline style
|
||||||
Powerline SegmentStyle = "powerline"
|
Powerline SegmentStyle = "powerline"
|
||||||
//Plain writes it without ornaments
|
//Plain writes it without ornaments
|
||||||
|
@ -126,6 +128,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
|
||||||
Az: &az{},
|
Az: &az{},
|
||||||
Kubectl: &kubectl{},
|
Kubectl: &kubectl{},
|
||||||
Dotnet: &dotnet{},
|
Dotnet: &dotnet{},
|
||||||
|
Terraform: &terraform{},
|
||||||
}
|
}
|
||||||
if writer, ok := functions[segment.Type]; ok {
|
if writer, ok := functions[segment.Type]; ok {
|
||||||
props := &properties{
|
props := &properties{
|
||||||
|
|
24
segment_terraform.go
Normal file
24
segment_terraform.go
Normal 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
64
segment_terraform_test.go
Executable 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())
|
||||||
|
}
|
Loading…
Reference in a new issue