module TrainSH::Mixin::BuiltInCommands
Constants
- BUILTIN_PREFIX
Public Instance Methods
builtin_commands()
click to toggle source
# 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)
click to toggle source
# File lib/trainsh/mixin/builtin_commands.rb, line 12 def builtincmd_clear_history(_args = nil) Readline::HISTORY.clear end
builtincmd_connect(url = nil)
click to toggle source
# 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)
click to toggle source
def builtincmd_copy(source = nil, destination = nil)
# TODO: Copy files between sessions
end
# File lib/trainsh/mixin/builtin_commands.rb, line 29 def builtincmd_detect(_args = nil) __detect end
builtincmd_download(remote_path = nil, local_path = nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/trainsh/mixin/builtin_commands.rb, line 67 def builtincmd_env(_args = nil) puts session.env end
builtincmd_history(_args = nil)
click to toggle source
# File lib/trainsh/mixin/builtin_commands.rb, line 89 def builtincmd_history(_args = nil) puts Readline::HISTORY.to_a end
builtincmd_host(_args = nil)
click to toggle source
# File lib/trainsh/mixin/builtin_commands.rb, line 93 def builtincmd_host(_args = nil) say session.host end
builtincmd_ping(_args = nil)
click to toggle source
# 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)
click to toggle source
# File lib/trainsh/mixin/builtin_commands.rb, line 102 def builtincmd_pwd(_args = nil) say session.pwd end
builtincmd_read(path = nil)
click to toggle source
# 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)
click to toggle source
# File lib/trainsh/mixin/builtin_commands.rb, line 106 def builtincmd_reconnect(_args = nil) session.reconnect end
builtincmd_session(session_id = nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
train_mutable?()
click to toggle source
# File lib/trainsh/mixin/builtin_commands.rb, line 149 def train_mutable? return true if session.respond_to?(:upload) say "Support for remote file modification needs at least Train #{::TrainSH::TRAIN_MUTABLE_VERSION} (is: #{::Train::VERSION})".red end