-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrab.go
194 lines (171 loc) · 4.02 KB
/
grab.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
)
func runGrabCmd(cmd *flag.FlagSet, args []string) error {
opts, err := parseGrabFlags(cmd, args)
if err != nil {
return err
}
err = grabTabs(opts)
if err != nil {
return err
}
return nil
}
type grabOptions struct {
*commonOptions
urlWriter *writeCloseRemover
template string
}
func parseGrabFlags(fs *flag.FlagSet, args []string) (*grabOptions, error) {
attachCommonFlags(fs)
var (
urlFile = fs.String(
"file",
"",
"path for output file containing newline-delimited list of URLs",
)
quiet = fs.Bool(
"quiet",
false,
"disable console output",
)
template = fs.String(
"template",
setStringFlagDefault(defaultTemplate, envVarTemplate),
"output format specifying tab URL with {{.URL}} tab name with {{.Name}}",
)
)
defaultUsage := fs.Usage
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "`%s` %s\n\n", grabCmdName, grabCmdDescription)
defaultUsage()
}
err := fs.Parse(args)
if err != nil {
return nil, err
}
commonOpts, err := parseCommonOptions()
if err != nil {
return nil, err
}
builder := multiWriteCloseRemoverBuilder{}
if commonOpts.clipboard {
builder.add(&writeCloseRemover{
Writer: &clipboard{},
Closer: func() error { return nil },
Remover: func() error { return nil },
})
}
if *urlFile != "" {
f, err := os.Create(*urlFile)
if err != nil {
return nil, fmt.Errorf("failed to create file: %w", err)
}
builder.add(&writeCloseRemover{
Writer: f,
Closer: f.Close,
Remover: func() error { return os.Remove(f.Name()) },
})
}
if !*quiet {
builder.add(&writeCloseRemover{
Writer: os.Stdout,
Closer: func() error { return nil },
Remover: func() error { return nil },
})
}
urlWriter := builder.build()
opts := &grabOptions{
commonOptions: commonOpts,
urlWriter: urlWriter,
template: *template,
}
return opts, nil
}
func grabTabs(opts *grabOptions) error {
// Cleanup if exit due to error
writeCleanup := true
defer func() {
if writeCleanup {
_ = opts.urlWriter.Remove()
}
}()
// Buffers to capture stdout and stderr
var stdout, stderr bytes.Buffer
// Script to capture URL of tab i
tabScript := "tell application \"" + opts.browserApp.cmdName + "\" to get {URL, NAME} of tab %d of window 1"
for i := 0; i < opts.maxTabs; i++ {
iTabScript := fmt.Sprintf(tabScript, i+1)
// There is no intention that this implementation be secure so ignore the linter warning
cmd := exec.Command("osascript", "-e", iTabScript) // #nosec
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if opts.verbose {
log.Printf("executing: %s\n", cmd.String())
}
if err := cmd.Run(); err != nil {
// Check stderr for clean exit on end-of-tabs error
if stderr.Len() == 0 || isErrEndOfTabs(&stderr) {
if opts.verbose {
log.Printf("tab %d of window 1 not found, end of tabs", i+1)
}
break
}
// Return error if not end-of-tabs
return fmt.Errorf("%s\n%v\n", stderr.String(), err)
}
}
// Write output
defer opts.urlWriter.Close()
writer := bufio.NewWriter(opts.urlWriter)
writeF, err := buildTemplateWriteF(writer, fmt.Sprintf("%s%s", opts.prefix, opts.template))
if err != nil {
return fmt.Errorf("failed to construct writer template: %w", err)
}
for _, tab := range parseTabInfo(stdout) {
if tab.URL != "" || tab.Name != "" {
err := writeF(tab)
if err != nil {
return fmt.Errorf("failed to write output to buffer: %w", err)
}
}
}
err = writer.Flush()
if err != nil {
return fmt.Errorf("failed to flush buffer: %w", err)
}
writeCleanup = false
return nil
}
type tabInfo struct {
URL string
Name string
}
func parseTabInfo(raw bytes.Buffer) []*tabInfo {
tInfo := []*tabInfo{}
tabs := strings.Split(raw.String(), "\n")
for _, tab := range tabs {
if tab == "" {
continue
}
info := strings.SplitN(tab, ",", 2)
tabName := ""
if len(info) == 2 {
tabName = strings.TrimSpace(info[1])
}
tInfo = append(tInfo, &tabInfo{
URL: info[0],
Name: tabName,
})
}
return tInfo
}