-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.go
More file actions
166 lines (128 loc) · 4.22 KB
/
Copy pathexecutor.go
File metadata and controls
166 lines (128 loc) · 4.22 KB
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
package main
import (
"fmt"
"log"
"os/exec"
)
func execute(pathToApp, command string, args []string) error {
switch command {
case "npm_i":
return tryCommand(pathToApp, func() ([]byte, error) {
return npmInstall(pathToApp, args[0])
})
case "yarn_add":
return tryCommand(pathToApp, func() ([]byte, error) {
return yarnInstall(pathToApp, args[0])
})
case "npm_b":
return tryCommand(pathToApp, func() ([]byte, error) {
return npmBuild(pathToApp)
})
case "git_pull":
return tryCommand(pathToApp, func() ([]byte, error) {
return gitPull(pathToApp)
})
case "git_push":
return tryCommand(pathToApp, func() ([]byte, error) {
return gitPush(pathToApp)
})
case "git_commit":
return tryCommand(pathToApp, func() ([]byte, error) {
return gitCommit(pathToApp, args[0])
})
case "git_stash_push":
return tryCommand(pathToApp, func() ([]byte, error) {
return gitStashPush(pathToApp, args[0])
})
}
return fmt.Errorf("unknown commandName %s", command)
}
func tryCommand(pathToApp string, commandCall func() ([]byte, error)) error {
out, err := commandCall()
if err != nil {
logRed("[tryCommand_err] err: " + err.Error())
logRed("[tryCommand_out] out: " + string(out))
logBlue("Check invalid permission.")
removePermissionTrackGlobally()
givePermission(pathToApp)
out, err = commandCall()
if err == nil {
logGreen("Execution succeeded.")
log.Println("Output: ", string(out))
return nil
}
logRed("Execution failed.")
log.Println("Output: ", string(out))
log.Fatal(err)
return err
}
logGreen("Execution succeeded.")
log.Println("Output: ", string(out))
return nil
}
func npmBuild(pathToApp string) ([]byte, error) {
npm := executables["npm"]
cmd := exec.Command(npm, "run", "build", "--prefix", pathToApp)
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}
func npmInstall(pathToApp, packageName string) ([]byte, error) {
npm := executables["npm"]
if packageName == "" {
cmd := exec.Command("sudo", npm, "install", "--prefix", pathToApp, "--legacy-peer-deps")
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}
cmd := exec.Command("sudo", npm, "install", packageName, "--prefix", pathToApp, "--legacy-peer-deps")
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}
func yarnInstall(pathToApp, packageName string) ([]byte, error) {
yarn := executables["yarn"]
if packageName == "" {
cmd := exec.Command("sudo", yarn, "--cwd", pathToApp)
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}
cmd := exec.Command("sudo", yarn, "add", packageName, "--cwd", pathToApp)
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}
func gitPull(pathToApp string) ([]byte, error) {
git := executables["git"]
gitDir := "--git-dir=" + pathToApp + "/.git"
gitWorkTree := "--work-tree=" + pathToApp
cmd := exec.Command(git, gitDir, gitWorkTree, "pull")
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}
func gitPush(pathToApp string) ([]byte, error) {
git := executables["git"]
gitDir := "--git-dir=" + pathToApp + "/.git"
gitWorkTree := "--work-tree=" + pathToApp
cmd := exec.Command(git, gitDir, gitWorkTree, "push")
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}
func gitCommit(pathToApp, commitMsg string) ([]byte, error) {
git := executables["git"]
gitDir := "--git-dir=" + pathToApp + "/.git"
gitWorkTree := "--work-tree=" + pathToApp
cmd := exec.Command(git, gitDir, gitWorkTree, "add", pathToApp)
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
out, err := cmd.Output()
if err != nil {
return out, err
}
cmd = exec.Command(git, gitDir, "commit", "-m", commitMsg)
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}
func gitStashPush(pathToApp string, stashMsg string) ([]byte, error) {
git := executables["git"]
gitDir := "--git-dir=" + pathToApp + "/.git"
gitWorkTree := "--work-tree=" + pathToApp
cmd := exec.Command(git, gitDir, gitWorkTree, "stash", "push", "-u", "-m", stashMsg)
logBlue(fmt.Sprintf("Executable command: %s\n", cmd.String()))
return cmd.Output()
}