Class: Jekyll::Commands::GenerateKeys

Inherits:
Jekyll::Command
  • Object
show all
Defined in:
lib/jekyll/commands/generate_keys.rb

Overview

Send Activity Pub notifications

Class Method Summary collapse

Class Method Details

.init_with_program(prog) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jekyll/commands/generate_keys.rb', line 11

def init_with_program(prog)
  prog.command(:generate_keys) do |c|
    c.syntax      'generate_keys --key-size 2048 --key path/to/privkey.pem'
    c.description 'Generate an RSA keypair for ActivityPub'

    activity_pub_private_key c

    c.option 'activity_pub_key_size', '--key-size 2048', Integer, 'RSA key size (2048 by default)'

    c.action do |_, options|
      process_with_graceful_fail(c, options, self)
    end
  end
end

.process(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jekyll/commands/generate_keys.rb', line 26

def process(options)
  # Adjust verbosity quickly
  Jekyll.logger.adjust_verbosity(options)

  private_key_path = options['activity_pub_private_key'] || 'privkey.pem'
  key_size = options['activity_pub_key_size'] || 2048

  if File.exist? private_key_path
    raise Jekyll::Errors::FatalException, "Private key already exists: #{private_key_path}"
  end

  client = DistributedPress::V1::Social::Client.new(public_key_url: nil, key_size: key_size)

  File.open(private_key_path, 'w') do |f|
    f.write client.private_key.export
  rescue StandardError
    FileUtils.rm_f(private_key_path)
    raise
  end

  Jekyll.logger.info 'ActivityPub:', "Private key written to #{private_key_path}"
end