@@ -92,59 +92,44 @@ func handleRun(w http.ResponseWriter, r *http.Request) {
92
92
}
93
93
}
94
94
95
- type chromeExtInstall struct {}
95
+ type installExtHostCmd struct {}
96
96
97
- func (c * chromeExtInstall ) Spec () cli.CommandSpec {
97
+ func (c * installExtHostCmd ) Spec () cli.CommandSpec {
98
98
return cli.CommandSpec {
99
- Name : "install-for-chrome- ext" ,
100
- Desc : `Installs the chrome native message host manifest.
101
- This allows the sail chrome extension to manage sail.` ,
99
+ Name : "install-ext-host " ,
100
+ Desc : `Installs the native message host manifest into Chrome and Firefox .
101
+ This allows the sail extension to manage sail.` ,
102
102
}
103
103
}
104
104
105
- func (c * chromeExtInstall ) Run (fl * flag.FlagSet ) {
106
- nativeHostDirs , err := nativeMessageHostManifestDirectories ()
105
+ func (c * installExtHostCmd ) Run (fl * flag.FlagSet ) {
106
+ binPath , err := os . Executable ()
107
107
if err != nil {
108
- flog .Fatal ("failed to get native message host manifest directory: %v" , err )
108
+ flog .Fatal ("failed to get sail binary location" )
109
109
}
110
110
111
- for _ , dir := range nativeHostDirs {
112
- if dir == "" {
113
- continue
114
- }
115
-
116
- err = os .MkdirAll (dir , 0755 )
117
- if err != nil {
118
- flog .Fatal ("failed to ensure manifest directory exists: %v" , err )
119
- }
120
- err = writeNativeHostManifest (dir )
121
- if err != nil {
122
- flog .Fatal ("failed to write native messaging host manifest: %v" , err )
123
- }
111
+ nativeHostDirsChrome , err := nativeMessageHostManifestDirectoriesChrome ()
112
+ if err != nil {
113
+ flog .Fatal ("failed to get chrome native message host manifest directory: %v" , err )
124
114
}
125
- }
126
-
127
- func writeNativeHostManifest (dir string ) error {
128
- binPath , err := os .Executable ()
115
+ err = installManifests (nativeHostDirsChrome , "com.coder.sail.json" , chromeManifest (binPath ))
129
116
if err != nil {
130
- return err
117
+ flog . Fatal ( "failed to write chrome manifest files: %v" , err )
131
118
}
132
119
133
- manifest := fmt .Sprintf (`{
134
- "name": "com.coder.sail",
135
- "description": "sail message host",
136
- "path": "%v",
137
- "type": "stdio",
138
- "allowed_origins": [
139
- "chrome-extension://deeepphleikpinikcbjplcgojfhkcmna/"
140
- ]
141
- }` , binPath )
120
+ nativeHostDirsFirefox , err := nativeMessageHostManifestDirectoriesFirefox ()
121
+ if err != nil {
122
+ flog .Fatal ("failed to get firefox native message host manifest directory: %v" , err )
123
+ }
124
+ err = installManifests (nativeHostDirsFirefox , "com.coder.sail.json" , firefoxManifest (binPath ))
125
+ if err != nil {
126
+ flog .Fatal ("failed to write firefox manifest files: %v" , err )
127
+ }
142
128
143
- dst := path .Join (dir , "com.coder.sail.json" )
144
- return ioutil .WriteFile (dst , []byte (manifest ), 0644 )
129
+ flog .Info ("Successfully installed manifests." )
145
130
}
146
131
147
- func nativeMessageHostManifestDirectories () ([]string , error ) {
132
+ func nativeMessageHostManifestDirectoriesChrome () ([]string , error ) {
148
133
homeDir , err := os .UserHomeDir ()
149
134
if err != nil {
150
135
return nil , xerrors .Errorf ("failed to get user home dir: %w" , err )
@@ -178,3 +163,87 @@ func nativeMessageHostManifestDirectories() ([]string, error) {
178
163
chromeCanaryDir ,
179
164
}, nil
180
165
}
166
+
167
+ func chromeManifest (binPath string ) string {
168
+ return fmt .Sprintf (`{
169
+ "name": "com.coder.sail",
170
+ "description": "sail message host",
171
+ "path": "%v",
172
+ "type": "stdio",
173
+ "allowed_origins": [
174
+ "chrome-extension://deeepphleikpinikcbjplcgojfhkcmna/"
175
+ ]
176
+ }` , binPath )
177
+ }
178
+
179
+ func nativeMessageHostManifestDirectoriesFirefox () ([]string , error ) {
180
+ homeDir , err := os .UserHomeDir ()
181
+ if err != nil {
182
+ return nil , xerrors .Errorf ("failed to get user home dir: %w" , err )
183
+ }
184
+
185
+ var firefoxDir string
186
+
187
+ switch runtime .GOOS {
188
+ case "linux" :
189
+ firefoxDir = path .Join (homeDir , ".mozilla" , "native-messaging-hosts" )
190
+ case "darwin" :
191
+ firefoxDir = path .Join (homeDir , "Library" , "Application Support" , "Mozilla" , "NativeMessagingHosts" )
192
+ default :
193
+ return nil , xerrors .Errorf ("unsupported os %q" , runtime .GOOS )
194
+ }
195
+
196
+ return []string {
197
+ firefoxDir ,
198
+ }, nil
199
+ }
200
+
201
+ func firefoxManifest (binPath string ) string {
202
+ return fmt .Sprintf (`{
203
+ "name": "com.coder.sail",
204
+ "description": "sail message host",
205
+ "path": "%v",
206
+ "type": "stdio",
207
+ "allowed_extensions": [
208
+
209
+ ]
210
+ }` , binPath )
211
+ }
212
+
213
+ func installManifests (nativeHostDirs []string , file string , content string ) error {
214
+ data := []byte (content )
215
+
216
+ for _ , dir := range nativeHostDirs {
217
+ if dir == "" {
218
+ continue
219
+ }
220
+
221
+ err := os .MkdirAll (dir , 0755 )
222
+ if err != nil {
223
+ return xerrors .Errorf ("failed to ensure manifest directory exists: %w" , err )
224
+ }
225
+
226
+ dst := path .Join (dir , file )
227
+ err = ioutil .WriteFile (dst , data , 0644 )
228
+ if err != nil {
229
+ return xerrors .Errorf ("failed to write native messaging host manifest: %w" , err )
230
+ }
231
+ }
232
+
233
+ return nil
234
+ }
235
+
236
+ type chromeExtInstallCmd struct {
237
+ cmd * installExtHostCmd
238
+ }
239
+
240
+ func (c * chromeExtInstallCmd ) Spec () cli.CommandSpec {
241
+ return cli.CommandSpec {
242
+ Name : "install-for-chrome-ext" ,
243
+ Desc : "DEPRECATED: alias of install-ext-host." ,
244
+ }
245
+ }
246
+
247
+ func (c * chromeExtInstallCmd ) Run (fl * flag.FlagSet ) {
248
+ c .cmd .Run (fl )
249
+ }
0 commit comments