Skip to content

Commit

Permalink
Use new IRB extension API to add decrypt/encrypt commands
Browse files Browse the repository at this point in the history
See https://github.com/ruby/irb/blob/2f42b2360dd023319519d231863860bc2fd30a8a/EXTEND_IRB.md

Rails::ConsoleMethods is deprecated in Rails 8 and will be removed
eventually, so let's just use this.
  • Loading branch information
rosa committed May 22, 2024
1 parent f086763 commit f9bd357
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 17 deletions.
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PATH
remote: .
specs:
console1984 (0.1.31)
irb (~> 1.13)
parser
rails (>= 7.0)
rainbow
Expand Down Expand Up @@ -98,6 +99,10 @@ GEM
activesupport (>= 6.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.13.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.6.3)
language_server-protocol (3.17.0.3)
loofah (2.22.0)
Expand Down Expand Up @@ -135,6 +140,8 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.3)
psych (5.1.2)
stringio
racc (1.7.1)
rack (2.2.7)
rack-test (2.1.0)
Expand Down Expand Up @@ -169,7 +176,11 @@ GEM
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.1.0)
rdoc (6.6.3.1)
psych (>= 4.0.0)
regexp_parser (2.8.1)
reline (0.5.7)
io-console (~> 0.5)
rexml (3.2.5)
rubocop (1.54.1)
json (~> 2.3)
Expand Down Expand Up @@ -200,6 +211,7 @@ GEM
rubyzip (2.3.2)
sqlite3 (1.6.3)
mini_portile2 (~> 2.8.0)
stringio (3.1.0)
thor (1.3.1)
timeout (0.4.1)
tzinfo (2.0.6)
Expand Down
1 change: 1 addition & 0 deletions console1984.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'rainbow'
spec.add_dependency 'parser'
spec.add_dependency 'rails', '>= 7.0'
spec.add_dependency 'irb', '~> 1.13'

spec.add_development_dependency 'benchmark-ips'
spec.add_development_dependency 'mocha'
Expand Down
2 changes: 1 addition & 1 deletion lib/console1984/command_validator/.command_parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Naming class with dot so that it doesn't get loaded eagerly by Zeitwork. We want to load
# Naming class with dot so that it doesn't get loaded eagerly by Zeitwerk. We want to load
# only when a console session is started, when +parser+ is loaded.
#
# See +Console1984::Supervisor#require_dependencies+
Expand Down
12 changes: 12 additions & 0 deletions lib/console1984/commands/decrypt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Console1984::Commands
class Decrypt < IRB::Command::Base
include Console1984::Ext::Irb::Commands

category "Console1984"
description "go back to protected mode, without access to encrypted information"

def execute(*)
decrypt!
end
end
end
12 changes: 12 additions & 0 deletions lib/console1984/commands/encrypt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Console1984::Commands
class Encrypt < IRB::Command::Base
include Console1984::Ext::Irb::Commands

category "Console1984"
description "go back to protected mode, without access to encrypted information"

def execute(*)
encrypt!
end
end
end
9 changes: 0 additions & 9 deletions lib/console1984/ext/irb/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,4 @@ def decrypt!
def encrypt!
shield.enable_protected_mode
end

# This returns the last error that prevented a command execution in the console
# or nil if there isn't any.
#
# This is meant for internal usage when debugging legit commands that are wrongly
# prevented.
def _console_last_suspicious_command_error
Console1984.command_executor.last_suspicious_command_error
end
end
3 changes: 2 additions & 1 deletion lib/console1984/shield.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def extend_protected_systems

def extend_irb
IRB::Context.prepend(Console1984::Ext::Irb::Context)
Rails::ConsoleMethods.include(Console1984::Ext::Irb::Commands)
IRB::Command.register :decrypt!, Console1984::Commands::Decrypt
IRB::Command.register :encrypt!, Console1984::Commands::Encrypt
end

def extend_core_ruby
Expand Down
6 changes: 0 additions & 6 deletions lib/console1984/supervisor.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
require "active_support/all"

if Rails.version >= "8"
require "rails/console/methods"
else
require "rails/console/app"
end

# Entry point to the system. In charge of installing everything
# and starting and stopping sessions.
class Console1984::Supervisor
Expand Down

0 comments on commit f9bd357

Please sign in to comment.