-
Notifications
You must be signed in to change notification settings - Fork 1
/
alias.go
106 lines (94 loc) · 2.1 KB
/
alias.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"github.com/urfave/cli/v2"
)
const BASH_ALIAS = "\nalias nw='narwhal'"
const ZSH_ALIAS = "\nalias nw='narwhal'"
const POWERSHELL_ALIAS = "\nSet-Alias -Name nw -Value narwhal"
func removeAlias(c *cli.Context) error {
shell := c.Args().Get(0)
if shell == "bash" {
f, err := homeFile(".bashrc")
if err != nil {
return ee(err)
}
return removeFromFile(f, BASH_ALIAS)
} else if shell == "zsh" {
f, err := homeFile(".zshrc")
if err != nil {
return ee(err)
}
return removeFromFile(f, ZSH_ALIAS)
} else if shell == "powershell" {
profile := c.Args().Get(1)
return removeFromFile(profile, POWERSHELL_ALIAS)
} else {
return e1("unknown command")
}
}
func addAlias(c *cli.Context) error {
shell := c.Args().Get(0)
err := removeAlias(c)
if err != nil {
return err
}
if shell == "bash" {
err := appendTo(".bashrc", BASH_ALIAS, true)
if err != nil {
return err
}
fmt.Println("Please either restart your shell or run:")
fmt.Println("\tsource ~/.bashrc")
return nil
} else if shell == "zsh" {
err := appendTo(".zshrc", ZSH_ALIAS, true)
if err != nil {
return err
}
fmt.Println("Please either restart your shell or run:")
fmt.Println("\tsource ~/.zshrc")
return nil
} else if shell == "powershell" {
profile := c.Args().Get(1)
err := appendTo(profile, POWERSHELL_ALIAS, false)
if err != nil {
return err
}
fmt.Println("Please either restart your shell or run:")
fmt.Println("\t& $profile")
return nil
} else {
return e1("unknown command")
}
}
func addAliasComplete(c *cli.Context) {
if c.NArg() == 0 {
fmt.Println("bash")
fmt.Println("zsh")
fmt.Println("powershell")
} else if c.NArg() == 1 {
if c.Args().Get(0) == "powershell" {
fmt.Println("$profile")
} else {
fmt.Println("")
}
} else {
fmt.Println("")
}
}
func removeAliasComplete(c *cli.Context) {
if c.NArg() == 0 {
fmt.Println("bash")
fmt.Println("zsh")
fmt.Println("powershell")
} else if c.NArg() == 1 {
if c.Args().Get(0) == "powershell" {
fmt.Println("$profile")
} else {
fmt.Println("")
}
} else {
fmt.Println("")
}
}