From 08020aee3fb8bb41fad1ccab5cde1b6d1795e63e Mon Sep 17 00:00:00 2001 From: Tom Hipkin Date: Tue, 22 Jun 2021 14:02:17 +0100 Subject: [PATCH] Add warning when accessing console in production If you have multiple Rails consoles open, it's often difficult to know whether you're connected to a local console or to production, which risks mistakes being made. This change makes Pry the default Rails console in all environments, and additionally, when starting a production console without the `--sandbox` switch, show an obvious warning banner to the user. Additionally, the prompt shows "writable" or "sandbox" on every line. You can customise the environment name shown in the warning message and prompts by overriding the `PRODUCTION_ENV` environment variable (e.g. 'staging'). Co-authored-by: Robert Lee-Cann --- .pryrc | 30 ++++++++++++++++++++++++++++++ Gemfile | 2 +- Gemfile.lock | 4 +++- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 .pryrc diff --git a/.pryrc b/.pryrc new file mode 100644 index 00000000..c8ab8334 --- /dev/null +++ b/.pryrc @@ -0,0 +1,30 @@ +Pry.config.color = true + +if defined?(Rails) + production_env = ENV.fetch("PRODUCTION_ENV", "production") + + if Rails.env.production? && Rails.application.sandbox == false + production_warning = "Warning: You are using the #{production_env} console in non-sandboxed mode" + + puts + puts Pry::Helpers::Text.red("*" * production_warning.size) + puts Pry::Helpers::Text.red(production_warning) + puts Pry::Helpers::Text.red("*" * production_warning.size) + puts + end + + environment_prompt = if Rails.env.production? + "(" + Pry::Helpers::Text.red(production_env) + ")" + else + "(" + Pry::Helpers::Text.green(Rails.env) + ")" + end + + sandbox_prompt = Rails.application.sandbox ? "(sandbox)" : "(writable)" + + Pry.config.prompt_name = [ + environment_prompt, + " ", + sandbox_prompt, + " " + ].join +end diff --git a/Gemfile b/Gemfile index 7430d2db..94c1b3ec 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem "high_voltage" gem "jbuilder", "~> 2.11" gem "jquery-rails" gem "pg" +gem "pry-rails" gem "mini_racer" gem "puma", "~> 5.3" gem "rollbar" @@ -46,7 +47,6 @@ group :development, :test do gem "dotenv-rails" gem "factory_bot_rails" gem "faker" - gem "pry" gem "rspec-rails" gem "standard" end diff --git a/Gemfile.lock b/Gemfile.lock index 739a49f9..adb7f454 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -165,6 +165,8 @@ GEM pry (0.14.1) coderay (~> 1.1) method_source (~> 1.0) + pry-rails (0.3.9) + pry (>= 0.10.4) public_suffix (4.0.6) puma (5.3.2) nio4r (~> 2.0) @@ -321,7 +323,7 @@ DEPENDENCIES listen (>= 3.0.5, < 3.6) mini_racer pg - pry + pry-rails puma (~> 5.3) rails (~> 6.1.3) rails_layout