pagecord-cli 0.2.3 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 691c38a950d1636ababf3611ca1d767046570a2558673fafe649c0c82712da57
4
- data.tar.gz: d49abbd1d948cf7ed4e52b56a31e32aa091eaa3a826780ade9b57c663d74cac4
3
+ metadata.gz: 282d0f9f75b00c70f396dd00494c60b0e345d8af90689fa6cd6f217ea2288c12
4
+ data.tar.gz: db5c120d320ed5c012c4576e7fce832879336e3b3e255ce0b494ff0ddb489e73
5
5
  SHA512:
6
- metadata.gz: dc466f620c67a38c614ce658d755f6c9f8e775f62a2d133bbc986246aaea38e4a039d2b55f02664bd0d7906b53eb57a82c15814283713e5147c8fb9382e48321
7
- data.tar.gz: 45b006d11a1a4be80e3a8d8728032c8500f41422e9a33c44a59c0dc24cf726e554f1448cafd7c6a1e572fd916e6b06b73a122f58a9e489f2902e03580a9844ea
6
+ metadata.gz: 1b8f2174e7ca61cdba14e6b1accdd617da6c71c1b747c379120c6ff13955d1de26b0256ea34fecb2908e128a19eab52785abd0d2ae2d868916ac129f16d0523a
7
+ data.tar.gz: 872259af41ebbf97ece3a9eb859ebf4509f97568cd8027d8b5d6f813d40c5d96562ee7de51ead2e133922a64c26d2c3f0c7f7ba1896e765de969e9ee01d88f67
data/README.md CHANGED
@@ -60,6 +60,25 @@ Remove a saved blog:
60
60
  pagecord logout myblog
