Merge branch 'fix/mastodon-api-settings' into 'develop'
[akkoma] / lib / mix / tasks / generate_config.ex
1 defmodule Mix.Tasks.GenerateConfig do
2 use Mix.Task
3
4 @moduledoc """
5 Generate a new config
6
7 ## Usage
8 ``mix generate_config``
9
10 This mix task is interactive, and will overwrite the config present at ``config/generated_config.exs``.
11 """
12
13 def run(_) do
14 IO.puts("Answer a few questions to generate a new config\n")
15 IO.puts("--- THIS WILL OVERWRITE YOUR config/generated_config.exs! ---\n")
16 domain = IO.gets("What is your domain name? (e.g. pleroma.soykaf.com): ") |> String.trim()
17 name = IO.gets("What is the name of your instance? (e.g. Pleroma/Soykaf): ") |> String.trim()
18 email = IO.gets("What's your admin email address: ") |> String.trim()
19
20 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
21 dbpass = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
22
23 resultSql = EEx.eval_file("lib/mix/tasks/sample_psql.eex", dbpass: dbpass)
24
25 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
26
27 result =
28 EEx.eval_file(
29 "lib/mix/tasks/sample_config.eex",
30 domain: domain,
31 email: email,
32 name: name,
33 secret: secret,
34 dbpass: dbpass,
35 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
36 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false)
37 )
38
39 IO.puts(
40 "\nWriting config to config/generated_config.exs.\n\nCheck it and configure your database, then copy it to either config/dev.secret.exs or config/prod.secret.exs"
41 )
42
43 File.write("config/generated_config.exs", result)
44
45 IO.puts(
46 "\nWriting setup_db.psql, please run it as postgre superuser, i.e.: sudo su postgres -c 'psql -f config/setup_db.psql'"
47 )
48
49 File.write("config/setup_db.psql", resultSql)
50 end
51 end