feat(terraform): use template

This commit is contained in:
Laurent Nullens 2021-11-20 11:14:26 +01:00 committed by GitHub
parent e495a885a7
commit a71d1f7454
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 38 additions and 17 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -6,7 +6,7 @@ sidebar_label: OS
## What
Display OS specific info. Defaults to Icon.
Display OS specific info - defaults to Icon.
## Sample Configuration

View file

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

View file

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

View file

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

View file

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