From 8c2f3235474c91df0627ce32ebbb9e37c47a8b30 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 26 Jul 2023 23:18:24 +0200 Subject: [PATCH] refactor: align model definition --- src/config/config.go | 6 ++-- src/core/init.go | 4 +-- src/shell/aliae.go | 2 +- src/shell/aliae_test.go | 24 ++++++------- src/shell/cmd.go | 6 ++-- src/shell/env.go | 14 ++++---- src/shell/env_test.go | 30 ++++++++-------- src/shell/fish.go | 8 ++--- src/shell/nu.go | 12 +++---- src/shell/path.go | 30 ++++++++-------- src/shell/path_test.go | 68 ++++++++++++++++++------------------ src/shell/pwsh.go | 12 +++---- src/shell/pwsh_test.go | 16 ++++----- src/shell/tcsh.go | 6 ++-- src/shell/xonsh.go | 10 +++--- src/shell/zsh.go | 12 +++---- website/docs/setup/alias.mdx | 8 ++--- 17 files changed, 134 insertions(+), 134 deletions(-) diff --git a/src/config/config.go b/src/config/config.go index fbb372e..6efea07 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -32,9 +32,9 @@ var ( ) type Aliae struct { - Aliae shell.Aliae `yaml:"aliae"` - Env shell.Env `yaml:"env"` - Path shell.Path `yaml:"path"` + Aliae shell.Aliae `yaml:"alias"` + Envs shell.Envs `yaml:"env"` + Paths shell.Paths `yaml:"path"` } func LoadConfig(configPath string) (*Aliae, error) { diff --git a/src/core/init.go b/src/core/init.go index f2b0cbf..ff72a8a 100644 --- a/src/core/init.go +++ b/src/core/init.go @@ -25,8 +25,8 @@ func Init(configPath, sh string, printOutput bool) string { } aliae.Aliae.Render() - aliae.Env.Render() - aliae.Path.Render() + aliae.Envs.Render() + aliae.Paths.Render() script := shell.Script.String() diff --git a/src/shell/aliae.go b/src/shell/aliae.go index ea05d45..dd44113 100644 --- a/src/shell/aliae.go +++ b/src/shell/aliae.go @@ -13,7 +13,7 @@ var ( type Aliae []*Alias type Alias struct { - Alias string `yaml:"alias"` + Name string `yaml:"name"` Value Template `yaml:"value"` Type Type `yaml:"type"` If If `yaml:"if"` diff --git a/src/shell/aliae_test.go b/src/shell/aliae_test.go index a7251fe..ad82416 100644 --- a/src/shell/aliae_test.go +++ b/src/shell/aliae_test.go @@ -8,7 +8,7 @@ import ( ) func TestAliasCommand(t *testing.T) { - alias := &Alias{Alias: "foo", Value: "bar"} + alias := &Alias{Name: "foo", Value: "bar"} cases := []struct { Case string Shell string @@ -68,7 +68,7 @@ func TestAliasFunction(t *testing.T) { cases := []struct { Case string Shell string - Alias string + Name string Expected string }{ { @@ -116,7 +116,7 @@ def __foo(): }, { Case: "XONSH - illegal character", - Alias: "foo-bar", + Name: "foo-bar", Shell: XONSH, Expected: `@aliases.register("foo-bar") def __foobar(): @@ -139,10 +139,10 @@ def __foobar(): } for _, tc := range cases { - alias := &Alias{Alias: "foo", Value: "bar", Type: Function} + alias := &Alias{Name: "foo", Value: "bar", Type: Function} - if len(tc.Alias) > 0 { - alias.Alias = tc.Alias + if len(tc.Name) > 0 { + alias.Name = tc.Name } context.Current = &context.Runtime{Shell: tc.Shell} @@ -159,21 +159,21 @@ func TestAliaeRender(t *testing.T) { { Case: "Single alias", Aliae: Aliae{ - &Alias{Alias: "FOO", Value: "bar"}, + &Alias{Name: "FOO", Value: "bar"}, }, Expected: `alias FOO="bar"`, }, { Case: "Invalid type", Aliae: Aliae{ - &Alias{Alias: "FOO", Value: "bar", Type: "invalid"}, + &Alias{Name: "FOO", Value: "bar", Type: "invalid"}, }, }, { Case: "Double alias", Aliae: Aliae{ - &Alias{Alias: "FOO", Value: "bar"}, - &Alias{Alias: "BAR", Value: "foo"}, + &Alias{Name: "FOO", Value: "bar"}, + &Alias{Name: "BAR", Value: "foo"}, }, Expected: `alias FOO="bar" alias BAR="foo"`, @@ -181,7 +181,7 @@ alias BAR="foo"`, { Case: "Filtered out", Aliae: Aliae{ - &Alias{Alias: "FOO", Value: "bar", If: `eq .Shell "fish"`}, + &Alias{Name: "FOO", Value: "bar", If: `eq .Shell "fish"`}, }, }, } @@ -218,7 +218,7 @@ func TestAliasWithTemplate(t *testing.T) { } for _, tc := range cases { - alias := &Alias{Alias: "a", Value: tc.Value} + alias := &Alias{Name: "a", Value: tc.Value} context.Current = &context.Runtime{Shell: BASH, Home: "/Users/jan", OS: "windows"} assert.Equal(t, tc.Expected, alias.string(), tc.Case) } diff --git a/src/shell/cmd.go b/src/shell/cmd.go index c26a0bb..aa6862c 100644 --- a/src/shell/cmd.go +++ b/src/shell/cmd.go @@ -8,7 +8,7 @@ const ( func (a *Alias) cmd() *Alias { if a.Type == Command { - a.template = `local p = assert(io.popen("doskey {{ .Alias }}={{ .Value }}")) + a.template = `local p = assert(io.popen("doskey {{ .Name }}={{ .Value }}")) p:close()` } @@ -23,12 +23,12 @@ print(message)` return e } -func (e *Variable) cmd() *Variable { +func (e *Env) cmd() *Env { e.template = `os.setenv("{{ .Name }}", {{ formatString .Value }})` return e } -func (p *PathEntry) cmd() *PathEntry { +func (p *Path) cmd() *Path { p.template = `os.setenv("PATH", "{{ cleanString .Value }};" .. os.getenv("PATH"))` return p } diff --git a/src/shell/env.go b/src/shell/env.go index 47ba0ee..d6da3e3 100644 --- a/src/shell/env.go +++ b/src/shell/env.go @@ -2,9 +2,9 @@ package shell import "github.com/jandedobbeleer/aliae/src/context" -type Env []*Variable +type Envs []*Env -type Variable struct { +type Env struct { Name string `yaml:"name"` Value interface{} `yaml:"value"` If If `yaml:"if"` @@ -12,7 +12,7 @@ type Variable struct { template string } -func (e *Variable) string() string { +func (e *Env) string() string { switch context.Current.Shell { case ZSH, BASH: return e.zsh().render() @@ -33,7 +33,7 @@ func (e *Variable) string() string { } } -func (e *Variable) render() string { +func (e *Env) render() string { if text, OK := e.Value.(string); OK { template := Template(text) e.Value = template.Parse() @@ -47,7 +47,7 @@ func (e *Variable) render() string { return script } -func (e Env) Render() { +func (e Envs) Render() { e = e.filter() if len(e) == 0 { @@ -78,8 +78,8 @@ func (e Env) Render() { } } -func (e Env) filter() Env { - var env Env +func (e Envs) filter() Envs { + var env Envs for _, variable := range e { if variable.If.Ignore() { diff --git a/src/shell/env_test.go b/src/shell/env_test.go index bbd3647..7481b03 100644 --- a/src/shell/env_test.go +++ b/src/shell/env_test.go @@ -8,7 +8,7 @@ import ( ) func TestEnvironmentVariable(t *testing.T) { - env := &Variable{Name: "HELLO", Value: "world"} + env := &Env{Name: "HELLO", Value: "world"} cases := []struct { Case string Shell string @@ -91,17 +91,17 @@ func TestEnvironmentVariableWithTemplate(t *testing.T) { } for _, tc := range cases { - env := &Variable{Name: "HELLO", Value: tc.Value} + env := &Env{Name: "HELLO", Value: tc.Value} context.Current = &context.Runtime{Shell: BASH, Home: "/Users/jan"} assert.Equal(t, tc.Expected, env.string(), tc.Case) } } func TestEnvFilter(t *testing.T) { - env := Env{ - &Variable{Name: "FOO", Value: "bar"}, - &Variable{Name: "BAR", Value: "foo"}, - &Variable{Name: "BAZ", Value: "baz", If: `eq .Shell "zsh"`}, + env := Envs{ + &Env{Name: "FOO", Value: "bar"}, + &Env{Name: "BAR", Value: "foo"}, + &Env{Name: "BAZ", Value: "baz", If: `eq .Shell "zsh"`}, } context.Current = &context.Runtime{Shell: "FISH"} filtered := env.filter() @@ -112,30 +112,30 @@ func TestEnvRender(t *testing.T) { cases := []struct { Case string Shell string - Env Env + Env Envs NonEmptyScript bool Expected string }{ { Case: "PWSH - No elements", - Env: Env{&Variable{Name: "HELLO", Value: "world", If: `eq .Shell "fish"`}}, + Env: Envs{&Env{Name: "HELLO", Value: "world", If: `eq .Shell "fish"`}}, Shell: PWSH, }, { Case: "PWSH - If true", - Env: Env{&Variable{Name: "HELLO", Value: "world", If: `eq .Shell "pwsh"`}}, + Env: Envs{&Env{Name: "HELLO", Value: "world", If: `eq .Shell "pwsh"`}}, Shell: PWSH, Expected: `$env:HELLO = "world"`, }, { Case: "PWSH - Single variable", - Env: Env{&Variable{Name: "HELLO", Value: "world"}}, + Env: Envs{&Env{Name: "HELLO", Value: "world"}}, Shell: PWSH, Expected: `$env:HELLO = "world"`, }, { Case: "PWSH - Single variable, non empty", - Env: Env{&Variable{Name: "HELLO", Value: "world"}}, + Env: Envs{&Env{Name: "HELLO", Value: "world"}}, Shell: PWSH, NonEmptyScript: true, Expected: `foo @@ -144,9 +144,9 @@ $env:HELLO = "world"`, }, { Case: "PWSH - double variable", - Env: Env{ - &Variable{Name: "HELLO", Value: "world"}, - &Variable{Name: "FOO", Value: "bar"}, + Env: Envs{ + &Env{Name: "HELLO", Value: "world"}, + &Env{Name: "FOO", Value: "bar"}, }, Shell: PWSH, Expected: `$env:HELLO = "world" @@ -154,7 +154,7 @@ $env:FOO = "bar"`, }, { Case: "NU - Single variable", - Env: Env{&Variable{Name: "HELLO", Value: "world"}}, + Env: Envs{&Env{Name: "HELLO", Value: "world"}}, Shell: NU, Expected: `export-env { $env.HELLO = "world" diff --git a/src/shell/fish.go b/src/shell/fish.go index 4536f1c..dfffbea 100644 --- a/src/shell/fish.go +++ b/src/shell/fish.go @@ -7,9 +7,9 @@ const ( func (a *Alias) fish() *Alias { switch a.Type { case Command: - a.template = `alias {{ .Alias }} '{{ .Value }}'` + a.template = `alias {{ .Name }} '{{ .Value }}'` case Function: - a.template = `function {{ .Alias }} + a.template = `function {{ .Name }} {{ .Value }} end` } @@ -17,12 +17,12 @@ end` return a } -func (e *Variable) fish() *Variable { +func (e *Env) fish() *Env { e.template = `set --global {{ .Name }} {{ .Value }}` return e } -func (e *PathEntry) fish() *PathEntry { +func (e *Path) fish() *Path { e.template = `fish_add_path {{ .Value }}` return e } diff --git a/src/shell/nu.go b/src/shell/nu.go index 08040a0..73b14d3 100644 --- a/src/shell/nu.go +++ b/src/shell/nu.go @@ -16,9 +16,9 @@ const ( func (a *Alias) nu() *Alias { switch a.Type { case Command: - a.template = `alias {{ .Alias }} = {{ .Value }}` + a.template = `alias {{ .Name }} = {{ .Value }}` case Function: - a.template = `def {{ .Alias }} [] { + a.template = `def {{ .Name }} [] { {{ .Value }} }` } @@ -31,14 +31,14 @@ func (e *Echo) nu() *Echo { return e } -func (e *Variable) nu() *Variable { +func (e *Env) nu() *Env { e.template = ` $env.{{ .Name }} = {{ formatString .Value }}` return e } -func (e *PathEntry) nu() *PathEntry { - e.template = `let-env PATH = ($env.PATH | prepend "{{ .Value }}")` - return e +func (p *Path) nu() *Path { + p.template = `let-env PATH = ($env.PATH | prepend "{{ .Value }}")` + return p } func NuInit(script string) error { diff --git a/src/shell/path.go b/src/shell/path.go index 6efeaf0..e67c1c9 100644 --- a/src/shell/path.go +++ b/src/shell/path.go @@ -6,45 +6,45 @@ import ( "github.com/jandedobbeleer/aliae/src/context" ) -type Path []*PathEntry +type Paths []*Path -type PathEntry struct { +type Path struct { Value Template `yaml:"value"` If If `yaml:"if"` template string } -func (e *PathEntry) string() string { +func (p *Path) string() string { switch context.Current.Shell { case ZSH, BASH: - return e.zsh().render() + return p.zsh().render() case PWSH: - return e.pwsh().render() + return p.pwsh().render() case NU: - return e.nu().render() + return p.nu().render() case FISH: - return e.fish().render() + return p.fish().render() case TCSH: - return e.tcsh().render() + return p.tcsh().render() case XONSH: - return e.xonsh().render() + return p.xonsh().render() case CMD: - return e.cmd().render() + return p.cmd().render() default: return "" } } -func (e *PathEntry) render() string { - e.Value = e.Value.Parse() +func (p *Path) render() string { + p.Value = p.Value.Parse() var builder strings.Builder ctx := struct { Value string }{} - splitted := strings.Split(string(e.Value), "\n") + splitted := strings.Split(string(p.Value), "\n") first := true for _, line := range splitted { @@ -57,7 +57,7 @@ func (e *PathEntry) render() string { } ctx.Value = line - script, err := parse(e.template, ctx) + script, err := parse(p.template, ctx) if err != nil { builder.WriteString(err.Error()) } @@ -70,7 +70,7 @@ func (e *PathEntry) render() string { return builder.String() } -func (p Path) Render() { +func (p Paths) Render() { if len(p) == 0 { return } diff --git a/src/shell/path_test.go b/src/shell/path_test.go index 6eb99c4..b02232b 100644 --- a/src/shell/path_test.go +++ b/src/shell/path_test.go @@ -11,115 +11,115 @@ func TestPath(t *testing.T) { cases := []struct { Case string Shell string - Path *PathEntry + Path *Path Expected string }{ { Case: "Unknown shell", Shell: "FOO", - Path: &PathEntry{Value: "/usr/local/bin"}, + Path: &Path{Value: "/usr/local/bin"}, }, { Case: "PWSH - single item", Shell: PWSH, - Path: &PathEntry{Value: "/usr/local/bin"}, + Path: &Path{Value: "/usr/local/bin"}, Expected: `$env:Path = '/usr/local/bin;' + $env:Path`, }, { Case: "PWSH - single item with template", Shell: PWSH, - Path: &PathEntry{Value: "{{ .Home }}/.tools/bin"}, + Path: &Path{Value: "{{ .Home }}/.tools/bin"}, Expected: `$env:Path = '/Users/jan/.tools/bin;' + $env:Path`, }, { Case: "PWSH - single item with blank line", Shell: PWSH, - Path: &PathEntry{Value: "/usr/local/bin\n\n/usr/bin"}, + Path: &Path{Value: "/usr/local/bin\n\n/usr/bin"}, Expected: `$env:Path = '/usr/local/bin;' + $env:Path $env:Path = '/usr/bin;' + $env:Path`, }, { Case: "PWSH - multiple items", Shell: PWSH, - Path: &PathEntry{Value: "/usr/local/bin\n/usr/bin"}, + Path: &Path{Value: "/usr/local/bin\n/usr/bin"}, Expected: `$env:Path = '/usr/local/bin;' + $env:Path $env:Path = '/usr/bin;' + $env:Path`, }, { Case: "CMD - single item", Shell: CMD, - Path: &PathEntry{Value: "/usr/local/bin"}, + Path: &Path{Value: "/usr/local/bin"}, Expected: `os.setenv("PATH", "/usr/local/bin;" .. os.getenv("PATH"))`, }, { Case: "CMD - multiple items", Shell: CMD, - Path: &PathEntry{Value: "/usr/local/bin\n/usr/bin"}, + Path: &Path{Value: "/usr/local/bin\n/usr/bin"}, Expected: `os.setenv("PATH", "/usr/local/bin;" .. os.getenv("PATH")) os.setenv("PATH", "/usr/bin;" .. os.getenv("PATH"))`, }, { Case: "FISH - single item", Shell: FISH, - Path: &PathEntry{Value: "/usr/local/bin"}, + Path: &Path{Value: "/usr/local/bin"}, Expected: `fish_add_path /usr/local/bin`, }, { Case: "FISH - multiple items", Shell: FISH, - Path: &PathEntry{Value: "/usr/local/bin\n/usr/bin"}, + Path: &Path{Value: "/usr/local/bin\n/usr/bin"}, Expected: `fish_add_path /usr/local/bin fish_add_path /usr/bin`, }, { Case: "NU - single item", Shell: NU, - Path: &PathEntry{Value: "/usr/local/bin"}, + Path: &Path{Value: "/usr/local/bin"}, Expected: `let-env PATH = ($env.PATH | prepend "/usr/local/bin")`, }, { Case: "NU - multiple items", Shell: NU, - Path: &PathEntry{Value: "/usr/local/bin\n/usr/bin"}, + Path: &Path{Value: "/usr/local/bin\n/usr/bin"}, Expected: `let-env PATH = ($env.PATH | prepend "/usr/local/bin") let-env PATH = ($env.PATH | prepend "/usr/bin")`, }, { Case: "TCSH - single item", Shell: TCSH, - Path: &PathEntry{Value: "/usr/local/bin"}, + Path: &Path{Value: "/usr/local/bin"}, Expected: `set path = ( /usr/local/bin $path );`, }, { Case: "TCSH - multiple items", Shell: TCSH, - Path: &PathEntry{Value: "/usr/local/bin\n/usr/bin"}, + Path: &Path{Value: "/usr/local/bin\n/usr/bin"}, Expected: `set path = ( /usr/local/bin $path ); set path = ( /usr/bin $path );`, }, { Case: "XONSH - single item", Shell: XONSH, - Path: &PathEntry{Value: "/usr/local/bin"}, + Path: &Path{Value: "/usr/local/bin"}, Expected: `$PATH.add('/usr/local/bin', True, False)`, }, { Case: "XONSH - multiple items", Shell: XONSH, - Path: &PathEntry{Value: "/usr/local/bin\n/usr/bin"}, + Path: &Path{Value: "/usr/local/bin\n/usr/bin"}, Expected: `$PATH.add('/usr/local/bin', True, False) $PATH.add('/usr/bin', True, False)`, }, { Case: "ZSH - single item", Shell: ZSH, - Path: &PathEntry{Value: "/usr/local/bin"}, + Path: &Path{Value: "/usr/local/bin"}, Expected: `export PATH="/usr/local/bin:$PATH"`, }, { Case: "ZSH - multiple items", Shell: ZSH, - Path: &PathEntry{Value: "/usr/local/bin\n/usr/bin"}, + Path: &Path{Value: "/usr/local/bin\n/usr/bin"}, Expected: `export PATH="/usr/local/bin:$PATH" export PATH="/usr/bin:$PATH"`, }, @@ -135,42 +135,42 @@ func TestPathRender(t *testing.T) { cases := []struct { Case string Shell string - Paths Path + Paths Paths NonEmptyScript bool Expected string }{ { Case: "PWSH - No PATHS", - Paths: Path{}, + Paths: Paths{}, Shell: PWSH, }, { Case: "PWSH - If false", - Paths: Path{ - &PathEntry{Value: "/usr/bin", If: `eq .Shell "fish"`}, + Paths: Paths{ + &Path{Value: "/usr/bin", If: `eq .Shell "fish"`}, }, Shell: PWSH, }, { Case: "PWSH - If true", - Paths: Path{ - &PathEntry{Value: "/usr/bin", If: `eq .Shell "pwsh"`}, + Paths: Paths{ + &Path{Value: "/usr/bin", If: `eq .Shell "pwsh"`}, }, Shell: PWSH, Expected: `$env:Path = '/usr/bin;' + $env:Path`, }, { Case: "PWSH - 1 PATH definition", - Paths: Path{ - &PathEntry{Value: "/usr/bin"}, + Paths: Paths{ + &Path{Value: "/usr/bin"}, }, Shell: PWSH, Expected: `$env:Path = '/usr/bin;' + $env:Path`, }, { Case: "PWSH - Single PATH, non empty", - Paths: Path{ - &PathEntry{Value: "/usr/bin"}, + Paths: Paths{ + &Path{Value: "/usr/bin"}, }, Shell: PWSH, NonEmptyScript: true, @@ -180,9 +180,9 @@ $env:Path = '/usr/bin;' + $env:Path`, }, { Case: "PWSH - 2 PATH definitions", - Paths: Path{ - &PathEntry{Value: "/usr/bin"}, - &PathEntry{Value: "/Users/jan/.tools/bin"}, + Paths: Paths{ + &Path{Value: "/usr/bin"}, + &Path{Value: "/Users/jan/.tools/bin"}, }, Shell: PWSH, Expected: `$env:Path = '/usr/bin;' + $env:Path @@ -190,9 +190,9 @@ $env:Path = '/Users/jan/.tools/bin;' + $env:Path`, }, { Case: "PWSH - 2 PATH definitions with conditional", - Paths: Path{ - &PathEntry{Value: "/usr/bin", If: `eq .Shell "fish"`}, - &PathEntry{Value: "/Users/jan/.tools/bin"}, + Paths: Paths{ + &Path{Value: "/usr/bin", If: `eq .Shell "fish"`}, + &Path{Value: "/Users/jan/.tools/bin"}, }, Shell: PWSH, Expected: `$env:Path = '/Users/jan/.tools/bin;' + $env:Path`, diff --git a/src/shell/pwsh.go b/src/shell/pwsh.go index 05fcb8a..fd146c9 100644 --- a/src/shell/pwsh.go +++ b/src/shell/pwsh.go @@ -28,9 +28,9 @@ func (a *Alias) pwsh() *Alias { switch a.Type { case Command: - a.template = `Set-Alias -Name {{ .Alias }} -Value {{ .Value }}{{ if .Description }} -Description '{{ .Description }}'{{ end }}{{ if .Force }} -Force{{ end }}{{ if isPwshOption .Option }} -Option {{ .Option }}{{ end }}{{ if isPwshScope .Scope }} -Scope {{ .Scope }}{{ end }}` //nolint: lll + a.template = `Set-Alias -Name {{ .Name }} -Value {{ .Value }}{{ if .Description }} -Description '{{ .Description }}'{{ end }}{{ if .Force }} -Force{{ end }}{{ if isPwshOption .Option }} -Option {{ .Option }}{{ end }}{{ if isPwshScope .Scope }} -Scope {{ .Scope }}{{ end }}` //nolint: lll case Function: - a.template = `function {{ .Alias }}() { + a.template = `function {{ .Name }}() { {{ .Value }} }` } @@ -46,14 +46,14 @@ Write-Host $message` return e } -func (e *Variable) pwsh() *Variable { +func (e *Env) pwsh() *Env { e.template = `$env:{{ .Name }} = {{ formatString .Value }}` return e } -func (e *PathEntry) pwsh() *PathEntry { - e.template = `$env:Path = '{{ .Value }};' + $env:Path` - return e +func (p *Path) pwsh() *Path { + p.template = `$env:Path = '{{ .Value }};' + $env:Path` + return p } func isPwshOption(option Option) bool { diff --git a/src/shell/pwsh_test.go b/src/shell/pwsh_test.go index a33a18e..e65accd 100644 --- a/src/shell/pwsh_test.go +++ b/src/shell/pwsh_test.go @@ -17,14 +17,14 @@ func TestPowerShellCommandAlias(t *testing.T) { Case: "PWSH", Shell: PWSH, Expected: "Set-Alias -Name foo -Value bar", - Alias: &Alias{Alias: "foo", Value: "bar"}, + Alias: &Alias{Name: "foo", Value: "bar"}, }, { Case: "PWSH - Description", Shell: PWSH, Expected: "Set-Alias -Name foo -Value bar -Description 'This is a description'", Alias: &Alias{ - Alias: "foo", + Name: "foo", Value: "bar", Description: "This is a description", }, @@ -34,7 +34,7 @@ func TestPowerShellCommandAlias(t *testing.T) { Shell: PWSH, Expected: "Set-Alias -Name foo -Value bar -Force", Alias: &Alias{ - Alias: "foo", + Name: "foo", Value: "bar", Force: true, }, @@ -44,7 +44,7 @@ func TestPowerShellCommandAlias(t *testing.T) { Shell: PWSH, Expected: "Set-Alias -Name foo -Value bar -Option AllScope", Alias: &Alias{ - Alias: "foo", + Name: "foo", Value: "bar", Option: AllScope, }, @@ -54,7 +54,7 @@ func TestPowerShellCommandAlias(t *testing.T) { Shell: PWSH, Expected: "Set-Alias -Name foo -Value bar -Scope Global", Alias: &Alias{ - Alias: "foo", + Name: "foo", Value: "bar", Scope: Global, }, @@ -64,7 +64,7 @@ func TestPowerShellCommandAlias(t *testing.T) { Shell: PWSH, Expected: "Set-Alias -Name foo -Value bar -Description 'This is a description' -Force", Alias: &Alias{ - Alias: "foo", + Name: "foo", Value: "bar", Description: "This is a description", Force: true, @@ -75,7 +75,7 @@ func TestPowerShellCommandAlias(t *testing.T) { Shell: PWSH, Expected: "Set-Alias -Name foo -Value bar -Description 'This is a description' -Force -Scope Global", Alias: &Alias{ - Alias: "foo", + Name: "foo", Value: "bar", Description: "This is a description", Force: true, @@ -87,7 +87,7 @@ func TestPowerShellCommandAlias(t *testing.T) { Shell: PWSH, Expected: "Set-Alias -Name foo -Value bar -Description 'This is a description' -Force -Option AllScope -Scope Global", Alias: &Alias{ - Alias: "foo", + Name: "foo", Value: "bar", Description: "This is a description", Force: true, diff --git a/src/shell/tcsh.go b/src/shell/tcsh.go index ac3d5ff..5833528 100644 --- a/src/shell/tcsh.go +++ b/src/shell/tcsh.go @@ -6,18 +6,18 @@ const ( func (a *Alias) tcsh() *Alias { if a.Type == Command { - a.template = `alias {{ .Alias }} '{{ .Value }}';` + a.template = `alias {{ .Name }} '{{ .Value }}';` } return a } -func (e *Variable) tcsh() *Variable { +func (e *Env) tcsh() *Env { e.template = `setenv {{ .Name }} {{ .Value }};` return e } -func (p *PathEntry) tcsh() *PathEntry { +func (p *Path) tcsh() *Path { p.template = `set path = ( {{ .Value }} $path );` return p } diff --git a/src/shell/xonsh.go b/src/shell/xonsh.go index 6886324..091f326 100644 --- a/src/shell/xonsh.go +++ b/src/shell/xonsh.go @@ -12,11 +12,11 @@ const ( func (a *Alias) xonsh() *Alias { switch a.Type { case Command: - a.template = `aliases['{{ .Alias }}'] = '{{ .Value }}'` + a.template = `aliases['{{ .Name }}'] = '{{ .Value }}'` case Function: // some xonsh aliases are not valid python function names - funcName := strings.ReplaceAll(a.Alias, `-`, ``) - template := fmt.Sprintf(`@aliases.register("{{ .Alias }}") + funcName := strings.ReplaceAll(a.Name, `-`, ``) + template := fmt.Sprintf(`@aliases.register("{{ .Name }}") def __%s(): {{ .Value }}`, funcName) a.template = template @@ -31,12 +31,12 @@ print(message)` return e } -func (e *Variable) xonsh() *Variable { +func (e *Env) xonsh() *Env { e.template = `${{ .Name }} = {{ formatString .Value }}` return e } -func (p *PathEntry) xonsh() *PathEntry { +func (p *Path) xonsh() *Path { p.template = `$PATH.add('{{ .Value }}', True, False)` return p } diff --git a/src/shell/zsh.go b/src/shell/zsh.go index 91599c8..51cba51 100644 --- a/src/shell/zsh.go +++ b/src/shell/zsh.go @@ -8,9 +8,9 @@ const ( func (a *Alias) zsh() *Alias { switch a.Type { case Command: - a.template = `alias {{ .Alias }}="{{ .Value }}"` + a.template = `alias {{ .Name }}="{{ .Value }}"` case Function: - a.template = `{{ .Alias }}() { + a.template = `{{ .Name }}() { {{ .Value }} }` } @@ -23,12 +23,12 @@ func (e *Echo) zsh() *Echo { return e } -func (e *Variable) zsh() *Variable { +func (e *Env) zsh() *Env { e.template = `export {{ .Name }}={{ formatString .Value }}` return e } -func (e *PathEntry) zsh() *PathEntry { - e.template = `export PATH="{{ .Value }}:$PATH"` - return e +func (p *Path) zsh() *Path { + p.template = `export PATH="{{ .Value }}:$PATH"` + return p } diff --git a/website/docs/setup/alias.mdx b/website/docs/setup/alias.mdx index c0d4356..75b399a 100644 --- a/website/docs/setup/alias.mdx +++ b/website/docs/setup/alias.mdx @@ -9,10 +9,10 @@ Specify the same alias cross shell. ### Syntax ```yaml -aliae: - - alias: a +alias: + - name: a value: aliae - - alias: hello-world + - name: hello-world value: echo "hello world" type: function ``` @@ -21,7 +21,7 @@ aliae: | Name | Type | Description | | ------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | -| `alias` | `string` | the alias name | +| `name` | `string` | the alias name | | `value` | `string` | the command(s) you want to execute, supports [templating][templates] | | `type` | `string` | | | `if` | `string` | golang [template][go-text-template] conditional statement, see [if][if] |