lexdrill 0.19.0 → 0.24.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c04d80068319a1be565d64c8325ce808f242d7a71609f34796b348cc04b0571
4
- data.tar.gz: 2b06d6b6d48cc3556c77524bce72c77e18404aceadd802cb3ae40134a29cfc3d
3
+ metadata.gz: 15b139f554503da2fe92b647d869ffa88bdbe8a7d73e3ce9af8a757b93699013
4
+ data.tar.gz: 4c9e554c485de8f52a503f64a400f95bb1e6f5d2bb6e717d23ea1245f49b3b75
5
5
  SHA512:
6
- metadata.gz: c15dac65d6521acd98fb4a81f016466882d4397587a9de255544364cbcb0a9bc535e399c91dd7979ea4fadc5206ca86c4c2f04db62898baa298d2a82a24f908d
7
- data.tar.gz: b8f7dd159de9b89f81cafd9a73d50d2e7db6541ed3fcd0028f4dbee6b344301f085e3be7f7c78a669bb1494845afc65eaba886f17a164355dd99521a9a6d4c6b
6
+ metadata.gz: 6482d9523500b1b71634c4d11b104dc4612cbe1711f98a0585ff9cbfc80fc9f7bfbb8851eb74824de6e60d081d0cc5e1a912c854a91e3100751b3cb1411cecc0
7
+ data.tar.gz: 72ad7861817b7f33fefbed9fd723ad1c8f9e57a5af7af28f0320da218b6ee5e41f98c9f6842fc1410b740f1899658a43b0d27dceb520c916244da8281317cdb3
data/README.md CHANGED
@@ -176,31 +176,36 @@ the default, so it's also the way to undo `random`).
176
176
 
177
177
  ### Google Sheets export
178
178
 
179
- `drill export <sheet-name>` mirrors your word list (text only, no stats)
180
- into a tab of a Google Sheet you own; `drill import <sheet-name>` reads it
181
- back. There are two ways to authenticate — pick whichever you prefer:
182
-
183
- - **`drill remote <url>`** a **service account** you create yourself. No
184
- interactive sign-in, ever; you share the spreadsheet with the service
179
+ lexdrill can mirror your word list to a tab in a Google Sheet, and pull it
180
+ back. Three independent layers, each a one-time setup:
181
+
182
+ 1. **Auth mode** (`drill remote` / `drill oauth`, no arguments) — *how*
183
+ you authenticate to Google. A single global choice.
184
+ 2. **Workbooks** (`drill wb ...`) *which* spreadsheets lexdrill knows
185
+ about, and which one is currently active. You can add as many as you
186
+ like.
187
+ 3. **Sheets** (`drill sh ...`) — *which tab*, within the active workbook,
188
+ is currently active.
189
+
190
+ Once all three are set, `drill push`/`drill pull` (no arguments) act on
191
+ whichever workbook + tab are currently active — and `drill add <text>`
192
+ automatically pushes the updated list too, so your local list and the
193
+ sheet stay in sync as you go.
194
+
195
+ #### Step 1: choose an auth mode
196
+
197
+ - **`drill remote`** — a **service account** you create yourself. No
198
+ interactive sign-in, ever; you share each spreadsheet with the service
185
199
  account's email address once, and `drill` uses a local private key file
186
200
  to sign its own requests.
187
- - **`drill oauth <url>`** — your **personal Google login**, via a one-time
201
+ - **`drill oauth`** — your **personal Google login**, via a one-time
188
202
  interactive OAuth approval. No GCP service account needed, but you (or
189
203
  anyone using the gem) has to click through a sign-in flow once.
190
204
 
191
- If both are configured, whichever you set **more recently** wins running
192
- `drill oauth <url>` after `drill remote <url>` switches `export`/`import`
193
- over to the personal-login flow, and vice versa.
194
-
195
- Either way, `export` always **overwrites** the named tab (creating it
196
- first if it doesn't exist yet) with the current word list, one phrase per
197
- row, so it stays an exact mirror even if the list shrinks. `import` is the
198
- reverse: it reads column A of the tab (ignoring any other columns, like an
199
- old export's show counts) and **replaces** your local `~/.drill/words` —
200
- useful for editing the list in Sheets and pulling changes back down, or
201
- seeding a fresh machine from an existing sheet.
205
+ Whichever you pick applies to every workbook it's one global setting,
206
+ independent of which spreadsheet is active.
202
207
 
203
- #### Option A: service account (`drill remote`)
208
+ ##### Option A: service account (`drill remote`)
204
209
 
205
210
  No embedded secret, no interactive consent screen — but it does require a
206
211
  one-time Google Cloud Console setup, and the resulting private key file is
@@ -211,7 +216,7 @@ a real secret you must keep local.
211
216
  **Google Sheets API** is enabled (**APIs & Services → Library**).
212
217
  2. **IAM & Admin → Service Accounts → "+ CREATE SERVICE ACCOUNT"**. Name it
213
218
  anything (e.g. `lexdrill-export`); skip granting it any project-level
214
- roles — it doesn't need any, since access comes from sharing the
219
+ roles — it doesn't need any, since access comes from sharing each
215
220
  document directly.
216
221
  3. Click into the new service account → **Keys → Add Key → Create new key
217
222
  → JSON → Create**. This downloads a `.json` file — **this is a real
@@ -219,22 +224,21 @@ a real secret you must keep local.
219
224
  share it with anyone.
220
225
  4. Note the service account's email (looks like
221
226
  `name@your-project-id.iam.gserviceaccount.com` — also the `client_email`
222
- field in the downloaded JSON).
223
- 5. Open your target Google Sheet → **Share** → paste that email → grant
224
- **Editor** access → uncheck "Notify people" → Share.
225
- 6. Save the downloaded key file to `~/.drill/gcp-service-account.json` on
227
+ field in the downloaded JSON). Share each spreadsheet you want lexdrill
228
+ to use with this email (**Share** → paste it → grant **Editor** access
229
+ → uncheck "Notify people" → Share).
230
+ 5. Save the downloaded key file to `~/.drill/gcp-service-account.json` on
226
231
  your machine (`drill` reads it from that fixed path; it's never embedded
227
232
  in the gem or committed anywhere).
228
233
 
229
234
  ```bash
230
- drill remote 'https://docs.google.com/spreadsheets/d/1opBP4APL5SUvepm9qwjIYRNtDZdoY1Ee87F5PWdxaMg/edit?usp=sharing'
231
- drill export Sheet1
235
+ drill remote
232
236
  ```
233
237
 
234
238
  No sign-in prompt — every call authenticates silently by signing a fresh,
235
239
  short-lived JWT with the local key file.
236
240
 
237
- #### Option B: personal login (`drill oauth`)
241
+ ##### Option B: personal login (`drill oauth`)
238
242
 
