Skip to content

Commit

Permalink
feat(template): match shell name function
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jul 31, 2023
1 parent 42aa7bd commit 79caecc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/shell/if_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ func TestIf(t *testing.T) {
If: `eq .Shell "pwsh"`,
Expected: true,
},
{
Case: "Only two shells",
If: `match .Shell "bash" "zsh"`,
Expected: false,
},
{
Case: "Only two shells",
If: `match .Shell "pwsh" "nu"`,
Expected: true,
},
}

for _, tc := range cases {
Expand Down
10 changes: 10 additions & 0 deletions src/shell/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func funcMap() template.FuncMap {
"formatString": formatString,
"cleanString": cleanString,
"env": os.Getenv,
"match": match,
}
return template.FuncMap(funcMap)
}
Expand Down Expand Up @@ -76,3 +77,12 @@ func cleanString(variable interface{}) interface{} {
return variable
}
}

func match(variable string, values ...string) bool {
for _, value := range values {
if variable == value {
return true
}
}
return false
}
13 changes: 7 additions & 6 deletions website/docs/setup/templates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ configuration across multiple environments resulting in environment specific val
Out of the box you get the following functionality:
| Name | Type | Description | Example |
| -------- | ---------- | ----------------------------------------------------------- | ------------------------------------------------------------------ |
| `.Shell` | `string` | the current shell name | `{{ .Shell }}` |
| `.Home` | `string` | the user's `$HOME` folder | `{{ .Home }}/go/bin/aliae` |
| `.OS` | `string` | the current operating system (`windows`, `darwin`, `linux`) | `{{ .Home }}/go/bin/aliae{{ if eq .OS \"windows\" }}.exe{{ end }}` |
| `env` | `function` | retrieve an environment variable value | `{{ env \"POSH_THEME\" }}` |
| Name | Type | Description | Example |
| -------- | ---------- | ----------------------------------------------------------- | ---------------------------------------------------------------- |
| `.Shell` | `string` | the current shell name | `{{ .Shell }}` |
| `.Home` | `string` | the user's `$HOME` folder | `{{ .Home }}/go/bin/aliae` |
| `.OS` | `string` | the current operating system (`windows`, `darwin`, `linux`) | `{{ .Home }}/go/bin/aliae{{ if eq .OS "windows" }}.exe{{ end }}` |
| `env` | `function` | retrieve an environment variable value | `{{ env "POSH_THEME" }}` |
| `match` | `function` | match a shell name to one or multiple options | `{{ match .Shell "zsh" "bash" }}` |

[go-text-template]: https://golang.org/pkg/text/template/

0 comments on commit 79caecc

Please sign in to comment.