Repository: JanDeDobbeleer/oh-my-posh
Pull request #1258 (2021-11-19 14:05:41)

feat(terraform): use template
### Prerequisites

- [x] I have read and understood the `CONTRIBUTING` guide
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added / updated (for bug fixes/features)

### Description

template for terraform.

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/docs/docs/config-block.md b/docs/docs/config-block.md
index df1ee541..af9d8c3c 100644
--- a/docs/docs/config-block.md
+++ b/docs/docs/config-block.md
@@ -39,7 +39,7 @@ Supported on [ZSH][rprompt], Bash and Powershell.
 
 ### Newline
 
-Start the block on a new line. Defaults to `false`.
+Start the block on a new line - defaults to `false`.
 
 ### Alignment
 
diff --git a/docs/docs/config-transient.mdx b/docs/docs/config-transient.mdx
index 65e2d8a1..d41e0121 100644
--- a/docs/docs/config-transient.mdx
+++ b/docs/docs/config-transient.mdx
@@ -47,7 +47,7 @@ The configuration has the following properties:
 - background: `string` [color][colors]
 - foreground: `string` [color][colors]
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to `{{ .Shell }}> `
+properties below - defaults to `{{ .Shell }}> `
 
 ## Template Properties
 
diff --git a/docs/docs/segment-aws.md b/docs/docs/segment-aws.md
index b6d71d29..f1d5e71d 100644
--- a/docs/docs/segment-aws.md
+++ b/docs/docs/segment-aws.md
@@ -27,7 +27,7 @@ Display the currently active AWS profile and region.
 ## Properties
 
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to `{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}`
+properties below - defaults to `{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}`
 - display_default: `boolean` - display the segment or not when the user profile matches `default` - defaults
 to `true`
 
diff --git a/docs/docs/segment-az.mdx b/docs/docs/segment-az.mdx
index 72508008..93149387 100644
--- a/docs/docs/segment-az.mdx
+++ b/docs/docs/segment-az.mdx
@@ -32,7 +32,7 @@ To enable this, set `$env:AZ_ENABLED = $true` in your `$PROFILE`.
 ## Properties
 
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to `{{.Name}}`
+properties below - defaults to `{{.Name}}`
 
 ## Template Properties
 
diff --git a/docs/docs/segment-battery.md b/docs/docs/segment-battery.md
index 34808302..f8b6f179 100644
--- a/docs/docs/segment-battery.md
+++ b/docs/docs/segment-battery.md
@@ -36,7 +36,7 @@ Battery displays the remaining power percentage for your battery.
 ## Properties
 
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to `{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}`
+properties below - defaults to `{{.Icon}}{{ if not .Error }}{{.Percentage}}{{ end }}{{.Error}}`
 - display_error: `boolean` - show the error context when failing to retrieve the battery information - defaults to `false`
 - charging_icon: `string` - icon to display on the left when charging - defaults to empty
 - discharging_icon: `string` - icon to display on the left when discharging - defaults to empty
diff --git a/docs/docs/segment-environment.md b/docs/docs/segment-environment.md
index 982a0a52..3f625592 100644
--- a/docs/docs/segment-environment.md
+++ b/docs/docs/segment-environment.md
@@ -51,7 +51,7 @@ The segment will show when the value of the environment variable isn't empty.
 
 - var_name: `string` - the name of the environment variable
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to the value of the environment variable.
+properties below - defaults to the value of the environment variable.
 
 ## Template Properties
 
diff --git a/docs/docs/segment-git.mdx b/docs/docs/segment-git.mdx
index 79cded01..763553af 100644
--- a/docs/docs/segment-git.mdx
+++ b/docs/docs/segment-git.mdx
@@ -54,7 +54,7 @@ An alternative is to use the [Posh-Git segment][poshgit]
 ## Properties
 
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to empty.
+properties below - defaults to empty.
 
 ### Fetching information
 
diff --git a/docs/docs/segment-kubectl.md b/docs/docs/segment-kubectl.md
index e722db8d..9ccdf5ca 100644
--- a/docs/docs/segment-kubectl.md
+++ b/docs/docs/segment-kubectl.md
@@ -27,7 +27,7 @@ Display the currently active Kubernetes context name and namespace name.
 ## Properties
 
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to `{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}`
+properties below - defaults to `{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}`
 - display_error: `boolean` - show the error context when failing to retrieve the kubectl information - defaults to `false`
 
 ## Template Properties
