-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c6930e
commit 44a66b9
Showing
14 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package shell | ||
|
||
import ( | ||
"github.com/jandedobbeleer/aliae/src/context" | ||
) | ||
|
||
type Links []*Link | ||
|
||
type Link struct { | ||
Name Template `yaml:"name"` | ||
Target Template `yaml:"target"` | ||
If If `yaml:"if"` | ||
|
||
template string | ||
} | ||
|
||
func (l *Link) string() string { | ||
switch context.Current.Shell { | ||
case ZSH, BASH: | ||
return l.zsh().render() | ||
case PWSH, POWERSHELL: | ||
return l.pwsh().render() | ||
case NU: | ||
return l.nu().render() | ||
case FISH: | ||
return l.fish().render() | ||
case TCSH: | ||
return l.tcsh().render() | ||
case XONSH: | ||
return l.xonsh().render() | ||
case CMD: | ||
return l.cmd().render() | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func (l *Link) render() string { | ||
script, err := parse(l.template, l) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
|
||
return script | ||
} | ||
|
||
func (l Links) Render() { | ||
if len(l) == 0 { | ||
return | ||
} | ||
|
||
first := true | ||
for _, link := range l { | ||
script := link.string() | ||
if len(script) == 0 || link.If.Ignore() { | ||
continue | ||
} | ||
|
||
if first && DotFile.Len() > 0 { | ||
DotFile.WriteString("\n\n") | ||
} | ||
|
||
if !first { | ||
DotFile.WriteString("\n") | ||
} | ||
|
||
DotFile.WriteString(script) | ||
|
||
first = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package shell | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jandedobbeleer/aliae/src/context" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLinkCommand(t *testing.T) { | ||
link := &Link{Name: "foo", Target: "bar"} | ||
cases := []struct { | ||
Case string | ||
Shell string | ||
Expected string | ||
}{ | ||
{ | ||
Case: "PWSH", | ||
Shell: PWSH, | ||
Expected: "New-Item -Path \"foo\" -ItemType SymbolicLink -Value \"bar\" -Force", | ||
}, | ||
{ | ||
Case: "CMD", | ||
Shell: CMD, | ||
Expected: `mklink foo bar`, | ||
}, | ||
{ | ||
Case: "FISH", | ||
Shell: FISH, | ||
Expected: "ln -sf bar foo", | ||
}, | ||
{ | ||
Case: "NU", | ||
Shell: NU, | ||
Expected: "ln -sf bar foo", | ||
}, | ||
{ | ||
Case: "TCSH", | ||
Shell: TCSH, | ||
Expected: "ln -sf bar foo;", | ||
}, | ||
{ | ||
Case: "XONSH", | ||
Shell: XONSH, | ||
Expected: "ln -sf bar foo", | ||
}, | ||
{ | ||
Case: "ZSH", | ||
Shell: ZSH, | ||
Expected: `ln -sf bar foo`, | ||
}, | ||
{ | ||
Case: "BASH", | ||
Shell: BASH, | ||
Expected: `ln -sf bar foo`, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
link.template = "" | ||
context.Current = &context.Runtime{Shell: tc.Shell} | ||
assert.Equal(t, tc.Expected, link.string(), tc.Case) | ||
} | ||
} | ||
|
||
func TestLinkRender(t *testing.T) { | ||
cases := []struct { | ||
Case string | ||
Links Links | ||
Expected string | ||
}{ | ||
{ | ||
Case: "Single link", | ||
Links: Links{ | ||
&Link{Name: "FOO", Target: "bar"}, | ||
}, | ||
Expected: `ln -sf bar FOO`, | ||
}, | ||
{ | ||
Case: "Double link", | ||
Links: Links{ | ||
&Link{Name: "FOO", Target: "bar"}, | ||
&Link{Name: "BAR", Target: "foo"}, | ||
}, | ||
Expected: `ln -sf bar FOO | ||
ln -sf foo BAR`, | ||
}, | ||
{ | ||
Case: "Filtered out", | ||
Links: Links{ | ||
&Link{Name: "FOO", Target: "bar", If: `eq .Shell "fish"`}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
DotFile.Reset() | ||
context.Current = &context.Runtime{Shell: BASH} | ||
tc.Links.Render() | ||
assert.Equal(t, tc.Expected, DotFile.String(), tc.Case) | ||
} | ||
} | ||
|
||
func TestLinkWithTemplate(t *testing.T) { | ||
cases := []struct { | ||
Case string | ||
Target Template | ||
Expected string | ||
}{ | ||
{ | ||
Case: "No template", | ||
Target: "~/dotfiles/zshrc", | ||
Expected: `ln -sf ~/dotfiles/zshrc /tmp/l`, | ||
}, | ||
{ | ||
Case: "Home in template", | ||
Target: "{{ .Home }}/.aliae.yaml", | ||
Expected: `ln -sf /Users/jan/.aliae.yaml /tmp/l`, | ||
}, | ||
{ | ||
Case: "Advanced template", | ||
Target: "{{ .Home }}/go/bin/aliae{{ if eq .OS \"windows\" }}.exe{{ end }}", | ||
Expected: `ln -sf /Users/jan/go/bin/aliae.exe /tmp/l`, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
link := &Link{Name: "/tmp/l", Target: tc.Target} | ||
context.Current = &context.Runtime{Shell: BASH, Home: "/Users/jan", OS: context.WINDOWS} | ||
assert.Equal(t, tc.Expected, link.string(), tc.Case) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
id: link | ||
title: Symbolic Link | ||
sidebar_label: 🔗 Symbolic Link | ||
--- | ||
|
||
Create symlinks to files and directories. Useful for dotfiles. | ||
|
||
### Syntax | ||
|
||
```yaml | ||
link: | ||
- name: ~/.aliae.yaml | ||
target: ~/dotfiles/aliae.yaml | ||
- name: ~/.zshrc | ||
target: $DOTFILES/config/zsh/zshrc | ||
- name: ~/Brewfile | ||
value: /some/location/Brewfile | ||
if: eq .OS "darwin" | ||
``` | ||
### Link | ||
| Name | Type | Description | | ||
| -------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `name` | `string` | the link name, supports [templating][templates] | | ||
| `target` | `string` | the name of the file or directory to link to, supports [templating][templates] | | ||
| `if` | `string` | golang [template][go-text-template] conditional statement, see [if][if] | | ||
|
||
[go-text-template]: https://golang.org/pkg/text/template/ | ||
[if]: if.mdx | ||
[templates]: templates.mdx |