-
Notifications
You must be signed in to change notification settings - Fork 1
/
installer_linux.go
99 lines (86 loc) · 2.38 KB
/
installer_linux.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
package installer
import (
"os"
"os/exec"
"path/filepath"
)
const (
shareAppPath = ".local/share/applications"
xdgMime = "xdg-mime"
xdgSchemeHandler = "x-scheme-handler"
desktopExt = ".desktop"
)
//AddStepCreateScheme registers a new scheme by
//copying a customprotocol.desktop file in application dir
//located in ~/.local/shared/applications.
//A desktop file contains a link or a bash cmd that will
//be triggered when scheme is called
func (i *installer) AddStepCreateScheme(protoc string, content []byte) {
process := func() error { return createScheme(protoc, content) }
description := i.getRegisterSchemeText(protoc)
i.AddStep(process, description)
}
func (i *installer) AddStepDeleteScheme(scheme string) {
process := func() error { return deleteScheme(scheme) }
description := i.getUnregisterSchemeText(scheme)
i.AddStep(process, description)
}
func deleteScheme(scheme string) error {
return deleteDesktopFile(getSchemeDesktopFileName(scheme))
}
func deleteDesktopFile(file string) error {
path, err := getDotDesktopFilePath(file)
if err != nil {
return err
}
return rmvPath(path)
}
func getShareAppFullPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, shareAppPath), nil
}
func getDotDesktopFilePath(file string) (string, error) {
p, err := getShareAppFullPath()
if err != nil {
return "", err
}
return filepath.Join(p, file), nil
}
func getSchemeDesktopFileName(scheme string) string {
return scheme + desktopExt
}
func createScheme(protoc string, content []byte) error {
desktopFile := protoc + desktopExt
if err := mkAllShareAppDirPath(); err != nil {
return err
}
err := copyDesktopFile(desktopFile, content)
if err != nil {
return err
}
return runXDGMime(desktopFile, protoc)
}
func mkAllShareAppDirPath() error {
path, err := getShareAppFullPath()
if err != nil {
return err
}
return mkDirAll(path)
}
//runXDGMime calls xdg mime app to bind a scheme to a .desktop file
func runXDGMime(desktopFile, scheme string) error {
handler := filepath.Join(xdgSchemeHandler, scheme)
cmd := exec.Command(xdgMime, "default", desktopFile, handler)
return cmd.Run()
}
func copyDesktopFile(file string, content []byte) error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
filePath := filepath.Join(home, shareAppPath, file)
return copyFile(filePath, content)
}