booker 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/booker/cli.rb +23 -13
- data/lib/booker/config.rb +61 -9
- data/lib/booker/installer.rb +13 -4
- data/lib/booker/parsers.rb +45 -21
- data/lib/booker/tlds.txt +1436 -0
- data/lib/booker/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 775c1f7390fbe5b793703bba1be907504ed571adb2ce6bf97a67a6957dc0478a
|
|
4
|
+
data.tar.gz: 5f17c3c1348b41124626260004fd182ac8f90e2229da168942e95ed0059acb8a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dfe0cc75af9fd5c273ac460fc182148260ff8c3073df75865d8f852179677ccc94bea38890d97648f18411360518517fc8099181fd2aaeab6faab505e55d0d38
|
|
7
|
+
data.tar.gz: f1870b789f13c20a873f6fc3bd27e55fdb00e47cf29d1a756aa578d1a7266d53eeb39acb7247d2f17b426218149384887db152b60d1a9e451d7627ba1036acf9
|
data/lib/booker/cli.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
# parse booker's command line args and act on them
|
|
4
4
|
|
|
5
|
+
require "shellwords"
|
|
6
|
+
|
|
5
7
|
require_relative "output"
|
|
6
8
|
|
|
7
9
|
using Booker::Colors
|
|
@@ -13,10 +15,10 @@ module Booker
|
|
|
13
15
|
|
|
14
16
|
# a bookmark id is "<source index>_<browser id>". safari uses a uuid where
|
|
15
17
|
# chrome and firefox number theirs, so the second half is anything id-shaped
|
|
16
|
-
# rather than digits
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
BOOKMARK_ID = /\A
|
|
18
|
+
# rather than digits. a bare number is a search: `booker 2024` is looking
|
|
19
|
+
# for a year. completion inserts urls now, but ids are still first class -
|
|
20
|
+
# --bookmark takes them, the picker returns them
|
|
21
|
+
BOOKMARK_ID = /\A\d+_[a-z0-9-]+\z/i
|
|
20
22
|
|
|
21
23
|
def initialize(args)
|
|
22
24
|
parse args
|
|
@@ -37,7 +39,7 @@ module Booker
|
|
|
37
39
|
# every argument being a url means they all came from tab completion,
|
|
38
40
|
# which inserts one per bookmark - open the lot. one non-url word makes
|
|
39
41
|
# the whole line a search, so a phrase containing a domain still is one
|
|
40
|
-
if other_args.all? { |arg|
|
|
42
|
+
if other_args.all? { |arg| site?(arg) }
|
|
41
43
|
other_args.each do |site|
|
|
42
44
|
puts "opening website: ".grn + site
|
|
43
45
|
openweb(prep(site))
|
|
@@ -83,7 +85,9 @@ module Booker
|
|
|
83
85
|
case @mode
|
|
84
86
|
when :bookmark
|
|
85
87
|
pexit "Error: ".red + "booker --bookmark expects bookmark id" if args.empty?
|
|
86
|
-
|
|
88
|
+
# a bare number is an id from before ids carried their source, when
|
|
89
|
+
# there was only ever source 0
|
|
90
|
+
open_bookmark(args.map { |id| /\A\d+\z/.match?(id) ? "0_#{id}" : id })
|
|
87
91
|
when :install
|
|
88
92
|
installer.install(args.empty? ? %w[completion config bookmarks] : args)
|
|
89
93
|
when :search
|
|
@@ -98,7 +102,9 @@ module Booker
|
|
|
98
102
|
when :complete_raw
|
|
99
103
|
Bookmarks.new(args.join(" ")).autocomplete_raw
|
|
100
104
|
end
|
|
101
|
-
|
|
105
|
+
# ParseError rather than InvalidOption: `booker --list=x` is a
|
|
106
|
+
# NeedlessArgument, and every other subclass is just as much a usage error
|
|
107
|
+
rescue OptionParser::ParseError => e
|
|
102
108
|
pexit "Error: ".red + e.message
|
|
103
109
|
end
|
|
104
110
|
|
|
@@ -112,12 +118,14 @@ module Booker
|
|
|
112
118
|
end
|
|
113
119
|
|
|
114
120
|
def openweb(url)
|
|
115
|
-
#
|
|
116
|
-
#
|
|
117
|
-
|
|
121
|
+
# the url goes to the browser as its own argument, without a shell, so
|
|
122
|
+
# parentheses and ampersands in it mean nothing. the command itself is
|
|
123
|
+
# split with Shellwords, like :picker, so a configured `open -a Firefox`
|
|
124
|
+
# is a command and two flags rather than one program by that whole name
|
|
125
|
+
browser_cmd = Shellwords.split(browse)
|
|
118
126
|
|
|
119
127
|
# Redirect stdout/stderr to suppress GTK warnings
|
|
120
|
-
success = system(browser_cmd, url, out: File::NULL, err: File::NULL)
|
|
128
|
+
success = system(*browser_cmd, url, out: File::NULL, err: File::NULL)
|
|
121
129
|
|
|
122
130
|
unless success
|
|
123
131
|
# system reports a missing binary as nil rather than false, but the
|
|
@@ -146,8 +154,10 @@ module Booker
|
|
|
146
154
|
def open_search(term)
|
|
147
155
|
puts "searching ".grn + term
|
|
148
156
|
search = Config.default.searcher
|
|
149
|
-
|
|
150
|
-
|
|
157
|
+
# encoded, not just spaces to "+": a literal + reads back as a space, &
|
|
158
|
+
# ends the query and # starts a fragment, so `c++` searched for "c"
|
|
159
|
+
require "uri"
|
|
160
|
+
openweb(search + URI.encode_www_form_component(term))
|
|
151
161
|
end
|
|
152
162
|
|
|
153
163
|
# a term matching bookmarks is offered as a choice; one matching nothing is
|
data/lib/booker/config.rb
CHANGED
|
@@ -19,9 +19,13 @@ module Booker
|
|
|
19
19
|
def self.linux? = !(windows? || mac?)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
#
|
|
22
|
+
# the command that opens a link: the config's :browser if it names one,
|
|
23
|
+
# otherwise whatever opens a url on this platform
|
|
23
24
|
module Browser
|
|
24
25
|
def browse
|
|
26
|
+
configured = Config.default.browser
|
|
27
|
+
return configured if configured
|
|
28
|
+
|
|
25
29
|
if OS.windows?
|
|
26
30
|
# alternatively, start seems to work - probably check if powershell v cygwin?
|
|
27
31
|
'/cygdrive/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe '
|
|
@@ -39,12 +43,29 @@ module Booker
|
|
|
39
43
|
# read the rule from here rather than spelling it out twice and drifting
|
|
40
44
|
SCHEME = /[a-z][a-z0-9+.-]*:\/\//i
|
|
41
45
|
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
# (io|com|net|org|...) list sent .dev and .ai off to the search engine
|
|
46
|
+
# the shape of a website rather than a search term: an explicit scheme, or
|
|
47
|
+
# a bare host with a dot and an alphabetic tld, captured so #site? can check
|
|
48
|
+
# it is a real one
|
|
46
49
|
def domain
|
|
47
|
-
%r{\A(?:#{SCHEME}\S+|(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}(?::\d+)?(?:[/?#]\S*)?)\z}io
|
|
50
|
+
%r{\A(?:#{SCHEME}\S+|(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?<tld>[a-z]{2,})(?::\d+)?(?:[/?#]\S*)?)\z}io
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# should this argument be opened rather than searched for? anything with a
|
|
54
|
+
# scheme is a url. a bare host also has to end in a tld on IANA's list,
|
|
55
|
+
# all of them - completion inserts real bookmark urls, and a hand picked
|
|
56
|
+
# (io|com|net|org|...) once sent .dev and .ai to the search engine - but
|
|
57
|
+
# the shape alone said vue.js and node.js were websites
|
|
58
|
+
def site?(arg)
|
|
59
|
+
match = domain.match(arg) or return false
|
|
60
|
+
match[:tld].nil? || Browser.tlds.include?(match[:tld].downcase)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# IANA's list, refreshed with `just tlds`. read on first use rather than at
|
|
64
|
+
# load, since completion subshells never ask
|
|
65
|
+
def self.tlds
|
|
66
|
+
@tlds ||= File.readlines(File.join(__dir__, "tlds.txt"), chomp: true)
|
|
67
|
+
.reject { |line| line.start_with?("#") }
|
|
68
|
+
.to_set
|
|
48
69
|
end
|
|
49
70
|
|
|
50
71
|
# only a bare host needs a scheme invented for it. testing for "http" alone
|
|
@@ -57,6 +78,10 @@ module Booker
|
|
|
57
78
|
|
|
58
79
|
# configuration
|
|
59
80
|
class Config
|
|
81
|
+
# raised by #write rather than replacing a file that exists but would not
|
|
82
|
+
# parse: the defaults in memory are a fallback, not the user's settings
|
|
83
|
+
class Unreadable < StandardError; end
|
|
84
|
+
|
|
60
85
|
VALID = [:searcher, :bookmarks, :browser, :picker].freeze
|
|
61
86
|
HOME = ENV.fetch("HOME", "/usr/local/").freeze
|
|
62
87
|
YAMLCONF = (HOME + "/.booker.yml").freeze
|
|
@@ -79,15 +104,16 @@ module Booker
|
|
|
79
104
|
# the yaml file if there is one, and only work out the defaults when
|
|
80
105
|
# there is not: discovery walks every chrome profile directory to answer
|
|
81
106
|
# a question the config file has already answered
|
|
107
|
+
# no :browser here: #browse works it out per platform, and writing one
|
|
108
|
+
# down would pin this machine's answer into the file
|
|
82
109
|
@config = read(YAMLCONF) || {
|
|
83
|
-
browser: "open ",
|
|
84
110
|
searcher: "https://google.com/search?q=",
|
|
85
111
|
bookmarks: detect_default_bookmarks
|
|
86
112
|
}
|
|
87
113
|
|
|
88
114
|
# prune bad config keys
|
|
89
115
|
@config.each_key do |k|
|
|
90
|
-
if !VALID.include? k
|
|
116
|
+
if !VALID.include? k
|
|
91
117
|
warn "Failure:".red + " Bad key found in config file: #{k}"
|
|
92
118
|
exit 1
|
|
93
119
|
end
|
|
@@ -111,7 +137,15 @@ module Booker
|
|
|
111
137
|
|
|
112
138
|
def read(file)
|
|
113
139
|
# #write emits symbol keys, so they have to be permitted on the way back
|
|
114
|
-
YAML.safe_load_file(file, permitted_classes: [Symbol])
|
|
140
|
+
conf = YAML.safe_load_file(file, permitted_classes: [Symbol])
|
|
141
|
+
# an empty file is no config at all, which is fine to write over
|
|
142
|
+
return false if conf.nil?
|
|
143
|
+
raise Psych::Exception, "not a mapping" unless conf.is_a?(Hash)
|
|
144
|
+
|
|
145
|
+
# symbols throughout, whatever the file used: `searcher:` is how yaml is
|
|
146
|
+
# usually written, but the readers look up :searcher, so a config spelled
|
|
147
|
+
# the usual way passed validation and then read back as nil
|
|
148
|
+
conf.transform_keys { |k| k.to_s.to_sym }
|
|
115
149
|
rescue Errno::ENOENT
|
|
116
150
|
unless @quiet_missing
|
|
117
151
|
warn "Warning: ".yel +
|
|
@@ -123,6 +157,7 @@ module Booker
|
|
|
123
157
|
# every psych failure means the same thing here - an unusable config to
|
|
124
158
|
# fall back from rather than crash on. rescuing SyntaxError alone let
|
|
125
159
|
# anchors through as an AliasesNotEnabled backtrace under psych 4
|
|
160
|
+
@unreadable = true
|
|
126
161
|
warn "Warning: ".red +
|
|
127
162
|
"YAML configuration file could not be read. Using defaults."
|
|
128
163
|
false
|
|
@@ -130,6 +165,11 @@ module Booker
|
|
|
130
165
|
|
|
131
166
|
# used for creating and updating the default configuration
|
|
132
167
|
def write(k = nil, v = nil)
|
|
168
|
+
if @unreadable
|
|
169
|
+
raise Unreadable, "#{YAMLCONF} could not be read - fix or remove it, " \
|
|
170
|
+
"it was left as is"
|
|
171
|
+
end
|
|
172
|
+
|
|
133
173
|
@config[k] = v unless k.nil? # update users yaml config file
|
|
134
174
|
File.write(YAMLCONF, @config.to_yaml)
|
|
135
175
|
end
|
|
@@ -140,6 +180,18 @@ module Booker
|
|
|
140
180
|
|
|
141
181
|
def searcher = @config[:searcher]
|
|
142
182
|
|
|
183
|
+
# the command that opens links, flags and all. nil means "work it out from
|
|
184
|
+
# the platform" - see Browser#browse
|
|
185
|
+
def browser
|
|
186
|
+
configured = @config[:browser].to_s
|
|
187
|
+
# the built-in defaults once wrote "open " on every platform, so a linux
|
|
188
|
+
# or windows config made by --install holds the macOS command. it never
|
|
189
|
+
# worked there, and is not a choice anybody made
|
|
190
|
+
return nil if configured.strip.empty? || (configured.strip == "open" && !OS.mac?)
|
|
191
|
+
|
|
192
|
+
configured
|
|
193
|
+
end
|
|
194
|
+
|
|
143
195
|
# the finder command with its flags, split with Shellwords rather than
|
|
144
196
|
# handed to a shell. nil means "auto detect fzf", which is why #write leaves
|
|
145
197
|
# it out of the defaults: #initialize exits on keys it does not know, so a
|
data/lib/booker/installer.rb
CHANGED
|
@@ -270,8 +270,7 @@ module Booker
|
|
|
270
270
|
bms = Sources.discover
|
|
271
271
|
|
|
272
272
|
if bms.empty? # no bookmarks found
|
|
273
|
-
|
|
274
|
-
raise
|
|
273
|
+
raise "No browser bookmarks file could be found."
|
|
275
274
|
elsif bms.length == 1
|
|
276
275
|
# Auto-select if only one source found
|
|
277
276
|
selected = bms.first
|
|
@@ -288,9 +287,17 @@ module Booker
|
|
|
288
287
|
puts (i + 1).to_s.grn + " - " + label + " " + path
|
|
289
288
|
end
|
|
290
289
|
|
|
291
|
-
|
|
290
|
+
# $stdin, not Kernel#gets: that reads ARGF, which opens the install
|
|
291
|
+
# targets still left in ARGV (`bookmarks`) as files
|
|
292
|
+
input = $stdin.gets
|
|
292
293
|
raise "No input provided" if input.nil?
|
|
293
|
-
|
|
294
|
+
|
|
295
|
+
# to_i alone read any typo as 0 - all sources - and a negative number
|
|
296
|
+
# as an index from the end of the list
|
|
297
|
+
selection = Integer(input.strip, 10, exception: false)
|
|
298
|
+
unless selection&.between?(0, bms.length)
|
|
299
|
+
raise "Invalid selection: #{input.strip.inspect}"
|
|
300
|
+
end
|
|
294
301
|
|
|
295
302
|
if selection == 0
|
|
296
303
|
# User selected "ALL" - save array of all paths
|
|
@@ -325,6 +332,8 @@ module Booker
|
|
|
325
332
|
def install_config
|
|
326
333
|
config.write
|
|
327
334
|
puts "Success: ".grn + "example config file written to ~/.booker"
|
|
335
|
+
rescue Config::Unreadable => e
|
|
336
|
+
pexit "Failure: ".red + e.message
|
|
328
337
|
rescue
|
|
329
338
|
pexit "Failure: ".red + "could not write example config file to ~/.booker"
|
|
330
339
|
end
|
data/lib/booker/parsers.rb
CHANGED
|
@@ -50,22 +50,30 @@ module Booker
|
|
|
50
50
|
class Chrome < Base
|
|
51
51
|
def parse
|
|
52
52
|
begin
|
|
53
|
-
|
|
54
|
-
@chrome_bookmarks = local_bookmarks["roots"]["bookmark_bar"]["children"]
|
|
53
|
+
roots = JSON.parse(File.read(@file_path))["roots"].select { |_, r| r.is_a?(Hash) }
|
|
55
54
|
rescue
|
|
56
55
|
warn "Warning: ".yel + "Bookmarks file not found or invalid."
|
|
57
56
|
warn "Suggest: ".grn + "booker --install bookmarks"
|
|
58
57
|
return
|
|
59
58
|
end
|
|
60
59
|
|
|
61
|
-
|
|
60
|
+
# every root, not just the bar: "other bookmarks" is where chrome files
|
|
61
|
+
# anything saved without picking a folder, and "mobile bookmarks" is
|
|
62
|
+
# what sync brings over from a phone. the bar keeps the bare "|" it
|
|
63
|
+
# always had; the others are folders like any other, named by chrome
|
|
64
|
+
top = Folder.new([], "|")
|
|
65
|
+
roots.each do |key, root|
|
|
66
|
+
if key == "bookmark_bar"
|
|
67
|
+
parse_recursive(Folder.new(root["children"], "|"))
|
|
68
|
+
else
|
|
69
|
+
parse_folder(top, root)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
62
72
|
end
|
|
63
73
|
|
|
64
74
|
private
|
|
65
75
|
|
|
66
|
-
def parse_recursive(root
|
|
67
|
-
root = Folder.new(@chrome_bookmarks, "|") if root.nil?
|
|
68
|
-
|
|
76
|
+
def parse_recursive(root)
|
|
69
77
|
root.json.each { |x| parse_link(root.title, x) }
|
|
70
78
|
root.json.each { |x| parse_folder(root, x) }
|
|
71
79
|
end
|
|
@@ -118,15 +126,20 @@ module Booker
|
|
|
118
126
|
return
|
|
119
127
|
end
|
|
120
128
|
|
|
121
|
-
#
|
|
122
|
-
#
|
|
123
|
-
#
|
|
129
|
+
# a running firefox holds the database locked, so read a copy. it also
|
|
130
|
+
# keeps it in WAL mode: recent bookmarks live in places.sqlite-wal until
|
|
131
|
+
# a checkpoint, and sqlite only finds a log named after the database
|
|
132
|
+
# beside it. so the copy is all three files under the same basename, in
|
|
133
|
+
# a directory mktmpdir removes when the block ends
|
|
124
134
|
if firefox_running?
|
|
125
|
-
require "
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
FileUtils.cp(@file_path,
|
|
129
|
-
|
|
135
|
+
require "tmpdir"
|
|
136
|
+
Dir.mktmpdir("booker") do |dir|
|
|
137
|
+
copy = File.join(dir, "places.sqlite")
|
|
138
|
+
FileUtils.cp(@file_path, copy)
|
|
139
|
+
%w[-wal -shm].each do |log|
|
|
140
|
+
FileUtils.cp(@file_path + log, copy + log) if File.exist?(@file_path + log)
|
|
141
|
+
end
|
|
142
|
+
query_places(copy)
|
|
130
143
|
end
|
|
131
144
|
else
|
|
132
145
|
query_places(@file_path)
|
|
@@ -143,15 +156,19 @@ module Booker
|
|
|
143
156
|
# Recursive CTE query to build folder paths
|
|
144
157
|
query = <<-SQL
|
|
145
158
|
WITH RECURSIVE bookmark_tree(id, parent, title, url, path) AS (
|
|
146
|
-
-- Start with
|
|
159
|
+
-- Start with the children of every user-facing root: the menu is
|
|
160
|
+
-- where firefox saves a bookmark by default, so the toolbar alone
|
|
161
|
+
-- missed most of them. toolbar paths stay unprefixed, as before
|
|
147
162
|
SELECT b.id, b.parent, b.title, p.url,
|
|
148
|
-
CAST(
|
|
163
|
+
CAST(CASE r.guid
|
|
164
|
+
WHEN 'toolbar_____' THEN b.title
|
|
165
|
+
ELSE COALESCE(r.title, '') || '/' || b.title
|
|
166
|
+
END as TEXT) as path
|
|
149
167
|
FROM moz_bookmarks b
|
|
168
|
+
INNER JOIN moz_bookmarks r ON b.parent = r.id
|
|
150
169
|
LEFT JOIN moz_places p ON b.fk = p.id
|
|
151
|
-
WHERE
|
|
152
|
-
|
|
153
|
-
WHERE guid = 'toolbar_____'
|
|
154
|
-
)
|
|
170
|
+
WHERE r.guid IN
|
|
171
|
+
('toolbar_____', 'menu________', 'unfiled_____', 'mobile______')
|
|
155
172
|
|
|
156
173
|
UNION ALL
|
|
157
174
|
|
|
@@ -217,7 +234,14 @@ module Booker
|
|
|
217
234
|
# sniff the head rather than reading the whole file: on the binary path
|
|
218
235
|
# the contents are thrown away and plutil reads it again from disk, and
|
|
219
236
|
# a real Bookmarks.plist grows with the user's bookmarks
|
|
220
|
-
head =
|
|
237
|
+
head = begin
|
|
238
|
+
File.binread(path, 512).to_s
|
|
239
|
+
rescue Errno::EPERM, Errno::EACCES
|
|
240
|
+
# macOS privacy protection, when this process lacks Full Disk Access.
|
|
241
|
+
# the file is binary on a mac anyway, so leave it to plutil - which
|
|
242
|
+
# may have been granted access on its own (--install safari, [B])
|
|
243
|
+
""
|
|
244
|
+
end
|
|
221
245
|
|
|
222
246
|
# Already an XML plist (fixtures, non-macOS). Binary plists need plutil
|
|
223
247
|
# (macOS-only); the JSON converter rejects <data>/<date> fields, so xml1.
|