Skip to content

Commit f4ef5e6

Browse files
committed
Use new IRB extension API to add decrypt/encrypt commands
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.
1 parent f086763 commit f4ef5e6

File tree

8 files changed

+40
-17
lines changed

8 files changed

+40
-17
lines changed

Gemfile.lock

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PATH
1111
remote: .
1212
specs:
1313
console1984 (0.1.31)
14+
irb (~> 1.13)
1415
parser
1516
rails (>= 7.0)
1617
rainbow
@@ -98,6 +99,10 @@ GEM
9899
activesupport (>= 6.1)
99100
i18n (1.14.1)
100101
concurrent-ruby (~> 1.0)
102+
io-console (0.7.2)
103+
irb (1.13.1)
104+
rdoc (>= 4.0.0)
105+
reline (>= 0.4.2)
101106
json (2.6.3)
102107
language_server-protocol (3.17.0.3)
103108
loofah (2.22.0)
@@ -135,6 +140,8 @@ GEM
135140
ast (~> 2.4.1)
136141
racc
137142
pg (1.5.3)
143+
psych (5.1.2)
144+
stringio
138145
racc (1.7.1)
139146
rack (2.2.7)
140147
rack-test (2.1.0)
@@ -169,7 +176,11 @@ GEM
169176
zeitwerk (~> 2.5)
170177
rainbow (3.1.1)
171178
rake (13.1.0)
179+
rdoc (6.6.3.1)
180+
psych (>= 4.0.0)
172181
regexp_parser (2.8.1)
182+
reline (0.5.7)
183+
io-console (~> 0.5)
173184
rexml (3.2.5)
174185
rubocop (1.54.1)
175186
json (~> 2.3)
@@ -200,6 +211,7 @@ GEM
200211
rubyzip (2.3.2)
201212
sqlite3 (1.6.3)
202213
mini_portile2 (~> 2.8.0)
214+
stringio (3.1.0)
203215
thor (1.3.1)
204216
timeout (0.4.1)
205217
tzinfo (2.0.6)

console1984.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
2828
spec.add_dependency 'rainbow'
2929
spec.add_dependency 'parser'
3030
spec.add_dependency 'rails', '>= 7.0'
31+
spec.add_dependency 'irb', '~> 1.13'
3132

3233
spec.add_development_dependency 'benchmark-ips'
3334
spec.add_development_dependency 'mocha'

lib/console1984/command_validator/.command_parser.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Naming class with dot so that it doesn't get loaded eagerly by Zeitwork. We want to load
1+
# Naming class with dot so that it doesn't get loaded eagerly by Zeitwerk. We want to load
22
# only when a console session is started, when +parser+ is loaded.
33
#
44
# See +Console1984::Supervisor#require_dependencies+

lib/console1984/commands/decrypt.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Console1984::Commands
2+
class Decrypt < IRB::Command::Base
3+
include Console1984::Ext::Irb::Commands
4+
5+
category "Console1984"
6+
description "go back to protected mode, without access to encrypted information"
7+
8+
def execute(*)
9+
decrypt!
10+
end
11+
end
12+
end

lib/console1984/commands/encrypt.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Console1984::Commands
2+
class Encrypt < IRB::Command::Base
3+
include Console1984::Ext::Irb::Commands
4+
5+
category "Console1984"
6+
description "go back to protected mode, without access to encrypted information"
7+
8+
def execute(*)
9+
encrypt!
10+
end
11+
end
12+
end

lib/console1984/ext/irb/commands.rb

-9
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,4 @@ def decrypt!
1313
def encrypt!
1414
shield.enable_protected_mode
1515
end
16-
17-
# This returns the last error that prevented a command execution in the console
18-
# or nil if there isn't any.
19-
#
20-
# This is meant for internal usage when debugging legit commands that are wrongly
21-
# prevented.
22-
def _console_last_suspicious_command_error
23-
Console1984.command_executor.last_suspicious_command_error
24-
end
2516
end

lib/console1984/shield.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def extend_protected_systems
3434

3535
def extend_irb
3636
IRB::Context.prepend(Console1984::Ext::Irb::Context)
37-
Rails::ConsoleMethods.include(Console1984::Ext::Irb::Commands)
37+
IRB::Command.register :decrypt!, Console1984::Commands::Decrypt
38+
IRB::Command.register :encrypt!, Console1984::Commands::Encrypt
3839
end
3940

4041
def extend_core_ruby

lib/console1984/supervisor.rb

-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
require "active_support/all"
22

3-
if Rails.version >= "8"
4-
require "rails/console/methods"
5-
else
6-
require "rails/console/app"
7-
end
8-
93
# Entry point to the system. In charge of installing everything
104
# and starting and stopping sessions.
115
class Console1984::Supervisor

0 commit comments

Comments
 (0)