Skip to content

Commit

Permalink
feat: add hasCommand function to find executables
Browse files Browse the repository at this point in the history
resolves #60
  • Loading branch information
JanDeDobbeleer committed Nov 25, 2023
1 parent 573f64f commit 35a6520
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
}
},
"go.formatTool": "gofmt",
Expand Down
2 changes: 1 addition & 1 deletion src/core/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func Init(configPath, sh string, printOutput bool) string {
return errorString
}

aliae.Paths.Render()
aliae.Aliae.Render()
aliae.Envs.Render()
aliae.Paths.Render()
aliae.Scripts.Render()

script := shell.DotFile.String()
Expand Down
4 changes: 4 additions & 0 deletions src/shell/aliae.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (a Aliae) Render() {
continue
}

if first && DotFile.Len() > 0 {
DotFile.WriteString("\n\n")
}

if !first {
DotFile.WriteString("\n")
}
Expand Down
7 changes: 7 additions & 0 deletions src/shell/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"text/template"

Expand Down Expand Up @@ -53,6 +54,7 @@ func funcMap() template.FuncMap {
"escapeString": escapeString,
"env": os.Getenv,
"match": match,
"hasCommand": hasCommand,
}
return template.FuncMap(funcMap)
}
Expand Down Expand Up @@ -91,3 +93,8 @@ func match(variable string, values ...string) bool {
}
return false
}

func hasCommand(command string) bool {
_, err := exec.LookPath(command)
return err == nil
}
17 changes: 9 additions & 8 deletions website/docs/setup/templates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ 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 }}` |
| `.Arch` | `string` | the `aliae` executable's compiled architecture | `{{ .Home }}/go/bin/aliae-{{ .Arch }}{{ 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" }}` |
| 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 }}` |
| `.Arch` | `string` | the `aliae` executable's compiled architecture | `{{ .Home }}/go/bin/aliae-{{ .Arch }}{{ if eq .OS "windows" }}.exe{{ end }}` |
| `env` | `string` | retrieve an environment variable value | `{{ env "POSH_THEME" }}` |
| `match` | `boolean` | match a shell name to one or multiple options | `{{ match .Shell "zsh" "bash" }}` |
| `hasCommand` | `boolean` | check if an executable exists | `{{ hasCommand "oh-my-posh" }}` |

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

0 comments on commit 35a6520

Please sign in to comment.