239
243
  Uses the OAuth 2.0 **Device Authorization Grant** ("visit this URL, enter
240
244
  this code," the same style of flow `gcloud auth login` uses) — you approve
@@ -258,21 +262,69 @@ personal refresh token, generated only after you interactively approve.
258
262
  URI needed). Copy the generated Client ID and Client secret into
259
263
  `CLIENT_ID`/`CLIENT_SECRET` in `lib/lexdrill/google_auth.rb`.
260
264
  4. "Testing" publishing status has historically imposed a 7-day
261
- refresh-token expiry for some scopes. If `drill export` starts asking
265
+ refresh-token expiry for some scopes. If `drill push` starts asking
262
266
  you to re-authorize every week, switch the consent screen to **"In
263
267
  production"** — for the `spreadsheets` scope (not a "restricted" scope)
264
268
  this just adds an "unverified app" click-through on first consent, no
265
269
  Google review required.
266
270
 
267
271
  ```bash
268
- drill oauth 'https://docs.google.com/spreadsheets/d/1opBP4APL5SUvepm9qwjIYRNtDZdoY1Ee87F5PWdxaMg/edit?usp=sharing'
269
- drill export Sheet1
272
+ drill oauth
273
+ ```
274
+
275
+ The first call that needs a token prints a URL and a short code — visit
276
+ it, sign in with the Google account that has edit access to your
277
+ spreadsheets, and approve. Every call after that is silent (the cached
278
+ token refreshes itself automatically).
279
+
280
+ #### Step 2: add and switch between workbooks
281
+
282
+ ```bash
283
+ drill wb add 'https://docs.google.com/spreadsheets/d/1opBP4APL5SUvepm9qwjIYRNtDZdoY1Ee87F5PWdxaMg/edit'
284
+ drill wb
285
+ ```
286
+
287
+ `drill wb add <url>` fetches the spreadsheet's own title via the API and
288
+ uses that as its name — no need to type one. The very first workbook you
289
+ add becomes active automatically; adding more doesn't switch you away
290
+ from whichever is currently active. `drill wb` (or `drill wb index`, same
291
+ thing) lists every known workbook, marking the active one with the drill
292
+ sign (⟳); `drill wb use <name>` switches; `drill wb remove <name>` forgets
293
+ one.
294
+
295
+ #### Step 3: add and switch between tabs
296
+
297
+ ```bash
298
+ drill sh
299
+ drill sh use Sheet1
300
+ ```
301
+
302
+ `drill sh` (or `drill sh index`) lists the tabs in the active workbook,
303
+ marking the active one with the drill sign (⟳). `drill sh use <name>`
304
+ switches to a tab **and immediately pulls it** — replacing your local
305
+ word list with that tab's column A. `drill sh add <name>` creates a new,
306
+ empty tab (and makes it active if no tab was selected yet); `drill sh
307
+ remove <name>` deletes one — this is
308
+ real and permanent, so double-check the name.
309
+
310
+ #### Staying in sync
311
+
312
+ Once a workbook and tab are both active:
313
+
314
+ ```bash
315
+ drill push # overwrite the active tab with your current word list
316
+ drill pull # replace your word list with the active tab's column A
317
+ drill add some new phrase # appends locally, then pushes automatically
318
+ drill open # opens the active tab in your browser
270
319
  ```
271
320
 
272
- The first `export`/`import` prints a URL and a short code visit it, sign
273
- in with the Google account that has edit access to that spreadsheet, and
274
- approve. Every call after that is silent (the cached token refreshes
275
- itself automatically).
321
+ `push` always **overwrites** the active tab with the current word list,
322
+ one phrase per row, so it stays an exact mirror even if the list shrinks.
323
+ `pull` is the reverse: it reads column A (ignoring any other columns,
324
+ like an old push's show counts) and **replaces** your local
325
+ `~/.drill/words`. `drill add` never fails because of a sync problem —
326
+ if the push fails (no network, tab renamed, etc.) it just warns on
327
+ stderr; the word is still added locally either way.
276
328
 
277
329
  ### Commands
278
330
 
@@ -280,19 +332,20 @@ itself automatically).
280
332
  |---|---|
281
333
  | `drill next` | Print the current word and advance |
