forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
148 lines (122 loc) · 3.58 KB
/
setup.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package manifest
import (
"encoding/json"
"fmt"
"os"
"path"
"io/ioutil"
"path/filepath"
homedir "github.com/mitchellh/go-homedir"
)
type configuredFile struct {
path string
content string
}
// PrintSummary prints path and content of manifest and wrapper script
func PrintSummary(browser, wrapperPath, libpath string, global bool) error {
manifestFile, err := getManifest(browser, wrapperPath, libpath, global)
if err != nil {
return err
}
printConfiguredFile("Native Messaging Host Manifest", manifestFile)
wrapperFile, err := getWrapper(wrapperPath)
if err != nil {
return err
}
printConfiguredFile("Wrapper", wrapperFile)
return nil
}
// SetUp actually creates the manifest and wrapper scripts
func SetUp(browser, wrapperPath, libpath string, global bool) error {
manifestFile, err := getManifest(browser, wrapperPath, libpath, global)
if err != nil {
return err
}
if err := writeConfiguredFile("Manifest", manifestFile, 0644); err != nil {
return err
}
wrapperFile, err := getWrapper(wrapperPath)
if err != nil {
return err
}
return writeConfiguredFile("Wrapper", wrapperFile, 0755)
}
func printConfiguredFile(preamble string, file configuredFile) {
fmt.Println(preamble)
fmt.Printf("\npath: %s\n", file.path)
fmt.Printf("\n### File content: ###\n%s\n###\n\n", file.content)
}
func writeConfiguredFile(name string, file configuredFile, perm os.FileMode) error {
dir := filepath.Dir(file.path)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
if err := ioutil.WriteFile(file.path, []byte(file.content), perm); err != nil {
return err
}
fmt.Printf("\n%s written to %s\n", name, file.path)
return nil
}
func getManifest(browser, wrapperPath, libpath string, global bool) (configuredFile, error) {
file := configuredFile{}
manifestPath, err := getManifestPath(browser, libpath, global)
if err != nil {
return file, err
}
file.path = manifestPath
file.content, err = getManifestContent(browser, wrapperPath)
return file, err
}
func getWrapper(wrapperPath string) (configuredFile, error) {
file := configuredFile{path: path.Join(wrapperPath, wrapperName)}
gopassPath, err := getGopassPath()
if err != nil {
return file, err
}
file.content = getWrapperContent(gopassPath)
return file, nil
}
func getManifestPath(browser, libpath string, globalInstall bool) (string, error) {
location, err := getLocation(browser, libpath, globalInstall)
if err != nil {
return "", err
}
expanded, err := homedir.Expand(location)
if err != nil {
return "", err
}
return fmt.Sprintf(expanded, name), nil
}
func getGopassPath() (string, error) {
return os.Executable()
}
func getWrapperContent(gopassPath string) string {
return fmt.Sprintf(wrapperTemplate, gopassPath)
}
func getManifestContent(browser, wrapperPath string) (string, error) {
var bytes []byte
var err error
if browser == "firefox" {
jsonManifest := firefoxManifest{}
jsonManifest.InitFields(path.Join(wrapperPath, wrapperName))
jsonManifest.AllowedExtensions = firefoxOrigins
bytes, err = json.MarshalIndent(jsonManifest, "", " ")
} else if browser == "chrome" || browser == "chromium" {
jsonManifest := chromeManifest{}
jsonManifest.InitFields(path.Join(wrapperPath, wrapperName))
jsonManifest.AllowedOrigins = chromeOrigins
bytes, err = json.MarshalIndent(jsonManifest, "", " ")
} else {
return "", fmt.Errorf("no manifest template for browser %s", browser)
}
if err != nil {
return "", err
}
return string(bytes), nil
}
func (m *manifestBase) InitFields(wrapperPath string) {
m.Name = name
m.Type = connectionType
m.Path = wrapperPath
m.Description = description
}