Module: TrainSH::Mixin::BuiltInCommands
- Defined in:
- lib/trainsh/mixin/builtin_commands.rb
Constant Summary collapse
- BUILTIN_PREFIX =
'builtincmd_'.freeze
Instance Method Summary collapse
- #builtin_commands ⇒ Object
- #builtincmd_clear_history(_args = nil) ⇒ Object
- #builtincmd_connect(url = nil) ⇒ Object
-
#builtincmd_detect(_args = nil) ⇒ Object
def builtincmd_copy(source = nil, destination = nil) # TODO: Copy files between sessions end.
- #builtincmd_download(remote_path = nil, local_path = nil) ⇒ Object
- #builtincmd_edit(path = nil) ⇒ Object
- #builtincmd_env(_args = nil) ⇒ Object
- #builtincmd_history(_args = nil) ⇒ Object
- #builtincmd_host(_args = nil) ⇒ Object
- #builtincmd_ping(_args = nil) ⇒ Object
- #builtincmd_pwd(_args = nil) ⇒ Object
- #builtincmd_read(path = nil) ⇒ Object
- #builtincmd_reconnect(_args = nil) ⇒ Object
- #builtincmd_session(session_id = nil) ⇒ Object
- #builtincmd_sessions(_args = nil) ⇒ Object
- #builtincmd_upload(local_path = nil, remote_path = nil) ⇒ Object
Instance Method Details
#builtin_commands ⇒ Object
8 9 10 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 8 def builtin_commands methods.sort.filter { |method| method.to_s.start_with? BUILTIN_PREFIX }.map { |method| method.to_s.delete_prefix BUILTIN_PREFIX } end |
#builtincmd_clear_history(_args = nil) ⇒ Object
12 13 14 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 12 def builtincmd_clear_history(_args = nil) Readline::HISTORY.clear end |
#builtincmd_connect(url = nil) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 16 def builtincmd_connect(url = nil) if url.nil? || url.strip.empty? say 'Expecting session url, e.g. `!connect docker://123456789abcdef0`'.red return false end use_session(url) end |
#builtincmd_detect(_args = nil) ⇒ Object
def builtincmd_copy(source = nil, destination = nil)
# TODO: Copy files between sessions
end
29 30 31 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 29 def builtincmd_detect(_args = nil) __detect end |
#builtincmd_download(remote_path = nil, local_path = nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 33 def builtincmd_download(remote_path = nil, local_path = nil) if remote_path.nil? || local_path.nil? say 'Expecting remote path and local path, e.g. `!download /etc/passwd /home/ubuntu`' return false end return unless train_mutable? session.download(remote_path, local_path) rescue ::Train::NotImplementedError say 'Backend for session does not implement download operation'.red end |
#builtincmd_edit(path = nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 46 def builtincmd_edit(path = nil) if path.nil? || path.strip.empty? say 'Expecting remote path, e.g. `!less /tmp/somefile.txt`'.red return false end tempfile = read_file(path) localeditor = ENV['EDITOR'] || ENV['VISUAL'] || 'vi' # TODO: configuration, Windows, ... say format('Using local editor `%<editor>s` for %<tempfile>s', editor: localeditor, tempfile: tempfile.path) system("#{localeditor} #{tempfile.path}") new_content = File.read(tempfile.path) write_file(path, new_content) tempfile.unlink rescue ::Train::NotImplementedError say 'Backend for session does not implement file operations'.red end |
#builtincmd_env(_args = nil) ⇒ Object
67 68 69 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 67 def builtincmd_env(_args = nil) puts session.env end |
#builtincmd_history(_args = nil) ⇒ Object
89 90 91 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 89 def builtincmd_history(_args = nil) puts Readline::HISTORY.to_a end |
#builtincmd_host(_args = nil) ⇒ Object
93 94 95 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 93 def builtincmd_host(_args = nil) say session.host end |
#builtincmd_ping(_args = nil) ⇒ Object
97 98 99 100 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 97 def builtincmd_ping(_args = nil) session.run_idle say format('Ping: %<ping>dms', ping: session.ping) end |
#builtincmd_pwd(_args = nil) ⇒ Object
102 103 104 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 102 def builtincmd_pwd(_args = nil) say session.pwd end |
#builtincmd_read(path = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 71 def builtincmd_read(path = nil) if path.nil? || path.strip.empty? say 'Expecting remote path, e.g. `!read /tmp/somefile.txt`'.red return false end tempfile = read_file(path) return false unless tempfile localpager = ENV['PAGER'] || 'less' # TODO: configuration, Windows, ... say format('Using local pager `%<pager>s` for %<tempfile>s', pager: localpager, tempfile: tempfile.path) system("#{localpager} #{tempfile.path}") tempfile.unlink rescue ::NotImplementedError say 'Backend for session does not implement file operations'.red end |
#builtincmd_reconnect(_args = nil) ⇒ Object
106 107 108 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 106 def builtincmd_reconnect(_args = nil) session.reconnect end |
#builtincmd_session(session_id = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 118 def builtincmd_session(session_id = nil) session_id = validate_session_id(session_id) if session_id.nil? say 'Expecting valid session id, e.g. `!session 2`'.red return false end # TODO: Make this more pretty session_url = @sessions[session_id].url use_session(session_url) end |
#builtincmd_sessions(_args = nil) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 110 def builtincmd_sessions(_args = nil) say 'Active sessions:' @sessions.each_with_index do |session, idx| say format('[%<idx>d] %<session>s', idx: idx, session: session.url) end end |
#builtincmd_upload(local_path = nil, remote_path = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/trainsh/mixin/builtin_commands.rb', line 132 def builtincmd_upload(local_path = nil, remote_path = nil) if remote_path.nil? || local_path.nil? say 'Expecting remote path and local path, e.g. `!download /home/ubuntu/passwd /etc`' return false end return unless train_mutable? session.upload(local_path, remote_path) rescue ::Errno::ENOENT say "Local file/directory '#{local_path}' does not exist".red rescue ::NotImplementedError say 'Backend for session does not implement upload operation'.red end |