This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
/
alias_unix.go
184 lines (136 loc) · 3.76 KB
/
alias_unix.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package utils
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
)
//AddAlias will add the alias for the package name specified
func (u *Utility) AddAliasUnix(name string, ed string) error {
//get the home directory file path
home, err := os.UserHomeDir()
if err != nil {
return err
}
//Get the shell PID
ppid := fmt.Sprint(os.Getppid())
//Create a list of commands to run and pipe together
var cmds []exec.Cmd
cmds = append(cmds, *exec.Command("ps", "-ef"))
cmds = append(cmds, *exec.Command("awk", "{print $2 \" \" $8}"))
cmds = append(cmds, *exec.Command("grep", ppid))
cmds = append(cmds, *exec.Command("awk", "{print $2}"))
var output []byte
//Loop through the commands and run them
for i, _ := range cmds {
cmds[i].Stdin = bytes.NewReader(output)
output, err = cmds[i].Output()
if err != nil {
return err
}
}
var path string
//Trim the output to get rid of any whitespace
shell := strings.TrimSpace(string(output[:]))
//Get the filepath for the correct shell rc file
if shell == "bash" || shell == "-bash" {
path = home + "/.bashrc"
} else if shell == "zsh" || shell == "-zsh" {
path = home + "/.zshrc"
} else {
return errors.New("Shell: " + shell + " is currently unsupported.")
}
//If run on linux lets modify the shell rc file to include the new aliases
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
return err
}
//Create the alias and write it to the file
alias := "alias " + name + "=" + "\"" + ed + "/packageless run " + name + "\"" + "\n"
_, err = file.WriteString(alias)
if err != nil {
return err
}
file.Close()
return nil
}
//Remove Alias will remove the alias for the specified package name from the corresponding files
func (u *Utility) RemoveAliasUnix(name string, ed string) error {
//get the home directory file path
home, err := os.UserHomeDir()
if err != nil {
return err
}
//Get the shell PID
ppid := fmt.Sprint(os.Getppid())
//Create a list of commands to run and pipe them together
var cmds []exec.Cmd
cmds = append(cmds, *exec.Command("ps", "-ef"))
cmds = append(cmds, *exec.Command("awk", "{print $2 \" \" $8}"))
cmds = append(cmds, *exec.Command("grep", ppid))
cmds = append(cmds, *exec.Command("awk", "{print $2}"))
var output []byte
//Loop through the commands and run them
for i, _ := range cmds {
cmds[i].Stdin = bytes.NewReader(output)
output, err = cmds[i].Output()
if err != nil {
return err
}
}
var path string
//Trim the output whitespace
shell := strings.TrimSpace(string(output[:]))
//Get the filepath for the correct shell rc file
if shell == "bash" || shell == "-bash" {
path = home + "/.bashrc"
} else if shell == "zsh" || shell == "-zsh" {
path = home + "/.zshrc"
} else {
return errors.New("Shell: " + shell + " is currently unsupported.")
}
//Open the shell rc file
file, err := os.OpenFile(path, os.O_RDWR, 0755)
var newOut []string
if err != nil {
return err
}
reader := bufio.NewReader(file)
//Read the file line by line
for {
line, err := reader.ReadString('\n')
//Check for EOF
if err != nil && err == io.EOF {
break
}
if err != nil {
return err
}
alias := "alias " + name + "=" + "\"" + ed + "/packageless run " + name + "\"" + "\n"
//if the line is the alias for this package dont include it in the new file
if line != alias {
newOut = append(newOut, line)
}
}
//Close the file
file.Close()
//Recreate the shell rc file
newFile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
//Write the contents back to the shell rc file
for _, line := range newOut {
_, err = newFile.WriteString(line)
if err != nil {
return err
}
}
//Close the file
newFile.Close()
return nil
}