Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit 87f05b6

Browse files
Add Firefox native messaging manifest installer
Renamed install-for-chrome-ext to install-ext-host, added Firefox support and added a new deprecated command alias for the old name. Co-authored-by: Luca Casonato <[email protected]>
1 parent 3efad4e commit 87f05b6

File tree

5 files changed

+117
-46
lines changed

5 files changed

+117
-46
lines changed

chrome.go renamed to extension.go

Lines changed: 107 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -92,59 +92,44 @@ func handleRun(w http.ResponseWriter, r *http.Request) {
9292
}
9393
}
9494

95-
type chromeExtInstall struct{}
95+
type installExtHostCmd struct{}
9696

97-
func (c *chromeExtInstall) Spec() cli.CommandSpec {
97+
func (c *installExtHostCmd) Spec() cli.CommandSpec {
9898
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.`,
102102
}
103103
}
104104

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()
107107
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")
109109
}
110110

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)
124114
}
125-
}
126-
127-
func writeNativeHostManifest(dir string) error {
128-
binPath, err := os.Executable()
115+
err = installManifests(nativeHostDirsChrome, "com.coder.sail.json", chromeManifest(binPath))
129116
if err != nil {
130-
return err
117+
flog.Fatal("failed to write chrome manifest files: %v", err)
131118
}
132119

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+
}
142128

143-
dst := path.Join(dir, "com.coder.sail.json")
144-
return ioutil.WriteFile(dst, []byte(manifest), 0644)
129+
flog.Info("Successfully installed manifests.")
145130
}
146131

147-
func nativeMessageHostManifestDirectories() ([]string, error) {
132+
func nativeMessageHostManifestDirectoriesChrome() ([]string, error) {
148133
homeDir, err := os.UserHomeDir()
149134
if err != nil {
150135
return nil, xerrors.Errorf("failed to get user home dir: %w", err)
@@ -178,3 +163,87 @@ func nativeMessageHostManifestDirectories() ([]string, error) {
178163
chromeCanaryDir,
179164
}, nil
180165
}
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+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ require (
2626
go.coder.com/cli v0.1.1-0.20190426214427-610063ae7153
2727
go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8
2828
golang.org/x/sys v0.0.0-20190415145633-3fd5a3612ccd // indirect
29-
golang.org/x/xerrors v0.0.0-20190315151331-d61658bd2e18
29+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
3030
google.golang.org/grpc v1.20.0 // indirect
3131
gotest.tools v2.2.0+incompatible // indirect
3232
nhooyr.io/websocket v0.2.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ golang.org/x/tools v0.0.0-20190419195823-c39e7748f6eb h1:JbWwiXQ1L1jWKTGSwj6y63W
110110
golang.org/x/tools v0.0.0-20190419195823-c39e7748f6eb/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
111111
golang.org/x/xerrors v0.0.0-20190315151331-d61658bd2e18 h1:1AGvnywFL1aB5KLRxyLseWJI6aSYPo3oF7HSpXdWQdU=
112112
golang.org/x/xerrors v0.0.0-20190315151331-d61658bd2e18/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
113+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
114+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
113115
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
114116
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
115117
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@ func (r *rootCmd) RegisterFlags(fl *flag.FlagSet) {
5858
}
5959

6060
func (r rootCmd) Subcommands() []cli.Command {
61+
extHostCmd := &installExtHostCmd{}
62+
6163
return []cli.Command{
6264
&runcmd{gf: &r.globalFlags},
6365
&shellcmd{gf: &r.globalFlags},
6466
&editcmd{gf: &r.globalFlags},
6567
&lscmd{},
6668
&rmcmd{gf: &r.globalFlags},
6769
&proxycmd{},
68-
&chromeExtInstall{},
70+
extHostCmd,
71+
&chromeExtInstallCmd{cmd: extHostCmd},
6972
&versioncmd{},
7073
}
7174
}
@@ -74,7 +77,8 @@ func main() {
7477
root := &rootCmd{}
7578

7679
if (len(os.Args) >= 2 && strings.HasPrefix(os.Args[1], "chrome-extension://")) ||
77-
(len(os.Args) >= 3 && strings.HasPrefix(os.Args[2], "chrome-extension://")) {
80+
(len(os.Args) >= 3 && strings.HasPrefix(os.Args[2], "chrome-extension://")) ||
81+
(len(os.Args) >= 2 && strings.HasSuffix(os.Args[1], "com.coder.sail.json")) {
7882
runNativeMsgHost()
7983
return
8084
}

site/content/docs/browser-extension.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ The Sail browser extension allows you to open GitHub or GitLab projects with a s
1515
## Install
1616

1717
1. [Install Sail if you haven't already](/docs/installation)
18-
1. Run `sail install-for-chrome-ext` to install the chrome extension manifest.json
18+
1. Run `sail install-ext-host` to install the extension manifest.json
1919
1. [Install the extension from the Chrome Marketplace](https://chrome.google.com/webstore/detail/sail/deeepphleikpinikcbjplcgojfhkcmna)
2020
1. Get Sailing!
21-
22-
23-
24-

0 commit comments

Comments
 (0)