/
install.go
201 lines (194 loc) · 6.68 KB
/
install.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (C) 2021 Max Nikulin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"strings"
"github.com/maxnikulin/burl/pkg/burl_fileutil"
"github.com/maxnikulin/burl/pkg/version"
"github.com/maxnikulin/burl/pkg/webextensions"
)
type installFlagValues struct {
Name string
WrapperPath string
FirefoxManifest string
ChromeManifest string
Force bool
}
func createInstallFlags(flagset *flag.FlagSet) *installFlagValues {
v := installFlagValues{}
if flagset == nil {
flagset = flag.CommandLine
}
flagset.StringVar(&v.Name, "backend", "",
"Backend `NAME` to be specified in extension configuration")
flagset.StringVar(&v.FirefoxManifest, "manifest-firefox", "",
"Generate native application manifest in `FILE`."+
" See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_manifests"+
" and https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging#app_manifest"+
" for expected directory.")
flagset.StringVar(&v.ChromeManifest, "manifest-chrome", "",
"Generate native application manifest in `FILE`."+
" See https://developer.chrome.com/docs/apps/nativeMessaging/#native-messaging-host-location"+
" for expected directory.")
flagset.StringVar(&v.WrapperPath, "wrapper", "",
"Create wrapper `SCRIPT` that serves as configuration file")
flagset.BoolVar(&v.Force, "force", false,
"When generating wrapper or manifest, overwrite existing files")
return &v
}
// Returns true if generation of manifest or wrapper is requested.
func processInstallFlags(
installOptions *installFlagValues, backendOptions *BurlBackendArgs,
) (retval bool, err error) {
files := []string{installOptions.WrapperPath, installOptions.FirefoxManifest, installOptions.ChromeManifest}
nonEmpty := 0
toStdout := 0
for _, f := range files {
if f != "" {
nonEmpty++
}
if f == "-" {
toStdout++
}
}
if nonEmpty == 0 {
return false, nil
}
retval = true
if toStdout > 1 {
return retval, errors.New("Stdout is requested for more than one file")
}
if installOptions.WrapperPath == "-" &&
(installOptions.FirefoxManifest != "" ||
installOptions.ChromeManifest != "") {
return retval, errors.New("Manifest requires wrapper path but stdout is requested")
}
custom := backendOptions.Customized()
if installOptions.WrapperPath == "" && custom {
return retval, errors.New("-wrapper PATH is required due to custom options")
}
if err := backendOptions.FixPaths(); err != nil {
return retval, fmt.Errorf("make cwd independent args: %w", err)
}
manifestExe := ""
if installOptions.WrapperPath != "" && installOptions.WrapperPath != "-" {
if path, _, err := burl_fileutil.AsAbsFileName(
installOptions.WrapperPath, burl_fileutil.AbsFileNameOptions{},
); err != nil {
return retval, fmt.Errorf("%w: failed to get absolute wrapper path", err)
} else {
manifestExe = path
installOptions.WrapperPath = path
}
}
if installOptions.WrapperPath != "" || custom {
// Another way is to use os.SameFile however it may be more
// reasonable to overwrite a hardlink.
if installOptions.WrapperPath != backendOptions.Exe {
exe, err := burl_fileutil.EscapeShellArg(backendOptions.Exe)
if err != nil {
return retval, err
}
escaped_args, err := backendOptions.AsShellCommand()
if err != nil {
return retval, err
}
template := `#!/bin/sh -eu
BURL_BACKEND=%s
# Execution aborted on not found file even without "-e" option.
# "trap HANDLER EXIT" is ignored as well. So a test is required.
[ -x "$BURL_BACKEND" ] && \
exec "$BURL_BACKEND" %s
err="{\"error\": \"$(printf '%%s' "$BURL_BACKEND" | tr '\\"' "/'"): file not found or not executable\"}"
# platform endian-dependent hack
/usr/bin/printf "\\x$(printf '%%x' "${#err}")\\x00\\x00\\x00"
printf '%%s' "$err"
`
content := fmt.Sprintf(template, exe, strings.Join(escaped_args, " "))
err = burl_fileutil.WriteFile(installOptions.WrapperPath, []byte(content), 0755, installOptions.Force)
if err != nil {
return retval, fmt.Errorf("%w: failed to write wrapper", err)
}
} else if custom {
return retval, fmt.Errorf("-wrapper file must be distinct from backend path due to custom options")
}
}
if manifestExe == "" {
manifestExe = backendOptions.Exe
}
if installOptions.FirefoxManifest != "" {
absNameParams := burl_fileutil.AbsFileNameOptions{
Name: installOptions.Name,
DefaultName: version.AppName,
Ext: ".json",
}
path, name, err := burl_fileutil.AsAbsFileName(installOptions.FirefoxManifest, absNameParams)
if err != nil {
return retval, fmt.Errorf("Firefox manifest path: %w", err)
}
m := webextensions.Manifest{
Name: name,
Path: manifestExe,
ManifestPath: path,
AllowedExtensions: []string{version.FirefoxExtension},
}
if err := writeManifest(&m, installOptions.Force); err != nil {
return retval, fmt.Errorf("write Firefox manifest: %w", err)
}
}
if installOptions.ChromeManifest != "" {
absNameParams := burl_fileutil.AbsFileNameOptions{
Name: installOptions.Name,
DefaultName: version.AppName,
Ext: ".json",
}
path, name, err := burl_fileutil.AsAbsFileName(installOptions.ChromeManifest, absNameParams)
if err != nil {
return retval, fmt.Errorf("Chrome manifest path: %w", err)
}
m := webextensions.Manifest{
Name: name,
Path: manifestExe,
ManifestPath: path,
AllowedOrigins: []string{
fmt.Sprintf("chrome-extension://%s/", version.ChromeExtension),
},
}
if err := writeManifest(&m, installOptions.Force); err != nil {
return retval, fmt.Errorf("write Chrome manifest: %w", err)
}
}
return retval, nil
}
func writeManifest(m *webextensions.Manifest, force bool) error {
m.Description = version.Description
if err := m.Init(); err != nil {
return err
}
content, err := json.MarshalIndent(m, "", " ")
content = append(content, '\n')
if err != nil {
return fmt.Errorf("manifest: %w", err)
}
err = burl_fileutil.WriteFile(m.ManifestPath, content, 0644, force)
if err != nil {
return fmt.Errorf("manifest: %w", err)
}
return nil
}