61
61
  ```
62
62
 
63
+ ## Posts and pages
64
+
65
+ Read and change posts directly, without a local file:
66
+
67
+ ```bash
68
+ pagecord post list # published posts, newest first
69
+ pagecord post list --drafts
70
+ pagecord post show TOKEN # fields, then the HTML content
71
+ pagecord post update TOKEN --content-file post.html --status draft
72
+ pagecord post create --title "Hello" --content-file hello.md
73
+ pagecord post delete TOKEN # add --permanent to skip the bin
74
+ ```
75
+
76
+ `pagecord page` takes the same commands. Lists are paged 15 at a time; pass
77
+ `--page N` for more. `--content-file` sends Markdown when the file ends `.md`,
78
+ and HTML otherwise. Other flags: `--title`, `--status draft|published`,
79
+ `--slug`, `--published-at`, `--tags`, `--canonical-url`, `--locale` and
80
+ `--hidden`/`--no-hidden`. `--json` prints the full response.
81
+
63
82
  ## Choosing a blog
64
83
 
65
84
  With one blog configured, every command uses it. With several, set a default
@@ -34,6 +34,7 @@ module PagecordCLI
34
34
 
35
35
  def run
36
36
  return help if argv.empty? || %w[help -h --help].include?(argv.first)
37
+ return version if %w[version -v --version].include?(argv.first)
37
38
 
38
39
  parser.order!(argv)
39
40
  command = argv.shift
@@ -45,6 +46,8 @@ module PagecordCLI
45
46
  when "blog" then blog
46
47
  when "appearance" then appearance
47
48
  when "custom-code" then custom_code
49
+ when "post" then records("posts")
50
+ when "page" then records("pages")
48
51
  when "publish" then publish("published")
49
52
  when "draft" then publish("draft")
50
53
  else
@@ -200,6 +203,112 @@ module PagecordCLI
200
203
  raise Error, "#{e.message}.\nCommon causes: nested CSS, or @import from a host other than Google Fonts or Bunny Fonts.\nSee https://help.pagecord.com/custom-css"
201
204
  end
202
205
 
206
+ def records(resource)
207
+ noun = resource.chomp("s")
208
+
209
+ case argv.shift
210
+ when "list" then list_records(resource)
211
+ when "show" then show_record(resource)
212
+ when "create" then create_record(resource, noun)
213
+ when "update" then update_record(resource, noun)
214
+ when "delete" then delete_record(resource, noun)
215
+ else fail_with("Usage: pagecord #{noun} list|show|create|update|delete")
216
+ end
217
+ rescue Client::Error => e
218
+ raise unless e.status == 404
219
+
220
+ raise Error, "No #{noun} with that token"
221
+ end
222
+
223
+ def list_records(resource)
224
+ status = nil
225
+ page = nil
226
+ parser do |opts|
227
+ opts.on("--drafts") { status = "draft" }
228
+ opts.on("--published") { status = "published" }
229
+ opts.on("--page N", Integer) { |value| page = value }
230
+ end.parse!(argv)
231
+
232
+ records = client_for(resolve_blog).list(resource, status: status, page: page)
233
+
234
+ if options[:json]
235
+ print_json(records)
236
+ else
237
+ records.each do |record|
238
+ date = record["published_at"].to_s[0, 10]
239
+ output.puts [ record["token"], date.empty? ? record["status"] : date, record["title"] ].join(" ")
240
+ end
241
+ end
242
+
243
+ 0
244
+ end
245
+
246
+ def show_record(resource)
247
+ parser.parse!(argv)
248
+ token = argv.shift
249
+ return fail_with("Usage: pagecord #{resource.chomp("s")} show TOKEN") unless token
250
+
251
+ record = client_for(resolve_blog).show(resource, token)
252
+
253
+ if options[:json]
254
+ print_json(record)
255
+ else
256
+ record.except("content").each { |key, value| output.puts "#{key}: #{value.is_a?(Array) ? value.join(", ") : value}" }
257
+ output.puts
258
+ output.puts record["content"]
259
+ end
260
+
261
+ 0
262
+ end
263
+
264
+ def create_record(resource, noun)
265
+ params = record_params
266
+ record = client_for(resolve_blog).create(resource, params)
267
+ options[:json] ? print_json(record) : say("Created #{noun} #{record["token"]}")
268
+ 0
269
+ end
270
+
271
+ def update_record(resource, noun)
272
+ params = record_params
273
+ token = argv.shift
274
+ return fail_with("Usage: pagecord #{noun} update TOKEN [options]") unless token
275
+ return fail_with("Nothing to update") if params.empty?
276
+
277
+ record = client_for(resolve_blog).update(resource, token, params)
278
+ options[:json] ? print_json(record) : say("Updated #{noun} #{record["token"]}")
279
+ 0
280
+ end
281
+
282
+ def delete_record(resource, noun)
283
+ permanent = false
284
+ parser { |opts| opts.on("--permanent") { permanent = true } }.parse!(argv)
285
+ token = argv.shift
286
+ return fail_with("Usage: pagecord #{noun} delete TOKEN [--permanent]") unless token
287
+
288
+ client_for(resolve_blog).delete(resource, token, permanent: permanent)
289
+ say "Deleted #{noun} #{token}"
290
+ 0
291
+ end
292
+
293
+ def record_params
294
+ params = {}
295
+ parser do |opts|
296
+ opts.on("--title TITLE") { |value| params[:title] = value }
297
+ opts.on("--content-file PATH") do |path|
298
+ params[:content] = read_file("content-file", path)
299
+ params[:content_format] = "markdown" if PostFile::MARKDOWN_EXTENSIONS.include?(File.extname(path).downcase)
300
+ end
301
+ opts.on("--status STATUS", %w[draft published]) { |value| params[:status] = value }
302
+ opts.on("--slug SLUG") { |value| params[:slug] = value }
303
+ opts.on("--published-at TIME") { |value| params[:published_at] = value }
304
+ opts.on("--tags TAGS") { |value| params[:tags] = value }
305
+ opts.on("--canonical-url URL") { |value| params[:canonical_url] = value }
306
+ opts.on("--[no-]hidden") { |value| params[:hidden] = value }
307
+ opts.on("--locale LOCALE") { |value| params[:locale] = value }
308
+ end.parse!(argv)
309
+ params
310
+ end
311
+
203
312
  def publish(status)
204
313
  overrides = {}
205
314
  parser do |opts|
@@ -208,7 +317,7 @@ module PagecordCLI
208
317
  opts.on("--published-at TIME") { |value| overrides[:published_at] = value }
209
318
  opts.on("--tags TAGS") { |value| overrides[:tags] = value }
210
319
  opts.on("--canonical-url URL") { |value| overrides[:canonical_url] = value }
211
- opts.on("--hidden") { overrides[:hidden] = true }
320
+ opts.on("--[no-]hidden") { |value| overrides[:hidden] = value }
212
321
  opts.on("--locale LOCALE") { |value| overrides[:locale] = value }
213
322
  end.parse!(argv)
214
323
 
@@ -304,9 +413,16 @@ module PagecordCLI
304
413
  end
305
414
  end
306
415
 
416
+ def version
417
+ options[:json] = argv.include?("--json")
418
+ options[:json] ? print_json({ version: VERSION }) : output.puts("pagecord #{VERSION}")
419
+ 0
420
+ end
421
+
307
422
  def help
308
423
  output.puts <<~HELP
309
424
  Usage:
425
+ pagecord version
310
426
  pagecord login SUBDOMAIN
311
427
  pagecord logout [SUBDOMAIN]
312
428
  pagecord blog list
@@ -315,6 +431,12 @@ module PagecordCLI
315
431
  pagecord appearance update [options]
316
432
  pagecord custom-code show [--css|--footer-html|--head-html|--body-html]
317
433
  pagecord custom-code update [options]
434
+ pagecord post list [--drafts|--published] [--page N]
435
+ pagecord post show TOKEN
436
+ pagecord post create [options]
437
+ pagecord post update TOKEN [options]
438
+ pagecord post delete TOKEN [--permanent]
439
+ pagecord page list|show|create|update|delete (as for post)
318
440
  pagecord publish FILE [SUBDOMAIN] [options]
319
441
  pagecord draft FILE [SUBDOMAIN] [options]
320
442
 
@@ -323,14 +445,16 @@ module PagecordCLI
323
445
  --json machine-readable output
324
446
  --quiet suppress confirmation messages
325
447
 
326
- Publish options:
448
+ Post options (publish, draft, post and page):
327
449
  --title TITLE
328
450
  --slug SLUG
329
451
  --published-at TIME
330
452
  --tags TAGS
331
453
  --canonical-url URL
332
- --hidden
333
454
  --locale LOCALE
455
+ --hidden, --no-hidden
456
+ --content-file PATH post/page create and update; Markdown if it ends .md
457
+ --status draft|published post/page create and update
334
458
 
335
459
  Appearance options:
336
460
  --theme, --font, --width, --layout
@@ -30,6 +30,27 @@ module PagecordCLI
30
30
  true
31
31
  end
32
32
 
33
+ def list(resource, status: nil, page: nil)
34
+ query = URI.encode_www_form({ status: status, page: page }.compact)
35
+ request(Net::HTTP::Get.new(uri_for(query.empty? ? resource : "#{resource}?#{query}")))
36
+ end
37
+
38
+ def show(resource, token)
39
+ request(Net::HTTP::Get.new(uri_for("#{resource}/#{token}")))
40
+ end
41
+
42
+ def create(resource, params)
43
+ request(json_request(Net::HTTP::Post, resource, params))
44
+ end
45
+
46
+ def update(resource, token, params)
47
+ request(json_request(Net::HTTP::Patch, "#{resource}/#{token}", params))
48
+ end
49
+
50
+ def delete(resource, token, permanent: false)
51
+ request(Net::HTTP::Delete.new(uri_for("#{resource}/#{token}#{"?permanent=true" if permanent}")))
52
+ end
53
+
33
54
  def create_post(params)
34
55
  request(json_request(Net::HTTP::Post, "/posts", params))
35
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PagecordCLI
4
- VERSION = "0.2.3"
4
+ VERSION = "0.3.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagecord-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Olly Headey
@@ -46,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  requirements: []
49
- rubygems_version: 4.0.10
49
+ rubygems_version: 4.0.20
50
50
  specification_version: 4
51
51
  summary: Publish local files to Pagecord
52
52
  test_files: []