Skip to content

Commit

Permalink
feat(env): add delimited string support
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Aug 29, 2023
1 parent 6864274 commit 553b709
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 9 deletions.
52 changes: 48 additions & 4 deletions src/shell/env.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package shell

import "github.com/jandedobbeleer/aliae/src/context"
import (
"strings"

"github.com/jandedobbeleer/aliae/src/context"
)

type Envs []*Env

type Env struct {
Name string `yaml:"name"`
Value interface{} `yaml:"value"`
If If `yaml:"if"`
Name string `yaml:"name"`
Value interface{} `yaml:"value"`
Delimiter Template `yaml:"delimiter"`
If If `yaml:"if"`

template string
}

func (e *Env) string() string {
e.join()

switch context.Current.Shell {
case ZSH, BASH:
return e.zsh().render()
Expand All @@ -33,6 +40,32 @@ func (e *Env) string() string {
}
}

func (e *Env) join() {
if len(e.Delimiter) == 0 {
return
}

text, OK := e.Value.(string)
if !OK {
return
}

splitted := strings.Split(text, "\n")
splitted = filterEmpty(splitted)
if len(splitted) == 1 {
e.Value = splitted[0]
return
}

for index, value := range splitted {
splitted[index] = strings.TrimSpace(value)
}

delimiter := e.Delimiter.String()

e.Value = strings.Join(splitted, delimiter)
}

func (e *Env) render() string {
if text, OK := e.Value.(string); OK {
template := Template(text)
Expand Down Expand Up @@ -90,3 +123,14 @@ func (e Envs) filter() Envs {

return env
}

func filterEmpty[S ~[]E, E string](s S) S {
var cleaned S
for _, a := range s {
if len(a) == 0 {
continue
}
cleaned = append(cleaned, a)
}
return cleaned
}
39 changes: 39 additions & 0 deletions src/shell/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,42 @@ $env:FOO = "bar"`,
assert.Equal(t, tc.Expected, DotFile.String(), tc.Case)
}
}

func TestEnvironmentVariableDelimiter(t *testing.T) {
cases := []struct {
Case string
Expected string
Env *Env
}{
{
Case: "No delimiter",
Expected: `$env:HELLO = "world"`,
Env: &Env{Name: "HELLO", Value: "world"},
},
{
Case: "Single value with delimiter",
Expected: `$env:HELLO = "world"`,
Env: &Env{Name: "HELLO", Value: "world\n", Delimiter: ";"},
},
{
Case: "Multiple values",
Expected: `$env:HELLO = "world;foo"`,
Env: &Env{Name: "HELLO", Value: "world\nfoo", Delimiter: ";"},
},
{
Case: "Not a string value",
Expected: `$env:HELLO = 2`,
Env: &Env{Name: "HELLO", Value: 2, Delimiter: ";"},
},
{
Case: "Multiple values, with a template delimiter",
Expected: `$env:HELLO = "world:foo"`,
Env: &Env{Name: "HELLO", Value: "world\nfoo", Delimiter: `{{ if eq .OS "windows" }};{{ else }}:{{ end }}`},
},
}

for _, tc := range cases {
context.Current = &context.Runtime{Shell: PWSH, OS: context.LINUX}
assert.Equal(t, tc.Expected, tc.Env.string(), tc.Case)
}
}
4 changes: 4 additions & 0 deletions src/shell/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (t Template) Parse() Template {
return t
}

func (t Template) String() string {
return string(t.Parse())
}

func parse(text string, ctx interface{}) (string, error) {
if !strings.Contains(text, "{{") || !strings.Contains(text, "}}") {
return text, nil
Expand Down
11 changes: 6 additions & 5 deletions website/docs/setup/env.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ env:
### Variable
| Name | Type | Description |
| ------- | -------- | ----------------------------------------------------------------------- |
| `name` | `string` | the variable name |
| `value` | `any` | the variable value, supports [templating][templates] |
| `if` | `string` | golang [template][go-text-template] conditional statement, see [if][if] |
| Name | Type | Description |
| ----------- | -------- | ------------------------------------------------------------------------------------------------------- |
| `name` | `string` | the variable name |
| `value` | `any` | the variable value, supports [templating][templates] |
| `delimiter` | `string` | if you want to join an array of string values (separated by newlines), supports [templating][templates] |
| `if` | `string` | golang [template][go-text-template] conditional statement, see [if][if] |

[templates]: templates.mdx
[go-text-template]: https://golang.org/pkg/text/template/
Expand Down

0 comments on commit 553b709

Please sign in to comment.