-
Notifications
You must be signed in to change notification settings - Fork 6
/
manifest_darwin.go
77 lines (62 loc) · 2.09 KB
/
manifest_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
// manifest_darwin.go - Install and Uninstall manifest file for OS X.
// Copyright (c) 2018 - 2020 Richard Huang <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package host
import (
"encoding/json"
"log"
"os"
"path/filepath"
)
// getTargetName returns an absolute path to native messaging host manifest
// location for OS X.
//
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
func (h *Host) getTargetName() string {
target := "/Library/Google/Chrome/NativeMessagingHosts"
if os.Getuid() != 0 {
homeDir, _ := os.UserHomeDir()
target = homeDir + "/Library/Application Support/Google/Chrome/NativeMessagingHosts"
}
return filepath.Join(target, h.AppName+".json")
}
// Install creates native-messaging manifest file on appropriate location. It
// will return error when it come across one.
//
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
func (h *Host) Install() error {
manifest, _ := json.MarshalIndent(h, "", " ")
targetName := h.getTargetName()
if err := osMkdirAll(filepath.Dir(targetName), 0755); err != nil {
return err
}
if err := ioutilWriteFile(targetName, manifest, 0644); err != nil {
return err
}
log.Printf("Installed: %s", targetName)
return nil
}
// Uninstall removes native-messaging manifest file from installed location.
//
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location-nix
func (h *Host) Uninstall() {
targetName := h.getTargetName()
if err := os.Remove(targetName); err != nil {
// It might never have been installed.
log.Print(err)
}
if err := os.Remove(h.ExecName); err != nil {
// It might be locked by current process.
log.Print(err)
}
if err := os.Remove(h.ExecName + ".chk"); err != nil {
// It might not exist.
log.Print(err)
}
log.Printf("Uninstalled: %s", targetName)
// Exit gracefully.
runtimeGoexit()
}