From d924b6cd3da7ad96c522f9de45eb8f7453e0f53a Mon Sep 17 00:00:00 2001 From: Rin Toshaka Date: Sun, 2 Dec 2018 20:04:33 +0100 Subject: [PATCH] Refactor copypasta to a private function in instance.ex --- lib/mix/tasks/pleroma/instance.ex | 70 ++++++++++++++++--------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/lib/mix/tasks/pleroma/instance.ex b/lib/mix/tasks/pleroma/instance.ex index 8c728625a..b3e0f99df 100644 --- a/lib/mix/tasks/pleroma/instance.ex +++ b/lib/mix/tasks/pleroma/instance.ex @@ -59,49 +59,37 @@ defmodule Mix.Tasks.Pleroma.Instance do unless not proceed? do domain = - Keyword.get(options, :domain) || - Mix.shell().prompt("What domain will your instance use? (e.g. pleroma.soykaf.com)") - |> String.trim() + get_option( + options, + :domain, + "What domain will your instance use? (e.g pleroma.soykaf.com)" + ) name = - Keyword.get(options, :name) || - Mix.shell().prompt("What is the name of your instance? (e.g. Pleroma/Soykaf)") - |> String.trim() + get_option(options, :name, "What is the name of your instance? (e.g. Pleroma/Soykaf)") - email = - Keyword.get(options, :admin_email) || - Mix.shell().prompt("What is your admin email address?") - |> String.trim() + email = get_option(options, :admin_email, "What is your admin email address?") - dbhost = - Keyword.get(options, :dbhost) || - case Mix.shell().prompt("What is the hostname of your database? [localhost]") do - "\n" -> "localhost" - dbhost -> dbhost |> String.trim() - end + dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost") - dbname = - Keyword.get(options, :dbname) || - case Mix.shell().prompt("What is the name of your database? [pleroma_dev]") do - "\n" -> "pleroma_dev" - dbname -> dbname |> String.trim() - end + dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma_dev") dbuser = - Keyword.get(options, :dbuser) || - case Mix.shell().prompt("What is the user used to connect to your database? [pleroma]") do - "\n" -> "pleroma" - dbuser -> dbuser |> String.trim() - end + get_option( + options, + :dbuser, + "What is the user used to connect to your database?", + "pleroma" + ) dbpass = - Keyword.get(options, :dbpass) || - case Mix.shell().prompt( - "What is the password used to connect to your database? [autogenerated]" - ) do - "\n" -> :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64) - dbpass -> dbpass |> String.trim() - end + 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" + ) secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64) @@ -160,4 +148,18 @@ defmodule Mix.Tasks.Pleroma.Instance do defp escape_sh_path(path) do ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(') end + + defp get_option(options, opt, prompt, def \\ nil, defname \\ nil) do + Keyword.get(options, opt) || + case Mix.shell().prompt("#{prompt} [#{defname || def}]") do + "\n" -> + case def do + nil -> get_option(options, opt, prompt, def) + def -> def + end + + opt -> + opt |> String.trim() + end + end end