Skip to content

Commit

Permalink
refactor: align model definition
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jul 28, 2023
1 parent 8f26514 commit 8c2f323
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 134 deletions.
6 changes: 3 additions & 3 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/shell/aliae.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
24 changes: 12 additions & 12 deletions src/shell/aliae_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestAliasFunction(t *testing.T) {
cases := []struct {
Case string
Shell string
Alias string
Name string
Expected string
}{
{
Expand Down Expand Up @@ -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():
Expand All @@ -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}
Expand All @@ -159,29 +159,29 @@ 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"`,
},
{
Case: "Filtered out",
Aliae: Aliae{
&Alias{Alias: "FOO", Value: "bar", If: `eq .Shell "fish"`},
&Alias{Name: "FOO", Value: "bar", If: `eq .Shell "fish"`},
},
},
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/shell/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
}

Expand All @@ -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
}
14 changes: 7 additions & 7 deletions src/shell/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ 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"`

template string
}

func (e *Variable) string() string {
func (e *Env) string() string {
switch context.Current.Shell {
case ZSH, BASH:
return e.zsh().render()
Expand All @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
30 changes: 15 additions & 15 deletions src/shell/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -144,17 +144,17 @@ $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"
$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"
Expand Down
8 changes: 4 additions & 4 deletions src/shell/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ 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`
}

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
}
12 changes: 6 additions & 6 deletions src/shell/nu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}`
}
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 8c2f323

Please sign in to comment.