diff --git a/docs/docs/segment-nbgv.mdx b/docs/docs/segment-nbgv.mdx
index a840ef1a..2b0a24b3 100644
--- a/docs/docs/segment-nbgv.mdx
+++ b/docs/docs/segment-nbgv.mdx
@@ -30,7 +30,7 @@ The Nerdbank.GitVersioning CLI can be a bit slow causing the prompt to feel slow
 ## Properties
 
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to `{{ .Version }}`
+properties below - defaults to `{{ .Version }}`
 
 ## Template Properties
 
diff --git a/docs/docs/segment-os.md b/docs/docs/segment-os.md
index 489b6171..98f149a6 100644
--- a/docs/docs/segment-os.md
+++ b/docs/docs/segment-os.md
@@ -6,7 +6,7 @@ sidebar_label: OS
 
 ## What
 
-Display OS specific info. Defaults to Icon.
+Display OS specific info - defaults to Icon.
 
 ## Sample Configuration
 
diff --git a/docs/docs/segment-owm.md b/docs/docs/segment-owm.md
index 711f5b49..4f892ed0 100644
--- a/docs/docs/segment-owm.md
+++ b/docs/docs/segment-owm.md
@@ -48,7 +48,7 @@ The free tier for *Current weather and forecasts collection* is sufficient.
 - http_timeout: `int` - The default timeout for http request is 20ms.
 - cache_timeout: `int` - The default timeout for request caching is 10m. A value of 0 disables the cache.
 - template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
-properties below. Defaults to `{{.Weather}} ({{.Temperature}}{{.UnitIcon}})`
+properties below - defaults to `{{.Weather}} ({{.Temperature}}{{.UnitIcon}})`
 
 ## Template Properties
 
diff --git a/docs/docs/segment-terraform.md b/docs/docs/segment-terraform.md
index 5a6ba2c7..8e1be815 100644
--- a/docs/docs/segment-terraform.md
+++ b/docs/docs/segment-terraform.md
@@ -20,6 +20,16 @@ This requires a terraform binary in your PATH and will only show in directories
   "style": "powerline",
   "powerline_symbol": "\uE0B0",
   "foreground": "#000000",
-  "background": "#ebcc34"
+  "background": "#ebcc34",
+  "properties": {
+    "template": "{{.WorkspaceName}}"
+  },
 }
 ```
+
+- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
+properties below - defaults to `{{ .WorkspaceName }}> `
+
+## Template Properties
+
+- `.WorkspaceName`: `string` - is the current workspace name
diff --git a/src/segment_terraform.go b/src/segment_terraform.go
index 0188ad75..be992fc8 100644
--- a/src/segment_terraform.go
+++ b/src/segment_terraform.go
@@ -3,11 +3,21 @@ package main
 type terraform struct {
 	props         *properties
 	env           environmentInfo
-	workspaceName string
+	WorkspaceName string
 }
 
 func (tf *terraform) string() string {
-	return tf.workspaceName
+	segmentTemplate := tf.props.getString(SegmentTemplate, "{{.WorkspaceName}}")
+	template := &textTemplate{
+		Template: segmentTemplate,
+		Context:  tf,
+		Env:      tf.env,
+	}
+	text, err := template.render()
+	if err != nil {
+		return err.Error()
+	}
+	return text
 }
 
 func (tf *terraform) init(props *properties, env environmentInfo) {
@@ -17,9 +27,9 @@ func (tf *terraform) init(props *properties, env environmentInfo) {
 
 func (tf *terraform) enabled() bool {
 	cmd := "terraform"
-	if !tf.env.hasCommand(cmd) || !tf.env.hasFolder(".terraform") {
+	if !tf.env.hasCommand(cmd) || !tf.env.hasFolder(tf.env.getcwd()+"/.terraform") {
 		return false
 	}
-	tf.workspaceName, _ = tf.env.runCommand(cmd, "workspace", "show")
+	tf.WorkspaceName, _ = tf.env.runCommand(cmd, "workspace", "show")
 	return true
 }


Test patch:
diff --git a/src/segment_terraform_test.go b/src/segment_terraform_test.go
index dc98ec99..a513a571 100644
--- a/src/segment_terraform_test.go
+++ b/src/segment_terraform_test.go
@@ -15,7 +15,8 @@ type terraformArgs struct {
 func bootStrapTerraformTest(args *terraformArgs) *terraform {
 	env := new(MockedEnvironment)
 	env.On("hasCommand", "terraform").Return(args.hasTfCommand)
-	env.On("hasFolder", ".terraform").Return(args.hasTfFolder)
+	env.On("hasFolder", "/.terraform").Return(args.hasTfFolder)
+	env.On("getcwd", nil).Return("")
 	env.On("runCommand", "terraform", []string{"workspace", "show"}).Return(args.workspaceName, nil)
 	k := &terraform{
 		env:   env,