From e5c5b4934e0d5a87a32e7a576ec6931dbfa96a4f Mon Sep 17 00:00:00 2001 From: MotorTruck1221 Date: Mon, 8 Apr 2024 01:33:33 -0600 Subject: [PATCH] Fix freezing --- Dockerfile | 2 +- cli/cli.rb | 98 ++++++++++++++++++++++++++---------------------------- 2 files changed, 49 insertions(+), 51 deletions(-) diff --git a/Dockerfile b/Dockerfile index 83fafca..1d5f610 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /app RUN apt-get update -qq && apt-get install -y build-essential libpq-dev COPY . . RUN bundle install -RUN touch .env +ENV DOCKER=true EXPOSE 9292 CMD ["bundle", "exec", "ruby", "cli/cli.rb", "start"] diff --git a/cli/cli.rb b/cli/cli.rb index 298fda4..51b08fb 100644 --- a/cli/cli.rb +++ b/cli/cli.rb @@ -10,53 +10,17 @@ class ByodCLI < Thor desc "start", "Start the bot" option :sqlite, type: :boolean, default: false, desc: "Use SQLite instead of PostgreSQL" def start - if File.exist?(".env") == false - File.open(".env", "w") do |f| - end - end - if options[:sqlite] == true - if ENV['DATABASE'] == nil - File.open(".env", "a") do |f| - f.write("DATABASE=sqlite\n") - end - end - if ENV['DATABASE'] != "sqlite" - #delete the key - File.open(".env", "r+") do |f| - f.each_line do |line| - if line.include?("DATABASE") - f.seek(-line.length, IO::SEEK_CUR) - f.write("#" + line) - end - end - end - File.open(".env", "a") do |f| - f.write("DATABASE=sqlite\n") - #reinit the evvironment variables - ENV['DATABASE'] = "sqlite" - end - end - else - if ENV['DATABASE'] != "postgres" - #delete the key - File.open(".env", "r+") do |f| - f.each_line do |line| - if line.include?("DATABASE") - f.seek(-line.length, IO::SEEK_CUR) - f.write("#" + line) - end - end - end - File.open(".env", "a") do |f| - f.write("DATABASE=postgres\n") + #if running in docker, completley ignore this + if ENV['DOCKER'] != "true" + if File.exist?(".env") == false + File.open(".env", "w") do |f| end end end - if ENV['REVERSE_PROXY'] == nil while true prompt = TTY::Prompt.new - reverseProxy = prompt.select("Which reverse proxy do you use?", %w(caddy), required: true, default: "caddy") + reverseProxy = prompt.select("Which reverse proxy do you use?", %w(caddy nginx apache), required: true, default: "caddy") reverseProxyConfirm = prompt.yes?("Is #{reverseProxy} the correct reverse proxy?") if reverseProxyConfirm == false next @@ -97,33 +61,67 @@ def start f.write("SERVER_IP=#{serverIP}\n") end end - if options[:sqlite] != true + if options[:sqlite] == true + #delete any existing DATABSE key in the env + if ENV['DATABASE'] != "sqlite" + #delete the key + File.open(".env", "r+") do |f| + f.each_line do |line| + if line.include?("DATABASE") + f.seek(-line.length, IO::SEEK_CUR) + f.write("#" + line) + end + end + end + File.open(".env", "a") do |f| + f.write("DATABASE=sqlite\n") + ENV['DATABASE'] = "sqlite" + end + end + else + if ENV['DATABASE'] != "postgres" + #delete the key + File.open(".env", "r+") do |f| + f.each_line do |line| + if line.include?("DATABASE") + f.seek(-line.length, IO::SEEK_CUR) + f.write("#" + line) + end + end + end + File.open(".env", "a") do |f| + f.write("DATABASE=postgres\n") + end + end + end + if ENV['DATABASE'] == 'postgres' && ENV['DOCKER'] != "true" + puts "Running setup for PostgreSQL database".yellow if ENV['DB_HOST'] == nil prompt = TTY::Prompt.new - dbHost = prompt.ask("What is the host of your PostgreSQL database?", required: true) + host = prompt.ask("What is the host of your PostgreSQL database?", required: true) File.open(".env", "a") do |f| - f.write("DB_HOST=#{dbHost}\n") + f.write("DB_HOST=#{host}\n") end end if ENV['DB_USER'] == nil prompt = TTY::Prompt.new - dbUser = prompt.ask("What is the user of your PostgreSQL database?", required: true) + user = prompt.ask("What is the user of your PostgreSQL database?", required: true) File.open(".env", "a") do |f| - f.write("DB_USER=#{dbUser}\n") + f.write("DB_USER=#{user}\n") end end if ENV['DB_PASSWORD'] == nil prompt = TTY::Prompt.new - dbPassword = prompt.mask("What is the password of your PostgreSQL database?", required: true) + password = prompt.mask("What is the password of your PostgreSQL database?", required: true) File.open(".env", "a") do |f| - f.write("DB_PASSWORD=#{dbPassword}\n") + f.write("DB_PASSWORD=#{password}\n") end end if ENV['DB_NAME'] == nil prompt = TTY::Prompt.new - dbName = prompt.ask("What is the name of your PostgreSQL database?", required: true) + database = prompt.ask("What is the name of your PostgreSQL database?", required: true) File.open(".env", "a") do |f| - f.write("DB_DATABASE=#{dbName}\n") + f.write("DB_NAME=#{database}\n") end end end