# Modified from Pleroma, lib/mix/tasks/pleroma/instance.ex
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
defmodule Mix.Tasks.FuckGab.Config do
|
|
use Mix.Task
|
|
import Mix.FuckGab
|
|
|
|
alias FuckGab.Config
|
|
|
|
@shortdoc "Manages Fuck Gab configuration"
|
|
|
|
def run(["gen" | rest]) do
|
|
{options, [], []} =
|
|
OptionParser.parse(
|
|
rest,
|
|
strict: [
|
|
force: :boolean,
|
|
output: :string,
|
|
output_psql: :string,
|
|
domain: :string,
|
|
site_name: :string,
|
|
site_description: :string,
|
|
dbhost: :string,
|
|
dbname: :string,
|
|
dbuser: :string,
|
|
dbpass: :string,
|
|
listen_ip: :string,
|
|
listen_port: :string
|
|
]
|
|
)
|
|
|
|
paths =
|
|
[config_path, psql_path] = [
|
|
Keyword.get(options, :output, "config/generated_config.exs"),
|
|
Keyword.get(options, :output_psql, "config/setup_db.psql")
|
|
]
|
|
|
|
will_overwrite = Enum.filter(paths, &File.exists?/1)
|
|
proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
|
|
|
|
if proceed? do
|
|
[domain, port | _] =
|
|
String.split(
|
|
get_option(
|
|
options,
|
|
:domain,
|
|
"What domain will your instance use? (e.g. fuckgab.com)"
|
|
),
|
|
":"
|
|
) ++ [443]
|
|
|
|
site_name =
|
|
get_option(
|
|
options,
|
|
:site_name,
|
|
"What will the name of the site be?",
|
|
Config.get([:site, :name]),
|
|
"Default site name"
|
|
)
|
|
|
|
site_description =
|
|
get_option(
|
|
options,
|
|
:site_description,
|
|
"What will the description of the site be?",
|
|
Config.get([:site, :description, :content]),
|
|
"Default description"
|
|
)
|
|
|
|
dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
|
|
|
|
dbname = get_option(options, :dbname, "What is the name of your database?", "fuck_gab")
|
|
|
|
dbuser =
|
|
get_option(
|
|
options,
|
|
:dbuser,
|
|
"What is the user used to connect to your database?",
|
|
"fuck_gab"
|
|
)
|
|
|
|
dbpass =
|
|
get_option(
|
|
options,
|
|
:dbpass,
|
|
"What is the password used to connect to your database?",
|
|
:crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
|
|
"autogenerated"
|
|
)
|
|
|
|
listen_ip =
|
|
get_option(
|
|
options,
|
|
:listen_ip,
|
|
"What IP address will the app listen to (leave it if you are doing the default setup with nginx)?",
|
|
"127.0.0.1"
|
|
)
|
|
|
|
listen_port =
|
|
get_option(
|
|
options,
|
|
:listen_port,
|
|
"What port will the app listen to (leave it if you are doing the default setup with nginx)?",
|
|
9002
|
|
)
|
|
|
|
secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
|
|
signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
|
|
|
|
live_view_signing_salt =
|
|
:crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
|
|
|
|
template_dir = Application.app_dir(:fuck_gab, "priv") <> "/templates"
|
|
|
|
result_config =
|
|
EEx.eval_file(
|
|
template_dir <> "/sample_config.eex",
|
|
domain: domain,
|
|
port: port,
|
|
site_name: site_name,
|
|
site_description: site_description,
|
|
dbhost: dbhost,
|
|
dbname: dbname,
|
|
dbuser: dbuser,
|
|
dbpass: dbpass,
|
|
secret: secret,
|
|
signing_salt: signing_salt,
|
|
live_view_signing_salt: live_view_signing_salt,
|
|
listen_ip: listen_ip,
|
|
listen_port: listen_port
|
|
)
|
|
|
|
result_psql =
|
|
EEx.eval_file(
|
|
template_dir <> "/sample_psql.eex",
|
|
dbname: dbname,
|
|
dbuser: dbuser,
|
|
dbpass: dbpass
|
|
)
|
|
|
|
shell_info("Writing config to #{config_path}...")
|
|
File.write(config_path, result_config)
|
|
shell_info("Writing the postgres script to #{psql_path}...")
|
|
File.write(psql_path, result_psql)
|
|
|
|
shell_info("\nAll files successfully written!")
|
|
else
|
|
shell_error(
|
|
"The task would have overwritten the following files:\n" <>
|
|
(Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
|
|
"Rerun with `--force` to overwrite them."
|
|
)
|
|
end
|
|
end
|
|
end
|