This repository has been archived by the owner on Feb 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
328 lines (293 loc) · 9.99 KB
/
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"github.com/kardianos/osext"
"strings"
"fmt"
"os"
"path/filepath"
"runtime"
"os/exec"
"io/ioutil"
"syscall"
)
func fail(message string, args ...interface{}) {
fmt.Fprintf(os.Stderr, message + "\n\n", args...)
os.Exit(1)
}
func usageAndFail() {
fail("Usage: %s <install|uninstall> <targetExecutable...>", os.Args[0])
}
func getCwdOrFail() string {
result, err := os.Getwd();
if err != nil {
fail("Could not get current working directory. Got: %s", err.Error())
}
return result
}
func createTargetExecutableFor(sourceExecutable string) string {
sourceExecutableDir := filepath.Dir(sourceExecutable)
sourceExecutableFile := filepath.Base(sourceExecutable)
targetExecutableDir := sourceExecutableDir
targetExecutableFile := "." + sourceExecutableFile
targetExecutable := filepath.Join(targetExecutableDir, targetExecutableFile)
return targetExecutable
}
func createSourceExecutableFor(targetExecutable string) (string, bool) {
targetExecutableDir := filepath.Dir(targetExecutable)
targetExecutableFile := filepath.Base(targetExecutable)
sourceExecutableDir := targetExecutableDir
if !strings.HasPrefix(targetExecutableFile, ".") || len(targetExecutableFile) < 1 {
return "", false
}
sourceExecutableFile := targetExecutableFile[1:]
sourceExecutable := filepath.Join(sourceExecutableDir, sourceExecutableFile)
return sourceExecutable, true
}
func isExecutable(filePath string) bool {
fileInfo, err := os.Lstat(filePath)
if err != nil {
return false
}
if runtime.GOOS == "windows" {
ext := strings.ToLower(filepath.Ext(filePath))
if ext == ".bat" || ext == ".cmd" || ext == ".exe" {
return true
}
return false
}
return hasPermission(0555, fileInfo.Mode())
}
func hasPermission(required os.FileMode, value os.FileMode) bool {
if (value & required) == required {
return true
}
return false
}
func makeAbsoluteOrExit(what string) string {
result, err := filepath.Abs(what)
if err != nil {
fail("It is not possible to make '%s' absolute. Got: %s", what, err.Error())
}
return result
}
func absoluteLauncherFor(sourceExecutable string) string {
return makeAbsoluteOrExit(sourceExecutable)
}
func launcherTargetFor(launcher string, source string) string {
launcherDirectory := filepath.Dir(launcher)
launcherFile := filepath.Base(launcher)
launcherTargetDirectory, err := filepath.Rel(filepath.Dir(source), launcherDirectory)
if err != nil {
launcherTargetDirectory = launcherDirectory
}
launcherTarget := filepath.Join(launcherTargetDirectory, launcherFile)
return launcherTarget;
}
func getAbsoluteSymlinkOf(file string) (string, bool) {
absoluteFile, err := filepath.Abs(file)
if err != nil {
return "", false
}
fileInfo, err := os.Lstat(absoluteFile)
if err != nil {
fail("Cannot get absolute symlink. %v", err)
}
if (fileInfo.Mode() & os.ModeSymlink) != 0 {
target, err := os.Readlink(absoluteFile)
if err != nil {
fail("Cannot get absolute symlink. %v", err)
}
oldCwd := getCwdOrFail()
if err := os.Chdir(filepath.Dir(file)); err != nil {
fail("Cannot get absolute symlink. %v", err)
}
absolute, err := filepath.Abs(target)
if err := os.Chdir(oldCwd); err != nil {
fail("Cannot get absolute symlink. %v", err)
}
return absolute, true
}
return "", false
}
func install(candidate string, absoluteLauncher string) {
launcherTarget := launcherTargetFor(absoluteLauncher, candidate)
candidateTarget := createTargetExecutableFor(candidate)
err := os.Rename(candidate, candidateTarget)
if err != nil {
fail("Cannot rename candidate to its new name. Got: %v", err)
}
oldCwd := getCwdOrFail()
os.Chdir(filepath.Dir(launcherTarget))
err = os.Symlink(launcherTarget, candidate)
if err != nil {
fail("Could not place launcher on place of candiate. Got: %v", err)
}
os.Chdir(oldCwd)
fmt.Fprintf(os.Stdout, "Launcher installed on %s.\n", candidate)
}
func isLauncher(what string, absoluteLauncher string) bool {
if linkTarget, ok := getAbsoluteSymlinkOf(what); ok {
if (linkTarget == absoluteLauncher) {
return true
}
}
return false
}
func checkAndInstall(candidate string, absoluteLauncher string) {
if isLauncher(candidate, absoluteLauncher) {
return
}
possibleSourceCandidate, ok := createSourceExecutableFor(candidate)
if ok && isLauncher(possibleSourceCandidate, absoluteLauncher) {
return
}
install(candidate, absoluteLauncher)
}
func installOnAllOf(this string, candidatePatterns []string) {
absoluteLauncher := absoluteLauncherFor(this)
for _, candidatesPattern := range candidatePatterns {
candidates, err := filepath.Glob(candidatesPattern)
if err != nil {
fail("Could not install for '%s'. Got: %v", candidatesPattern, err)
}
for _, plainCandidate := range candidates {
if isExecutable(plainCandidate) {
checkAndInstall(makeAbsoluteOrExit(plainCandidate), absoluteLauncher)
}
}
}
}
func uninstall(candidate string, absoluteLauncher string) {
candidateTarget := createTargetExecutableFor(candidate)
if err := os.Remove(candidate); err != nil {
fail("Could not remove current launcher. Got: %v", err)
}
if err := os.Rename(candidateTarget, candidate); err != nil {
fail("Could not move the candidate '%s' back to its old name '%s'. Got: %v", candidateTarget, candidate, err)
}
fmt.Fprintf(os.Stdout, "Launcher removed from %s.\n", candidate)
}
func checkAndUninstall(candidate string, absoluteLauncher string) {
if isLauncher(candidate, absoluteLauncher) {
uninstall(candidate, absoluteLauncher)
}
}
func uninstallOnAllOf(this string, candidatePatterns []string) {
absoluteLauncher := absoluteLauncherFor(this)
for _, candidatesPattern := range candidatePatterns {
candidates, err := filepath.Glob(candidatesPattern)
if err != nil {
fail("Could not install for '%s'. Got: %v", candidatesPattern, err)
}
for _, plainCandidate := range candidates {
if isExecutable(plainCandidate) {
checkAndUninstall(makeAbsoluteOrExit(plainCandidate), absoluteLauncher)
}
}
}
}
func execute(targetExecutable string) {
arguments := []string{targetExecutable}
if (len(os.Args) > 1) {
arguments = append(arguments, os.Args[1:]...)
}
command := exec.Cmd{
Path: targetExecutable,
Args: arguments,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
var waitStatus syscall.WaitStatus
if err := command.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
os.Exit(int(waitStatus.ExitStatus()))
} else {
fail("Could not start process. Got: %v", err)
}
} else {
waitStatus = command.ProcessState.Sys().(syscall.WaitStatus)
os.Exit(int(waitStatus.ExitStatus()))
}
}
func isEnabled(value string) bool {
trimmed := strings.TrimSpace(strings.ToLower(value))
if trimmed == "yes" || trimmed == "y" || trimmed == "true" || trimmed == "1" || trimmed == "on" {
return true
}
return false
}
func isExecutionAllowed() bool {
if byteContent, err := ioutil.ReadFile("/etc/oracle-javase-license-accepted"); err == nil {
if isEnabled(string(byteContent)) {
return true
}
}
if isEnabled(os.Getenv("ORACLE_JAVASE_LICENSE_ACCEPTED")) {
return true
}
return false
}
func permitExecutionAndDisplayInstructions() {
fmt.Fprint(os.Stderr, "You must accept the 'Oracle Binary Code License Agreement for Java SE' to use this software.\n" +
"\n" +
"To do this, follow the steps below:\n" +
"1# Read http://www.oracle.com/technetwork/java/javase/terms/license/index.html in your web browser.\n" +
"2# Accept the license by choosing one of the following possibilities:\n" +
"\ta) Place a readable file named '/etc/oracle-javase-license-accepted' with content 'yes'.\n" +
"\tOR\n" +
"\tb) Provide an environment variable 'ORACLE_JAVASE_LICENSE_ACCEPTED' with content 'yes'.\n" +
"\n" +
"This program will exit now.\n")
os.Exit(127)
}
func runAsInstallerMode(this string) {
if len(os.Args) <= 2 {
usageAndFail()
}
command := strings.ToLower(strings.TrimSpace(os.Args[1]))
if command == "install" || command == "i" {
installOnAllOf(this, os.Args[2:])
} else if command == "uninstall" || command == "u" || command == "remove" || command == "r" {
uninstallOnAllOf(this, os.Args[2:])
} else {
usageAndFail()
}
}
func resolveThisExecutable() (string, error) {
if runtime.GOOS == "windows" {
return osext.Executable()
} else {
this := os.Args[0]
if this[0] == '/' || this[0] == '.' {
return makeAbsoluteOrExit(this), nil
} else {
execIsInPath, err := exec.LookPath(this)
if err != nil {
return "", err
}
return execIsInPath, nil
}
}
}
func main() {
this, err := resolveThisExecutable()
if err != nil {
fail("Could not get name of myself. Got: %v", err)
}
if _, ok := getAbsoluteSymlinkOf(this); ok {
targetExecutable := createTargetExecutableFor(this)
if _, err := os.Stat(targetExecutable); err == nil {
if isExecutionAllowed() {
execute(targetExecutable)
} else {
permitExecutionAndDisplayInstructions()
}
} else {
runAsInstallerMode(this)
}
} else {
runAsInstallerMode(this)
}
}