Module: Lich::Common::CLI::ActiveSessionsQuery
- Defined in:
- documented/common/cli/active_sessions_query.rb
Class Method Summary collapse
-
.execute ⇒ Integer
private
Executes the active sessions query if requested.
-
.print_session_info(snapshot, session_name) ⇒ Integer
Prints detailed information about a specific active session.
-
.print_session_info_usage ⇒ void
Prints usage information for the session info command.
-
.print_snapshot(snapshot) ⇒ void
Prints the snapshot of active sessions in a table format.
-
.query_requested? ⇒ Boolean
Checks if an active sessions query has been requested.
-
.query_snapshot ⇒ Hash
Retrieves the current snapshot of active sessions.
-
.requested_session_name ⇒ String?
Retrieves the requested session name from command line arguments.
-
.run ⇒ Integer
Runs the active sessions query and prints the results.
Class Method Details
.execute ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Executes the active sessions query if requested.
15 16 17 18 19 |
# File 'documented/common/cli/active_sessions_query.rb', line 15 def self.execute return unless query_requested? exit run end |
.print_session_info(snapshot, session_name) ⇒ Integer
Prints detailed information about a specific active session.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'documented/common/cli/active_sessions_query.rb', line 103 def self.print_session_info(snapshot, session_name) if snapshot[:error] $stdout.puts "No active sessions service available (#{snapshot[:error]})." return 1 end session = Array(snapshot[:sessions]).find do |entry| entry[:session_name].to_s.casecmp?(session_name) end unless session $stdout.puts "No active session found for #{session_name}." return 1 end listener = session[:listener] $stdout.puts "Session: #{session[:session_name]}" $stdout.puts "PID: #{session[:pid]}" $stdout.puts "Role: #{session[:role] || 'session'}" $stdout.puts "Connected: #{yes_no(session[:connected])}" $stdout.puts "Detachable listener: #{listener_display(listener)}" $stdout.puts "Uptime: #{format_uptime(session[:uptime_seconds])}" 0 end |
.print_session_info_usage ⇒ void
This method returns an undefined value.
Prints usage information for the session info command.
130 131 132 133 134 135 |
# File 'documented/common/cli/active_sessions_query.rb', line 130 def self.print_session_info_usage lich_script = File.join(LICH_DIR, 'lich.rbw') $stdout.puts 'error: Missing session name' $stdout.puts "Usage: ruby #{lich_script} --session-info NAME" $stdout.puts " or: ruby #{lich_script} --session-info=NAME" end |
.print_snapshot(snapshot) ⇒ void
This method returns an undefined value.
Prints the snapshot of active sessions in a table format.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'documented/common/cli/active_sessions_query.rb', line 67 def self.print_snapshot(snapshot) if snapshot[:error] $stdout.puts "No active sessions service available (#{snapshot[:error]})." return end sessions = Array(snapshot[:sessions]) if sessions.empty? $stdout.puts 'No active sessions found.' return end rows = sessions.sort_by { |session| session[:session_name].to_s.downcase }.map do |session| [ session[:session_name] || '(unnamed)', session[:pid], session[:role] || 'session', yes_no(session[:connected]), session[:listener] ? yes_no(true) : yes_no(false), listener_display(session[:listener]), format_uptime(session[:uptime_seconds]) ] end table = Terminal::Table.new( title: 'Active Sessions', headings: ['Session', 'PID', 'Role', 'Connected', 'Detachable', 'Listener', 'Uptime'], rows: rows ) $stdout.puts table end |
.query_requested? ⇒ Boolean
Checks if an active sessions query has been requested.
40 41 42 |
# File 'documented/common/cli/active_sessions_query.rb', line 40 def self.query_requested? ARGV.include?('--active-sessions') || !requested_session_name.nil? end |
.query_snapshot ⇒ Hash
Retrieves the current snapshot of active sessions.
58 59 60 61 62 |
# File 'documented/common/cli/active_sessions_query.rb', line 58 def self.query_snapshot return unavailable_snapshot unless defined?(Lich::InternalAPI::ActiveSessions) Lich::InternalAPI::ActiveSessions.query_snapshot end |
.requested_session_name ⇒ String?
Retrieves the requested session name from command line arguments.
46 47 48 49 50 51 52 53 54 |
# File 'documented/common/cli/active_sessions_query.rb', line 46 def self.requested_session_name inline_arg = ARGV.find { |arg| arg.start_with?('--session-info=') } return inline_arg.split('=', 2).last if inline_arg idx = ARGV.index('--session-info') return nil unless idx ARGV[idx + 1] end |
.run ⇒ Integer
Runs the active sessions query and prints the results.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'documented/common/cli/active_sessions_query.rb', line 23 def self.run if ARGV.include?('--active-sessions') print_snapshot(query_snapshot) return 0 end session_name = requested_session_name if session_name.nil? || session_name.empty? print_session_info_usage return 1 end print_session_info(query_snapshot, session_name) end |