Skip to content

Commit c18ca8a

Browse files
committed
Adjustment from the tests.
1 parent f84e9d8 commit c18ca8a

File tree

3 files changed

+42
-78
lines changed

3 files changed

+42
-78
lines changed

manifest.go

+17-33
Original file line numberDiff line numberDiff line change
@@ -11,66 +11,51 @@ package host
1111

1212
import (
1313
"encoding/json"
14-
"io/ioutil"
1514
"log"
1615
"os"
17-
"os/user"
1816
"path/filepath"
19-
"runtime"
2017
)
2118

2219
// getTargetName returns an absolute path to native messaging host manifest
23-
// location for Linux. It will return error when it come across one.
20+
// location for Linux.
21+
//
2422
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
25-
func (h *Host) getTargetName() (string, error) {
23+
func (h *Host) getTargetName() string {
2624
target := "/etc/opt/chrome/native-messaging-hosts"
2725

28-
current, err := user.Current()
29-
if err != nil {
30-
return "", err
31-
}
32-
33-
if current.Uid != "0" {
34-
target = current.HomeDir + "/.config/google-chrome/NativeMessagingHosts"
26+
if os.Getuid() != 0 {
27+
homeDir, _ := os.UserHomeDir()
28+
target = homeDir + "/.config/google-chrome/NativeMessagingHosts"
3529
}
3630

37-
return filepath.Join(target, h.AppName+".json"), nil
31+
return filepath.Join(target, h.AppName+".json")
3832
}
3933

4034
// Install creates native-messaging manifest file on appropriate location. It
4135
// will return error when it come across one.
36+
//
4237
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
4338
func (h *Host) Install() error {
44-
targetName, err := h.getTargetName()
45-
if err != nil {
46-
return err
47-
}
48-
49-
if err := os.MkdirAll(filepath.Dir(targetName), 0755); err != nil {
50-
return err
51-
}
39+
manifest, _ := json.MarshalIndent(h, "", " ")
40+
targetName := h.getTargetName()
5241

53-
manifest, err := json.MarshalIndent(h, "", " ")
54-
if err != nil {
42+
if err := osMkdirAll(filepath.Dir(targetName), 0755); err != nil {
5543
return err
5644
}
5745

58-
if err := ioutil.WriteFile(targetName, manifest, 0644); err != nil {
46+
if err := ioutilWriteFile(targetName, manifest, 0644); err != nil {
5947
return err
6048
}
6149

6250
log.Printf("Installed: %s", targetName)
6351
return nil
6452
}
6553

66-
// Uninstall removes native-messaging manifest file from installed location. It
67-
// will return error when it come across one.
54+
// Uninstall removes native-messaging manifest file from installed location.
55+
//
6856
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
69-
func (h *Host) Uninstall() error {
70-
targetName, err := h.getTargetName()
71-
if err != nil {
72-
return err
73-
}
57+
func (h *Host) Uninstall() {
58+
targetName := h.getTargetName()
7459

7560
if err := os.Remove(targetName); err != nil {
7661
// It might never have been installed.
@@ -90,6 +75,5 @@ func (h *Host) Uninstall() error {
9075
log.Printf("Uninstalled: %s", targetName)
9176

9277
// Exit gracefully.
93-
runtime.Goexit()
94-
return nil
78+
runtimeGoexit()
9579
}

manifest_darwin.go

+17-33
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,51 @@ package host
99

1010
import (
1111
"encoding/json"
12-
"io/ioutil"
1312
"log"
1413
"os"
15-
"os/user"
1614
"path/filepath"
17-
"runtime"
1815
)
1916

2017
// getTargetName returns an absolute path to native messaging host manifest
21-
// location for OS X. It will return error when it come across one.
18+
// location for OS X.
19+
//
2220
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
23-
func (h *Host) getTargetName() (string, error) {
21+
func (h *Host) getTargetName() string {
2422
target := "/Library/Google/Chrome/NativeMessagingHosts"
2523

26-
current, err := user.Current()
27-
if err != nil {
28-
return "", err
29-
}
30-
31-
if current.Uid != "0" {
32-
target = current.HomeDir + "/Library/Application Support/Google/Chrome/NativeMessagingHosts"
24+
if os.Getuid() != 0 {
25+
homeDir, _ := os.UserHomeDir()
26+
target = homeDir + "/Library/Application Support/Google/Chrome/NativeMessagingHosts"
3327
}
3428

35-
return filepath.Join(target, h.AppName+".json"), nil
29+
return filepath.Join(target, h.AppName+".json")
3630
}
3731

3832
// Install creates native-messaging manifest file on appropriate location. It
3933
// will return error when it come across one.
34+
//
4035
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
4136
func (h *Host) Install() error {
42-
targetName, err := h.getTargetName()
43-
if err != nil {
44-
return err
45-
}
46-
47-
if err := os.MkdirAll(filepath.Dir(targetName), 0755); err != nil {
48-
return err
49-
}
37+
manifest, _ := json.MarshalIndent(h, "", " ")
38+
targetName := h.getTargetName()
5039

51-
manifest, err := json.MarshalIndent(h, "", " ")
52-
if err != nil {
40+
if err := osMkdirAll(filepath.Dir(targetName), 0755); err != nil {
5341
return err
5442
}
5543

56-
if err := ioutil.WriteFile(targetName, manifest, 0644); err != nil {
44+
if err := ioutilWriteFile(targetName, manifest, 0644); err != nil {
5745
return err
5846
}
5947

6048
log.Printf("Installed: %s", targetName)
6149
return nil
6250
}
6351

64-
// Uninstall removes native-messaging manifest file from installed location. It
65-
// will return error when it come across one.
52+
// Uninstall removes native-messaging manifest file from installed location.
53+
//
6654
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
67-
func (h *Host) Uninstall() error {
68-
targetName, err := h.getTargetName()
69-
if err != nil {
70-
return err
71-
}
55+
func (h *Host) Uninstall() {
56+
targetName := h.getTargetName()
7257

7358
if err := os.Remove(targetName); err != nil {
7459
// It might never have been installed.
@@ -88,6 +73,5 @@ func (h *Host) Uninstall() error {
8873
log.Printf("Uninstalled: %s", targetName)
8974

9075
// Exit gracefully.
91-
runtime.Goexit()
92-
return nil
76+
runtimeGoexit()
9377
}

manifest_windows.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,18 @@ import (
1414
"log"
1515
"os"
1616
"path/filepath"
17-
"runtime"
1817
)
1918

2019
// Install creates native-messaging manifest file on appropriate location and
2120
// add an entry in windows registry. It will return error when it come across
2221
// one.
22+
//
2323
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location
2424
func (h *Host) Install() error {
25+
manifest, _ := json.MarshalIndent(h, "", " ")
2526
registryName := `Software\Google\Chrome\NativeMessagingHosts\` + h.AppName
2627
targetName := filepath.Join(filepath.Dir(h.ExecName), h.AppName+".json")
2728

28-
manifest, err := json.MarshalIndent(h, "", " ")
29-
if err != nil {
30-
return err
31-
}
32-
3329
if err := ioutil.WriteFile(targetName, manifest, 0644); err != nil {
3430
return err
3531
}
@@ -49,16 +45,17 @@ func (h *Host) Install() error {
4945
}
5046

5147
// Uninstall removes entry from windows registry and removes native-messaging
52-
// manifest file from installed location. It will return error when it come
53-
// across one.
48+
// manifest file from installed location.
49+
//
5450
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location
55-
func (h *Host) Uninstall() error {
51+
func (h *Host) Uninstall() {
5652
registryName := `Software\Google\Chrome\NativeMessagingHosts\` + h.AppName
5753
targetName := filepath.Join(filepath.Dir(h.ExecName), h.AppName+".json")
5854

5955
key, err := registry.OpenKey(registry.CURRENT_USER, registryName, registry.SET_VALUE)
6056
if err != nil {
61-
return err
57+
// Unable to open windows registry.
58+
log.Print(err)
6259
}
6360
defer key.Close()
6461

@@ -85,6 +82,5 @@ func (h *Host) Uninstall() error {
8582
log.Printf(`Uninstalled: HKCU\%s`, registryName)
8683

8784
// Exit gracefully.
88-
runtime.Goexit()
89-
return nil
85+
runtimeGoexit()
9086
}

0 commit comments

Comments
 (0)