282
334
  | `drill start` / `drill stop` | Pause/resume the automatic per-prompt hook (doesn't affect manual `next`) |
283
- | `drill inspect` | Show the active config directory, word count, counter value, toggle, beat, rand, and color state |
335
+ | `drill inspect` | Show the active config directory, word count, counter value, toggle, beat, rand, color, auth mode, workbook, and sheet state |
284
336
  | `drill hook zsh\|bash` | Print the shell integration snippet (used above) |
285
337
  | `drill beat <2-8> <repetitions>` / `drill beat none` | Set or disable the rhythm |
286
338
  | `drill polka\|waltz\|rock\|jazz\|jiga\|balkan\|samba <repetitions>` | Shorthand for a fixed loop size (see table above) |
287
339
  | `drill color random\|default` | Color each shown word randomly, or by its show count (`default`) |
288
- | `drill add <text>` | Append a new item to the end of the list |
340
+ | `drill add <text>` | Append a new item to the end of the list; also pushes to the active tab, if one is selected |
289
341
  | `drill list` | Show how many times each item has been shown, numbered |
290
- | `drill open` | Open the list file in `$EDITOR`/`$VISUAL` (falls back to `vi`) |
342
+ | `drill edit` | Open the list file in `$EDITOR`/`$VISUAL` (falls back to `vi`) |
291
343
  | `drill stats` | Print all items as `<count>\t<phrase>` (tab-separated), sorted by show count, highest first |
292
344
  | `drill rand <n>` | `drill next` shows a word ~1-in-`n` times (`n=1` is every time, the default) |
293
345
  | `drill go <number>` | Jump so the next `drill next` shows item `<number>` (1-based, see `drill list`) — prints nothing itself; refuses a graduated item; has no effect while `drill beat rand` is active, since that mode ignores the counter entirely |
294
- | `drill remote <url>` | Set the Google Sheet used by the service account flow (global, parses the spreadsheet id out of a normal share URL); whichever of `drill remote`/`drill oauth` was set more recently wins |
295
- | `drill oauth <url>` | Set the Google Sheet used by the OAuth (personal-login) flow (global, parses the spreadsheet id out of a normal share URL); whichever of `drill remote`/`drill oauth` was set more recently wins |
296
- | `drill sheet` | Print a link to whichever spreadsheet (`drill remote`/`drill oauth`) is currently active |
297
- | `drill export <sheet-name>` | Export the word list text to the named tab (created if it doesn't exist), overwriting it; uses whichever of `drill remote`/`drill oauth` was configured most recently (first OAuth use triggers a one-time Google device-flow sign-in) |
298
- | `drill import <sheet-name>` | Replace the local word list with column A of the named tab |
346
+ | `drill remote` / `drill oauth` | Choose the Google Sheets auth mode: local service account key, or personal OAuth login (global, applies to every workbook) |
347
+ | `drill wb` / `wb index` / `add <url>` / `remove <name>` / `use <name>` | List (bare `drill wb` is an alias for `wb index`)/add/forget/switch known workbooks; `add` names the workbook after the spreadsheet's own title; the active one is marked with the drill sign |
348
+ | `drill sh` / `sh index` / `add <name>` / `remove <name>` / `use <name>` | List (bare `drill sh` is an alias for `sh index`)/create/delete/switch tabs in the active workbook; `use` also pulls the tab's contents; the active one is marked with the drill sign |
349
+ | `drill open` | Open the active tab in your default browser |
350
+ | `drill push` | Overwrite the active tab with the current word list text |
351
+ | `drill pull` | Replace the local word list with column A of the active tab |
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Global "how to authenticate to Google Sheets" choice: "remote" (service
4
+ # account, no interactive sign-in) or "oauth" (personal Google login).
5
+ # Independent of which workbook is active — see Lexdrill::Workbooks for
6
+ # that. Set via `drill remote` / `drill oauth` (no arguments).
7
+ module Lexdrill::AuthMode
8
+ PATH = Lexdrill::Config.path("auth-mode")
9
+ REMOTE = "remote"
10
+ OAUTH = "oauth"
11
+ VALID = [REMOTE, OAUTH].freeze
12
+
13
+ def self.set(mode)
14
+ File.write(PATH, mode)
15
+ end
16
+
17
+ def self.current
18
+ return nil unless File.exist?(PATH)
19
+
20
+ value = File.read(PATH).strip
21
+ VALID.include?(value) ? value : nil
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rbconfig"
4
+
5
+ # Opens a URL in the system's default browser, cross-platform, for
6
+ # `drill open`.
7
+ module Lexdrill::Browser
8
+ def self.open(url)
9
+ system(*open_command(url), out: File::NULL, err: File::NULL)
10
+ end
11
+
12
+ def self.open_command(url)
13
+ case RbConfig::CONFIG["host_os"]
14
+ when /darwin/ then ["open", url]
15
+ when /mswin|mingw|cygwin/ then ["cmd", "/c", "start", "", url]
16
+ else ["xdg-open", url]
17
+ end
18
+ end
19
+ private_class_method :open_command
20
+ end
data/lib/lexdrill/cli.rb CHANGED
@@ -14,15 +14,17 @@ class Lexdrill::CLI
14
14
  run_color: %w[color],
15
15
  run_add: %w[add],
16
16
  run_list: %w[list],
17
- run_open: %w[open],
17
+ run_edit: %w[edit],
18
18
  run_stats: %w[stats],
19
19
  run_rand: %w[rand],
20
20
  run_go: %w[go],
21
21
  run_remote: %w[remote],
22
22
  run_oauth: %w[oauth],
23
- run_sheet: %w[sheet],
24
- run_export: %w[export],
25
- run_import: %w[import]
23
+ run_wb: %w[wb],
24
+ run_sh: %w[sh],
25
+ run_open: %w[open],
26
+ run_push: %w[push],
27
+ run_pull: %w[pull]
26
28
  }.freeze
27
29
 
28
30
  def self.start(argv = ARGV)
@@ -68,20 +70,28 @@ class Lexdrill::CLI
68
70
  drill polka|waltz|rock|jazz|jiga|balkan|samba [repetitions]
69
71
  Shorthand for a fixed loop size (2 through 8, in order)
70
72
  drill color random|default Color each word randomly, or by its show count (default)
71
- drill add <text> Append a new item to the end of the list
73
+ drill add <text> Append a new item to the list (and push, if a sheet is selected)
72
74
  drill list Show how many times each item has been shown
73
- drill open Open the list file in $EDITOR/$VISUAL (falls back to vi)
75
+ drill edit Open the list file in $EDITOR/$VISUAL (falls back to vi)
74
76
  drill stats Print items as <count>\t<phrase>, tab-separated, highest count first
75
77
  drill rand <n> drill next shows a word ~1-in-n times (n=1 is every time)
76
78
  drill go <number> Jump so the next `next` shows item <number> (1-based, see drill list)
77
- drill remote <url> Set the Google Sheet used by a local service account key
79
+ drill remote Use a local service account key for Google Sheets auth
78
80
  (~/.drill/gcp-service-account.json) — no interactive sign-in
79
- drill oauth <url> Set the Google Sheet used by the OAuth (personal-login) flow
80
- drill sheet Print a link to the currently active spreadsheet
81
- drill export <sheet-name> Export the word list text to the given tab (overwrites its
82
- contents); uses whichever of drill remote/drill oauth was set
83
- more recently (oauth opens a one-time sign-in on first use)
84
- drill import <sheet-name> Replace the local word list with column A of the given tab
81
+ drill oauth Use your personal Google login for Google Sheets auth
82
+ drill wb Alias for drill wb index
83
+ drill wb index List known workbooks (name and URL)
84
+ drill wb add <url> Add a workbook by URL (named after its own title)
85
+ drill wb remove <name> Forget a workbook
86
+ drill wb use <name> Switch the active workbook
87
+ drill sh Alias for drill sh index
88
+ drill sh index List the tabs in the active workbook
89
+ drill sh add <name> Create a new tab
90
+ drill sh remove <name> Delete a tab
91
+ drill sh use <name> Switch the active tab (pulls its contents)
92
+ drill open Open the active tab in your browser
93
+ drill push Push the word list text to the active tab (overwrites it)
94
+ drill pull Replace the local word list with the active tab's column A
85
95
  HELP
86
96
  0
87
97
  end
@@ -214,15 +224,34 @@ class Lexdrill::CLI
214
224
  1
215
225
  end
216
226
 
227
+ # Appends locally, then opportunistically pushes the updated list to the
228
+ # active tab if one is selected — a failed push is reported but doesn't
229
+ # change the exit code, since the local add always succeeds regardless.
217
230
  def run_add
218
231
  text = argv[1..].join(" ")
219
232
  return print_add_usage if text.empty?
220
233
 
221
234
  File.open(Lexdrill::WordList::PATH, "a", encoding: "UTF-8") { |file| file.puts(text) }
222
235
  puts "added: #{text}"
236
+ push_after_add
223
237
  0
224
238
  end
225
239
 
240
+ def push_after_add
241
+ spreadsheet_id = Lexdrill::Workbooks.current_id
242
+ sheet_name = Lexdrill::Workbooks.current_sheet
243
+ return unless spreadsheet_id && sheet_name
244
+
245
+ token = current_token
246
+ return unless token
247
+
248
+ Lexdrill::WordList.instance_variable_set(:@words, nil)
249
+ perform_push(spreadsheet_id, sheet_name, token)
250
+ rescue Lexdrill::GoogleAuth::AuthError, Lexdrill::ServiceAccountAuth::AuthError,
251
+ Lexdrill::SheetsClient::ApiError, Lexdrill::HTTPClient::NetworkError => error
252
+ warn "drill: added locally, but couldn't push to #{sheet_name.inspect}: #{error.message}"
253
+ end
254
+
226
255
  def print_add_usage
227
256
  warn "usage: drill add <text>"
228
257
  1
@@ -237,7 +266,7 @@ class Lexdrill::CLI
237
266
  0
238
267
  end
239
268
 
240
- def run_open
269
+ def run_edit
241
270
  editor_cmd = (ENV["VISUAL"] || ENV["EDITOR"] || "vi").split
242
271
  system(*editor_cmd, Lexdrill::WordList::PATH) ? 0 : 1
243
272
  end
@@ -297,174 +326,393 @@ class Lexdrill::CLI
297
326
  end
298
327
 
299
328
  def run_remote
300
- url = argv[1]
301
- return print_remote_usage unless url
329
+ Lexdrill::AuthMode.set(Lexdrill::AuthMode::REMOTE)
330
+ puts "auth mode set: remote (service account)"
331
+ 0
332
+ end
302
333
 
303
- Lexdrill::Remote.set(url)
304
- puts "remote spreadsheet set: #{Lexdrill::Remote.spreadsheet_id}"
334
+ def run_oauth
335
+ Lexdrill::AuthMode.set(Lexdrill::AuthMode::OAUTH)
336
+ puts "auth mode set: oauth (personal login)"
305
337
  0
306
- rescue ArgumentError
307
- print_invalid_remote_url(url)
308
338
  end
309
339
 
310
- def print_remote_usage
311
- warn "usage: drill remote <google-sheets-url>"
340
+ def current_token
341
+ case Lexdrill::AuthMode.current
342
+ when Lexdrill::AuthMode::REMOTE then Lexdrill::ServiceAccountAuth.fetch_token!
343
+ when Lexdrill::AuthMode::OAUTH then Lexdrill::GoogleAuth.ensure_token!
344
+ end
345
+ end
346
+
347
+ def print_no_auth_mode
348
+ warn "drill: no auth mode configured; run `drill remote` (service account) or `drill oauth` (personal login) first"
312
349
  1
313
350
  end
314
351
 
315
- def print_invalid_remote_url(url)
316
- warn "drill: could not find a spreadsheet id in #{url.inspect}"
352
+ def run_wb
353
+ case argv[1]
354
+ when nil, "index" then run_wb_index
355
+ when "add" then run_wb_add
356
+ when "remove" then run_wb_remove
357
+ when "use" then run_wb_use
358
+ else print_wb_usage
359
+ end
360
+ end
361
+
362
+ def print_wb_usage
363
+ warn "usage: drill wb index|add <url>|remove <name>|use <name>"
317
364
  1
318
365
  end
319
366
 
320
- def run_oauth
321
- url = argv[1]
322
- return print_oauth_usage unless url
367
+ def run_wb_index
368
+ names = Lexdrill::Workbooks.names
369
+ return print_no_workbooks if names.empty?
323
370
 
324
- Lexdrill::OauthRemote.set(url)
325
- puts "oauth spreadsheet set: #{Lexdrill::OauthRemote.spreadsheet_id}"
371
+ current = Lexdrill::Workbooks.current_name
372
+ names.each { |name| puts "#{current_marker(name == current)} #{name} #{Lexdrill::Workbooks.url_for(name)}" }
326
373
  0
327
- rescue ArgumentError
328
- print_invalid_oauth_url(url)
329
374
  end
330
375
 
331
- def print_oauth_usage
332
- warn "usage: drill oauth <google-sheets-url>"
376
+ # A painted drill sign for the current item, or a blank space otherwise —
377
+ # used by `drill wb`/`drill sh` to mark the active workbook/sheet.
378
+ def current_marker(is_current)
379
+ is_current ? Lexdrill::Colorizer.paint_yellow(Lexdrill::LineFormatter::SEPARATOR) : " "
380
+ end
381
+
382
+ def print_no_workbooks
383
+ warn "drill: no workbooks configured; run `drill wb add <url>`"
384
+ 1
385
+ end
386
+
387
+ def run_wb_add
388
+ url = argv[2]
389
+ return print_wb_add_usage unless url
390
+
391
+ spreadsheet_id = Lexdrill::Workbooks.extract_id(url)
392
+ return print_invalid_wb_url(url) unless spreadsheet_id
393
+
394
+ add_workbook(spreadsheet_id)
395
+ rescue Lexdrill::GoogleAuth::AuthError, Lexdrill::ServiceAccountAuth::AuthError => error
396
+ warn "drill: #{error.message}"
333
397
  1
398
+ rescue Lexdrill::SheetsClient::ApiError => error
399
+ print_sheets_api_error(error)
400
+ rescue Lexdrill::HTTPClient::NetworkError => error
401
+ warn "drill: network error talking to Google (#{error.message})"
402
+ 1
403
+ end
404
+
405
+ def add_workbook(spreadsheet_id)
406
+ token = current_token
407
+ return print_no_auth_mode unless token
408
+
409
+ title = Lexdrill::SheetsClient.spreadsheet_title(spreadsheet_id, token)
410
+ return print_wb_name_taken(title) if Lexdrill::Workbooks.names.include?(title)
411
+
412
+ Lexdrill::Workbooks.add(title, spreadsheet_id)
413
+ puts "workbook added: #{title.inspect}"
414
+ 0
334
415
  end
335
416
 
336
- def print_invalid_oauth_url(url)
417
+ def print_wb_add_usage
418
+ warn "usage: drill wb add <url>"
419
+ 1
420
+ end
421
+
422
+ def print_invalid_wb_url(url)
337
423
  warn "drill: could not find a spreadsheet id in #{url.inspect}"
338
424
  1
339
425
  end
340
426
 
341
- # Uses whichever of `drill remote` (service account) / `drill oauth`
342
- # (personal login) was set more recently, so switching between them with
343
- # either command always takes effect. Returns [spreadsheet_id,
344
- # access_token], or nil if neither is configured.
345
- def sheets_target
346
- kind = latest_remote_kind
347
- spreadsheet_id = spreadsheet_id_for(kind)
348
- return unless spreadsheet_id
427
+ def print_wb_name_taken(title)
428
+ warn "drill: a workbook named #{title.inspect} is already configured"
429
+ 1
430
+ end
431
+
432
+ def run_wb_remove
433
+ name = argv[2]
434
+ return print_wb_remove_usage unless name
435
+ return print_no_such_workbook(name) unless Lexdrill::Workbooks.remove(name)
436
+
437
+ puts "workbook removed: #{name.inspect}"
438
+ 0
439
+ end
440
+
441
+ def print_wb_remove_usage
442
+ warn "usage: drill wb remove <name>"
443
+ 1
444
+ end
445
+
446
+ def print_no_such_workbook(name)
447
+ warn "drill: no workbook named #{name.inspect} (see `drill wb index`)"
448
+ 1
449
+ end
450
+
451
+ def run_wb_use
452
+ name = argv[2]
453
+ return print_wb_use_usage unless name
454
+ return print_no_such_workbook(name) unless Lexdrill::Workbooks.use(name)
349
455
 
350
- [spreadsheet_id, kind == :remote ? Lexdrill::ServiceAccountAuth.fetch_token! : Lexdrill::GoogleAuth.ensure_token!]
456
+ puts "workbook set: #{name.inspect}"
457
+ 0
351
458
  end
352
459
 
353
- def spreadsheet_id_for(kind)
354
- case kind
355
- when :remote then Lexdrill::Remote.spreadsheet_id
356
- when :oauth then Lexdrill::OauthRemote.spreadsheet_id
460
+ def print_wb_use_usage
461
+ warn "usage: drill wb use <name>"
462
+ 1
463
+ end
464
+
465
+ def run_sh
466
+ case argv[1]
467
+ when nil, "index" then run_sh_index
468
+ when "add" then run_sh_add
469
+ when "remove" then run_sh_remove
470
+ when "use" then run_sh_use
471
+ else print_sh_usage
357
472
  end
358
473
  end
359
474
 
360
- def latest_remote_kind
361
- remote_set = Lexdrill::Remote.configured?
362
- oauth_set = Lexdrill::OauthRemote.configured?
363
- return :remote if remote_set && (!oauth_set || newer?(Lexdrill::Remote::PATH, Lexdrill::OauthRemote::PATH))
364
- return :oauth if oauth_set
475
+ def print_sh_usage
476
+ warn "usage: drill sh index|add <name>|remove <name>|use <name>"
477
+ 1
478
+ end
365
479
 
366
- nil
480
+ def print_no_workbook_selected
481
+ warn "drill: no workbook selected; run `drill wb add <url>` or `drill wb use <name>` first"
482
+ 1
367
483
  end
368
484
 
369
- def newer?(path, other_path) = File.mtime(path) >= File.mtime(other_path)
485
+ def run_sh_index
486
+ spreadsheet_id = Lexdrill::Workbooks.current_id
487
+ return print_no_workbook_selected unless spreadsheet_id
370
488
 
371
- def run_sheet
372
- spreadsheet_id = spreadsheet_id_for(latest_remote_kind)
373
- return print_no_remote unless spreadsheet_id
489
+ token = current_token
490
+ return print_no_auth_mode unless token
374
491
 
375
- puts "https://docs.google.com/spreadsheets/d/#{spreadsheet_id}/edit"
492
+ list_sheets(spreadsheet_id, token)
493
+ rescue Lexdrill::GoogleAuth::AuthError, Lexdrill::ServiceAccountAuth::AuthError => error
494
+ warn "drill: #{error.message}"
495
+ 1
496
+ rescue Lexdrill::SheetsClient::ApiError => error
497
+ print_sheets_api_error(error)
498
+ rescue Lexdrill::HTTPClient::NetworkError => error
499
+ warn "drill: network error talking to Google (#{error.message})"
500
+ 1
501
+ end
502
+
503
+ def list_sheets(spreadsheet_id, token)
504
+ current = Lexdrill::Workbooks.current_sheet
505
+ Lexdrill::SheetsClient.sheet_titles(spreadsheet_id, token).each do |title|
506
+ puts "#{current_marker(title == current)} #{title}"
507
+ end
376
508
  0
377
509
  end
378
510
 
379
- def run_export
380
- sheet_name = argv[1]
381
- return print_export_usage unless sheet_name
511
+ def run_sh_add
512
+ name = argv[2]
513
+ return print_sh_add_usage unless name
382
514
 
383
- target = sheets_target
384
- return print_no_remote unless target
515
+ spreadsheet_id = Lexdrill::Workbooks.current_id
516
+ return print_no_workbook_selected unless spreadsheet_id
385
517
 
386
- perform_export(sheet_name, *target)
518
+ token = current_token
519
+ return print_no_auth_mode unless token
520
+
521
+ add_sheet(spreadsheet_id, name, token)
387
522
  rescue Lexdrill::GoogleAuth::AuthError, Lexdrill::ServiceAccountAuth::AuthError => error
388
523
  warn "drill: #{error.message}"
389
524
  1
390
525
  rescue Lexdrill::SheetsClient::ApiError => error
391
- print_sheets_api_error(error, sheet_name)
526
+ print_sheets_api_error(error, name)
392
527
  rescue Lexdrill::HTTPClient::NetworkError => error
393
528
  warn "drill: network error talking to Google (#{error.message})"
394
529
  1
395
530
  end
396
531
 
397
- def perform_export(sheet_name, spreadsheet_id, token)
398
- rows = export_rows
399
- Lexdrill::SheetsClient.overwrite_sheet(spreadsheet_id, sheet_name, rows, token)
400
- puts "exported #{rows.size} word(s) to #{sheet_name.inspect}"
532
+ def add_sheet(spreadsheet_id, name, token)
533
+ sheet_id = Lexdrill::SheetsClient.add_sheet(spreadsheet_id, name, token)
534
+ Lexdrill::Workbooks.set_current_sheet(name, sheet_id) unless Lexdrill::Workbooks.current_sheet
535
+ puts "sheet added: #{name.inspect}"
401
536
  0
402
537
  end
403
538
 
404
- def export_rows
405
- Lexdrill::WordList.words.map { |word| [word] }
539
+ def print_sh_add_usage
540
+ warn "usage: drill sh add <name>"
541
+ 1
542
+ end
543
+
544
+ def run_sh_remove
545
+ name = argv[2]
546
+ return print_sh_remove_usage unless name
547
+
548
+ spreadsheet_id = Lexdrill::Workbooks.current_id
549
+ return print_no_workbook_selected unless spreadsheet_id
550
+
551
+ token = current_token
552
+ return print_no_auth_mode unless token
553
+
554
+ remove_sheet(spreadsheet_id, name, token)
555
+ rescue Lexdrill::GoogleAuth::AuthError, Lexdrill::ServiceAccountAuth::AuthError => error
556
+ warn "drill: #{error.message}"
557
+ 1
558
+ rescue Lexdrill::SheetsClient::ApiError => error
559
+ print_sheets_api_error(error, name)
560
+ rescue Lexdrill::HTTPClient::NetworkError => error
561
+ warn "drill: network error talking to Google (#{error.message})"
562
+ 1
563
+ end
564
+
565
+ def remove_sheet(spreadsheet_id, name, token)
566
+ sheet_id = Lexdrill::SheetsClient.find_sheet_id(spreadsheet_id, name, token)
567
+ return print_no_such_sheet(name) unless sheet_id
568
+
569
+ Lexdrill::SheetsClient.delete_sheet(spreadsheet_id, sheet_id, token)
570
+ puts "sheet removed: #{name.inspect}"
571
+ 0
406
572
  end
407
573
 
408
- def print_export_usage
409
- warn "usage: drill export <sheet-name>"
574
+ def print_sh_remove_usage
575
+ warn "usage: drill sh remove <name>"
410
576
  1
411
577
  end
412
578
 
413
- def print_no_remote
414
- warn "drill: no remote spreadsheet configured; run `drill remote <url>` (service account) " \
415
- "or `drill oauth <url>` (personal login) first"
579
+ def print_no_such_sheet(name)
580
+ warn "drill: no tab named #{name.inspect} in the current workbook"
416
581
  1
417
582
  end
418
583
 
419
- def run_import
420
- sheet_name = argv[1]
421
- return print_import_usage unless sheet_name
584
+ def run_sh_use
585
+ name = argv[2]
586
+ return print_sh_use_usage unless name
587
+
588
+ spreadsheet_id = Lexdrill::Workbooks.current_id
589
+ return print_no_workbook_selected unless spreadsheet_id
422
590
 
423
- target = sheets_target
424
- return print_no_remote unless target
591
+ token = current_token
592
+ return print_no_auth_mode unless token
425
593
 
426
- perform_import(sheet_name, *target)
594
+ select_sheet(spreadsheet_id, name, token)
427
595
  rescue Lexdrill::GoogleAuth::AuthError, Lexdrill::ServiceAccountAuth::AuthError => error
428
596
  warn "drill: #{error.message}"
429
597
  1
430
598
  rescue Lexdrill::SheetsClient::ApiError => error
431
- print_sheets_api_error(error, sheet_name)
599
+ print_sheets_api_error(error, name)
432
600
  rescue Lexdrill::HTTPClient::NetworkError => error
433
601
  warn "drill: network error talking to Google (#{error.message})"
434
602
  1
435
603
  end
436
604
 
437
- def perform_import(sheet_name, spreadsheet_id, token)
438
- words = Lexdrill::SheetsClient.read_column(spreadsheet_id, sheet_name, token)
439
- return print_empty_import(sheet_name) if words.empty?
605
+ def select_sheet(spreadsheet_id, name, token)
606
+ sheet_id = Lexdrill::SheetsClient.find_sheet_id(spreadsheet_id, name, token)
607
+ return print_no_such_sheet(name) unless sheet_id
440
608
 
441
- File.write(Lexdrill::WordList::PATH, "#{words.join("\n")}\n", encoding: "UTF-8")
442
- puts "imported #{words.size} word(s) from #{sheet_name.inspect}"
609
+ Lexdrill::Workbooks.set_current_sheet(name, sheet_id)
610
+ perform_pull(spreadsheet_id, name, token)
611
+ end
612
+
613
+ def print_sh_use_usage
614
+ warn "usage: drill sh use <name>"
615
+ 1
616
+ end
617
+
618
+ def run_open
619
+ url = Lexdrill::Workbooks.current_url
620
+ return print_no_workbook_selected unless url
621
+
622
+ sheet_id = Lexdrill::Workbooks.current_sheet_id
623
+ Lexdrill::Browser.open(sheet_id ? "#{url}#gid=#{sheet_id}" : url)
443
624
  0
444
625
  end
445
626
 
446
- def print_import_usage
447
- warn "usage: drill import <sheet-name>"
627
+ def print_no_sheet_selected
628
+ warn "drill: no sheet selected; run `drill sh use <name>` first"
448
629
  1
449
630
  end
450
631
 
451
- def print_empty_import(sheet_name)
452
- warn "drill: #{sheet_name.inspect} has no data to import"
632
+ def run_push
633
+ spreadsheet_id = Lexdrill::Workbooks.current_id
634
+ return print_no_workbook_selected unless spreadsheet_id
635
+
636
+ sheet_name = Lexdrill::Workbooks.current_sheet
637
+ return print_no_sheet_selected unless sheet_name
638
+
639
+ token = current_token
640
+ return print_no_auth_mode unless token
641
+
642
+ perform_push(spreadsheet_id, sheet_name, token)
643
+ rescue Lexdrill::GoogleAuth::AuthError, Lexdrill::ServiceAccountAuth::AuthError => error
644
+ warn "drill: #{error.message}"
453
645
  1
646
+ rescue Lexdrill::SheetsClient::ApiError => error
647
+ print_sheets_api_error(error, sheet_name)
648
+ rescue Lexdrill::HTTPClient::NetworkError => error
649
+ warn "drill: network error talking to Google (#{error.message})"
650
+ 1
651
+ end
652
+
653
+ def perform_push(spreadsheet_id, sheet_name, token)
654
+ rows = push_rows
655
+ Lexdrill::SheetsClient.overwrite_sheet(spreadsheet_id, sheet_name, rows, token)
656
+ puts "pushed #{rows.size} word(s) to #{sheet_name.inspect}"
657
+ 0
658
+ end
659
+
660
+ def push_rows
661
+ Lexdrill::WordList.words.map { |word| [word] }
454
662
  end
455
663
 
456
- def print_sheets_api_error(error, sheet_name)
664
+ def run_pull
665
+ spreadsheet_id = Lexdrill::Workbooks.current_id
666
+ return print_no_workbook_selected unless spreadsheet_id
667
+
668
+ sheet_name = Lexdrill::Workbooks.current_sheet
669
+ return print_no_sheet_selected unless sheet_name
670
+
671
+ token = current_token
672
+ return print_no_auth_mode unless token
673
+
674
+ perform_pull(spreadsheet_id, sheet_name, token)
675
+ rescue Lexdrill::GoogleAuth::AuthError, Lexdrill::ServiceAccountAuth::AuthError => error
676
+ warn "drill: #{error.message}"
677
+ 1
678
+ rescue Lexdrill::SheetsClient::ApiError => error
679
+ print_sheets_api_error(error, sheet_name)
680
+ rescue Lexdrill::HTTPClient::NetworkError => error
681
+ warn "drill: network error talking to Google (#{error.message})"
682
+ 1
683
+ end
684
+
685
+ def perform_pull(spreadsheet_id, sheet_name, token)
686
+ words = Lexdrill::SheetsClient.read_column(spreadsheet_id, sheet_name, token)
687
+ return print_empty_pull(sheet_name) if words.empty?
688
+
689
+ File.write(Lexdrill::WordList::PATH, "#{words.join("\n")}\n", encoding: "UTF-8")
690
+ puts "pulled #{words.size} word(s) from #{sheet_name.inspect}"
691
+ 0
692
+ end
693
+
694
+ def print_empty_pull(sheet_name)
695
+ warn "drill: #{sheet_name.inspect} has no data to pull"
696
+ 1
697
+ end
698
+
699
+ def print_sheets_api_error(error, sheet_name = nil)
457
700
  status = error.status
458
701
  message = error.message
459
702
  case status
460
- when 404 then warn "drill: spreadsheet not found or not accessible (check `drill remote`/`drill oauth`)"
703
+ when 404 then warn "drill: spreadsheet not found or not accessible (check `drill wb index`)"
461
704
  when 403 then warn "drill: access denied — make sure the spreadsheet is shared with the right account"
462
- when 400 then warn "drill: #{message} (is #{sheet_name.inspect} a real tab in the spreadsheet?)"
705
+ when 400 then print_400_error(message, sheet_name)
463
706
  else warn "drill: Google Sheets API error (#{status}): #{message}"
464
707
  end
465
708
  1
466
709
  end
467
710
 
711
+ def print_400_error(message, sheet_name)
712
+ hint = " (is #{sheet_name.inspect} a real tab in the spreadsheet?)" if sheet_name
713
+ warn "drill: #{message}#{hint}"
714
+ end
715
+
468
716
  def print_unknown_command(command)
469
717
  warn "drill: unknown command #{command.inspect}"
470
718
  print_help
@@ -15,10 +15,20 @@ module Lexdrill::Inspector
15
15
  Beat: #{beat_summary}
16
16
  Rand: #{rand_summary}
17
17
  Color: #{Lexdrill::Color.current}
18
+ Auth mode: #{Lexdrill::AuthMode.current || 'not configured'}
19
+ Workbook: #{workbook_summary}
20
+ Sheet: #{Lexdrill::Workbooks.current_sheet || 'not selected'}
18
21
  Config dir: #{Lexdrill::Config::DIR}
19
22
  REPORT
20
23
  end
21
24
 
25
+ def self.workbook_summary
26
+ name = Lexdrill::Workbooks.current_name
27
+ return "not selected" unless name
28
+
29
+ "#{name} (#{Lexdrill::Workbooks.current_url})"
30
+ end
31
+
22
32
  def self.words_summary
23
33
  return "missing" unless File.exist?(Lexdrill::WordList::PATH)
24
34
 
@@ -4,10 +4,12 @@ require "uri"
4
4
  require "json"
5
5
 
6
6
  # Thin wrapper around the Google Sheets API v4 REST endpoints needed by
7
- # `drill export`/`drill import`: create the target tab if it doesn't already
7
+ # `drill push`/`drill pull`: create the target tab if it doesn't already
8
8
  # exist, clear its contents, write new rows into it, then auto-fit column
9
9
  # A's width to the content so long phrases aren't visually truncated; or,
10
- # for import, read column A back out of an existing tab.
10
+ # for pull, read column A back out of an existing tab. Also covers
11
+ # workbook/tab management for `drill wb`/`drill sh`: the workbook's title,
12
+ # the list of tab titles, and creating/deleting a tab.
11
13
  module Lexdrill::SheetsClient
12
14
  BASE_URL = "https://sheets.googleapis.com/v4/spreadsheets"
13
15
 
@@ -42,13 +44,27 @@ module Lexdrill::SheetsClient
42
44
  end
43
45
  private_class_method :find_sheet
44
46
 
47
+ # The sheetId for a tab by title, or nil if no tab has that title —
48
+ # for `drill sh use`/`drill sh remove`, which need the numeric id (not
49
+ # just the name) to build a direct link or issue a delete request.
50
+ def self.find_sheet_id(spreadsheet_id, sheet_name, access_token)
51
+ find_sheet(spreadsheet_id, sheet_name, access_token)&.[]("sheetId")
52
+ end
53
+
54
+ # Creates a new, empty tab, for `drill sh add`. Returns its sheetId.
45
55
  def self.add_sheet(spreadsheet_id, sheet_name, access_token)
46
56
  url = "#{BASE_URL}/#{spreadsheet_id}:batchUpdate"
47
57
  body = { "requests" => [{ "addSheet" => { "properties" => { "title" => sheet_name } } }] }
48
58
  result = handle_response(Lexdrill::HTTPClient.json_post(url, body: body, headers: auth_header(access_token)))
49
59
  result.dig("replies", 0, "addSheet", "properties", "sheetId")
50
60
  end
51
- private_class_method :add_sheet
61
+
62
+ # Deletes a tab by its numeric sheetId, for `drill sh remove`.
63
+ def self.delete_sheet(spreadsheet_id, sheet_id, access_token)
64
+ url = "#{BASE_URL}/#{spreadsheet_id}:batchUpdate"
65
+ body = { "requests" => [{ "deleteSheet" => { "sheetId" => sheet_id } }] }
66
+ handle_response(Lexdrill::HTTPClient.json_post(url, body: body, headers: auth_header(access_token)))
67
+ end
52
68
 
53
69
  def self.autofit_first_column(spreadsheet_id, sheet_id, access_token)
54
70
  url = "#{BASE_URL}/#{spreadsheet_id}:batchUpdate"
@@ -77,6 +93,20 @@ module Lexdrill::SheetsClient
77
93
  data.fetch("values", []).filter_map { |row| row[0]&.strip }.reject(&:empty?)
78
94
  end
79
95
 
96
+ # The titles of every tab in the workbook, for `drill sh index`.
97
+ def self.sheet_titles(spreadsheet_id, access_token)
98
+ url = "#{BASE_URL}/#{spreadsheet_id}?fields=sheets.properties.title"
99
+ data = handle_response(Lexdrill::HTTPClient.json_get(url, headers: auth_header(access_token)))
100
+ data.fetch("sheets", []).map { |sheet| sheet.dig("properties", "title") }
101
+ end
102
+
103
+ # The workbook's own title, for naming it automatically in `drill wb add`.
104
+ def self.spreadsheet_title(spreadsheet_id, access_token)
105
+ url = "#{BASE_URL}/#{spreadsheet_id}?fields=properties.title"
106
+ data = handle_response(Lexdrill::HTTPClient.json_get(url, headers: auth_header(access_token)))
107
+ data.dig("properties", "title")
108
+ end
109
+
80
110
  def self.auth_header(token)
81
111
  { "Authorization" => "Bearer #{token}" }
82
112
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lexdrill
4
- VERSION = "0.19.0"
4
+ VERSION = "0.24.0"
5
5
  end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ # Global list of known Google Sheets workbooks (name => spreadsheet id),
6
+ # which one is currently active, and — per workbook — which tab is
7
+ # currently active within it. Lives at ~/.drill/workbooks.json. Names are
8
+ # the spreadsheet's own title (see `drill wb add`), not chosen by hand.
9
+ module Lexdrill::Workbooks
10
+ PATH = Lexdrill::Config.path("workbooks.json")
11
+ URL_PATTERN = %r{/spreadsheets/d/([a-zA-Z0-9_-]+)}
12
+
13
+ def self.extract_id(url)
14
+ url[URL_PATTERN, 1]
15
+ end
16
+
17
+ def self.names
18
+ load.fetch("workbooks").keys
19
+ end
20
+
21
+ def self.add(name, spreadsheet_id)
22
+ data = load
23
+ data["workbooks"][name] = { "id" => spreadsheet_id }
24
+ data["current"] ||= name
25
+ save(data)
26
+ end
27
+
28
+ def self.remove(name)
29
+ data = load
30
+ return false unless data["workbooks"].delete(name)
31
+
32
+ data["current"] = nil if data["current"] == name
33
+ save(data)
34
+ true
35
+ end
36
+
37
+ def self.use(name)
38
+ data = load
39
+ return false unless data["workbooks"].key?(name)
40
+
41
+ data["current"] = name
42
+ save(data)
43
+ true
44
+ end
45
+
46
+ def self.current_name
47
+ load["current"]
48
+ end
49
+
50
+ def self.current_id
51
+ current_entry&.fetch("id")
52
+ end
53
+
54
+ def self.url_for(name)
55
+ entry = load.fetch("workbooks")[name]
56
+ entry && "https://docs.google.com/spreadsheets/d/#{entry.fetch('id')}/edit"
57
+ end
58
+
59
+ def self.current_url
60
+ name = current_name
61
+ name && url_for(name)
62
+ end
63
+
64
+ def self.set_current_sheet(sheet_name, sheet_id)
65
+ data = load
66
+ workbooks = data["workbooks"]
67
+ name = data["current"]
68
+ return false unless name && workbooks.key?(name)
69
+
70
+ entry = workbooks[name]
71
+ entry["current_sheet"] = sheet_name
72
+ entry["current_sheet_id"] = sheet_id
73
+ save(data)
74
+ true
75
+ end
76
+
77
+ def self.current_sheet
78
+ current_entry&.[]("current_sheet")
79
+ end
80
+
81
+ def self.current_sheet_id
82
+ current_entry&.[]("current_sheet_id")
83
+ end
84
+
85
+ def self.current_entry
86
+ name = current_name
87
+ name && load.fetch("workbooks")[name]
88
+ end
89
+ private_class_method :current_entry
90
+
91
+ def self.load
92
+ return { "current" => nil, "workbooks" => {} } unless File.exist?(PATH)
93
+
94
+ data = JSON.parse(File.read(PATH, encoding: "UTF-8"))
95
+ data["workbooks"] ||= {}
96
+ data
97
+ rescue JSON::ParserError
98
+ { "current" => nil, "workbooks" => {} }
99
+ end
100
+ private_class_method :load
101
+
102
+ def self.save(data)
103
+ File.write(PATH, JSON.generate(data), encoding: "UTF-8")
104
+ end
105
+ private_class_method :save
106
+ end
data/lib/lexdrill.rb CHANGED
@@ -9,8 +9,9 @@ require_relative "lexdrill/stats"
9
9
  require_relative "lexdrill/word_list"
10
10
  require_relative "lexdrill/toggle"
11
11
  require_relative "lexdrill/rand"
12
- require_relative "lexdrill/remote"
13
- require_relative "lexdrill/oauth_remote"
12
+ require_relative "lexdrill/auth_mode"
13
+ require_relative "lexdrill/workbooks"
14
+ require_relative "lexdrill/browser"
14
15
  require_relative "lexdrill/http_client"
15
16
  require_relative "lexdrill/google_auth"
16
17
  require_relative "lexdrill/service_account_auth"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lexdrill
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.24.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Siarhei Kisliak
@@ -25,7 +25,9 @@ files:
25
25
  - bin/drill
26
26
  - lexdrill.gemspec
27
27
  - lib/lexdrill.rb
28
+ - lib/lexdrill/auth_mode.rb
28
29
  - lib/lexdrill/beat.rb
30
+ - lib/lexdrill/browser.rb
29
31
  - lib/lexdrill/cli.rb
30
32
  - lib/lexdrill/color.rb
31
33
  - lib/lexdrill/colorizer.rb
@@ -36,9 +38,7 @@ files:
36
38
  - lib/lexdrill/http_client.rb
37
39
  - lib/lexdrill/inspector.rb
38
40
  - lib/lexdrill/line_formatter.rb
39
- - lib/lexdrill/oauth_remote.rb
40
41
  - lib/lexdrill/rand.rb
41
- - lib/lexdrill/remote.rb
42
42
  - lib/lexdrill/service_account_auth.rb
43
43
  - lib/lexdrill/sheets_client.rb
44
44
  - lib/lexdrill/shell_snippet.rb
@@ -46,6 +46,7 @@ files:
46
46
  - lib/lexdrill/toggle.rb
47
47
  - lib/lexdrill/version.rb
48
48
  - lib/lexdrill/word_list.rb
49
+ - lib/lexdrill/workbooks.rb
49
50
  homepage: https://github.com/kislak/lexdrill
50
51
  licenses:
51
52
  - MIT
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Global "which Google Sheet to export to via the OAuth device-flow login"
4
- # setting. Stored as a plain spreadsheet id, parsed out of a normal
5
- # Google Sheets share URL. Set via `drill oauth <url>`. See
6
- # Lexdrill::Remote for the separate, service-account-based flow.
7
- module Lexdrill::OauthRemote
8
- PATH = Lexdrill::Config.path("oauth-remote")
9
- URL_PATTERN = %r{/spreadsheets/d/([a-zA-Z0-9_-]+)}
10
-
11
- def self.set(url)
12
- id = extract_id(url)
13
- raise ArgumentError, "no spreadsheet id found in #{url.inspect}" unless id
14
-
15
- File.write(PATH, id)
16
- end
17
-
18
- def self.extract_id(url)
19
- url[URL_PATTERN, 1]
20
- end
21
-
22
- def self.configured?
23
- File.exist?(PATH)
24
- end
25
-
26
- def self.spreadsheet_id
27
- File.read(PATH).strip if configured?
28
- end
29
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Global "which Google Sheet to export to via a service account" setting.
4
- # Stored as a plain spreadsheet id, parsed out of a normal Google Sheets
5
- # share URL. Set via `drill remote <url>` — the user shares that
6
- # spreadsheet with their service account's email directly in Google
7
- # Sheets, so no interactive Google sign-in is needed at export/import time.
8
- # See Lexdrill::OauthRemote for the separate, personal-login-based flow.
9
- module Lexdrill::Remote
10
- PATH = Lexdrill::Config.path("remote")
11
- URL_PATTERN = %r{/spreadsheets/d/([a-zA-Z0-9_-]+)}
12
-
13
- def self.set(url)
14
- id = extract_id(url)
15
- raise ArgumentError, "no spreadsheet id found in #{url.inspect}" unless id
16
-
17
- File.write(PATH, id)
18
- end
19
-
20
- def self.extract_id(url)
21
- url[URL_PATTERN, 1]
22
- end
23
-
24
- def self.configured?
25
- File.exist?(PATH)
26
- end
27
-
28
- def self.spreadsheet_id
29
- File.read(PATH).strip if configured?
30
- end
31
- end