diff --git a/src/shell/env.go b/src/shell/env.go index 6c17bbf..73afad5 100644 --- a/src/shell/env.go +++ b/src/shell/env.go @@ -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() @@ -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) @@ -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 +} diff --git a/src/shell/env_test.go b/src/shell/env_test.go index b7682d3..1e0a704 100644 --- a/src/shell/env_test.go +++ b/src/shell/env_test.go @@ -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) + } +} diff --git a/src/shell/template.go b/src/shell/template.go index 198ca68..a8c6f05 100644 --- a/src/shell/template.go +++ b/src/shell/template.go @@ -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 diff --git a/website/docs/setup/env.mdx b/website/docs/setup/env.mdx index 22fed2a..6064372 100644 --- a/website/docs/setup/env.mdx +++ b/website/docs/setup/env.mdx @@ -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/