-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinstall_darwin.go
143 lines (123 loc) · 4.63 KB
/
install_darwin.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
// +build darwin
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
)
func install(chromeExtID string, firefoxExtID string) bool {
currentUser, error := user.Current()
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot get current user\n"))
return false
}
_ = os.Mkdir(filepath.Join(currentUser.HomeDir, "Socketify"), os.ModePerm)
binaryPath, error := os.Executable()
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot get binary path\n"))
return false
}
binaryStat, error := os.Stat(binaryPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot get binary stat\n"))
return false
}
if binaryStat.Mode().IsRegular() != true {
os.Stdout.Write([]byte("install: binary is not a regular file\n"))
return false
}
binaryBytes, error := ioutil.ReadFile(binaryPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot read binary\n"))
return false
}
userBinaryPath := filepath.Join(currentUser.HomeDir, "Socketify", binaryStat.Name())
error = ioutil.WriteFile(userBinaryPath, binaryBytes, os.ModePerm)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot write binary\n"))
return false
}
chromeManifestString := chromeManifest
chromeManifestString = strings.Replace(chromeManifestString, "BIN_PATH", filepath.ToSlash(userBinaryPath), 1)
chromeManifestString = strings.Replace(chromeManifestString, "EXT_ID", chromeExtID, 1)
firefoxManifestString := firefoxManifest
firefoxManifestString = strings.Replace(firefoxManifestString, "BIN_PATH", filepath.ToSlash(userBinaryPath), 1)
firefoxManifestString = strings.Replace(firefoxManifestString, "EXT_ID", firefoxExtID, 1)
chromeManifestPath := filepath.Join(currentUser.HomeDir,
"Library", "Application Support",
"Google", "Chrome", "NativeMessagingHosts",
"net.socketify.messenger.json")
error = ioutil.WriteFile(chromeManifestPath, []byte(chromeManifestString), os.ModePerm)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot write chrome manifest\n"))
return false
}
firefoxManifestPath := filepath.Join(currentUser.HomeDir,
"Library", "Application Support",
"Mozilla", "NativeMessagingHosts",
"net.socketify.messenger.json")
error = ioutil.WriteFile(firefoxManifestPath, []byte(firefoxManifestString), os.ModePerm)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("install: cannot write firefox manifest\n"))
return false
}
return true
}
func uninstall() {
currentUser, error := user.Current()
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot get current user\n"))
return
}
binaryPath, error := os.Executable()
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot get binary path\n"))
return
}
binaryStat, error := os.Stat(binaryPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("install: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot get binary stat\n"))
return
}
if binaryStat.Mode().IsRegular() != true {
os.Stdout.Write([]byte("uninstall: binary is not a regular file\n"))
return
}
userBinaryPath := filepath.Join(currentUser.HomeDir, "Socketify", binaryStat.Name())
error = os.Remove(userBinaryPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot remove binary\n"))
}
chromeManifestPath := filepath.Join(currentUser.HomeDir,
"Library", "Application Support",
"Google", "Chrome", "NativeMessagingHosts",
"net.socketify.messenger.json")
error = os.Remove(chromeManifestPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot remove chrome manifest\n"))
}
firefoxManifestPath := filepath.Join(currentUser.HomeDir,
"Library", "Application Support",
"Mozilla", "NativeMessagingHosts",
"net.socketify.messenger.json")
error = os.Remove(firefoxManifestPath)
if error != nil {
os.Stdout.Write([]byte(fmt.Sprintf("uninstall: %s\n", error.Error())))
os.Stdout.Write([]byte("uninstall: cannot remove firefox manifest\n"))
}
}