-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
3 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters