Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Pre-complied binaries for following platforms are available in [Releases page](h
### Features

- Support all options and rmate.rc settings described in [Ruby implementation](https://github.com/textmate/rmate).
- Connect over a UNIX socket (`-s`/`--unixsocket`, `RMATE_UNIXSOCKET`, or `unixsocket` in rmate.rc; default `~/.rmate.socket`). The socket takes precedence over host/port if the file exists. Forward it with SSH using `RemoteForward /path/to/.rmate.socket localhost:52698`: other users on a shared server cannot connect to the socket, as they can to a TCP port.
- Create backups of local files to avoid corrupting your files if any io/network operations go wrong.
- Verbose logging of operation (use `-v` one or more times).

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ fn main() -> Result<(), String> {
}
env_logger::init();

// Set host/port if user didn't specify in arguments and
// Set host/port/unixsocket if user didn't specify in arguments and
// we found them in one of rmate.rc files. Otherwise use
// default values.
let disk_settings = settings::read_disk_settings();
settings.host.get_or_insert(disk_settings.0);
settings.port.get_or_insert(disk_settings.1);
settings.unixsocket.get_or_insert(disk_settings.2);

// if --host auto is set in cmd line arguments, we try to find the host address from
// SSH_CONNECTION
Expand Down
19 changes: 18 additions & 1 deletion src/remote_editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::settings;
use log::{debug, info, trace};
use socket2::{Domain, Type};
use socket2::{Domain, SockAddr, Type};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Error, ErrorKind, Write};
Expand All @@ -9,6 +9,23 @@ use std::net::{IpAddr, Ipv4Addr};
pub(crate) fn connect_to_editor(
settings: &settings::Settings,
) -> Result<socket2::Socket, std::io::Error> {
// Like the Ruby rmate, the UNIX socket takes precedence over host/port if the file exists
let unixsocket = settings::expand_home(settings.unixsocket.as_ref().unwrap());
if unixsocket.exists() {
let socket = socket2::Socket::new(Domain::unix(), Type::stream(), None)?;
debug!("About to connect to UNIX socket {}", unixsocket.display());
socket.connect(&SockAddr::unix(&unixsocket)?)?;
trace!(
"Socket details: \n\tremote address {:?}",
socket.peer_addr()?
);
return Ok(socket);
}
trace!(
"No UNIX socket at {}, using host/port",
unixsocket.display()
);

let socket = socket2::Socket::new(Domain::ipv4(), Type::stream(), None).unwrap();

debug!("Host: {}", settings.host.as_ref().unwrap());
Expand Down
34 changes: 28 additions & 6 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use structopt::StructOpt;
pub(crate) const NO_TRIES_CREATE_BACKUP_FN: u8 = 5;
pub(crate) const RMATE_HOST: &str = "localhost";
pub(crate) const RMATE_PORT: u16 = 52698;
pub(crate) const RMATE_UNIXSOCKET: &str = "~/.rmate.socket";

// program settings from command-line arguments and environment variables
#[allow(clippy::struct_excessive_bools)]
Expand All @@ -32,6 +33,11 @@ pub(crate) struct Settings {
#[structopt(short, long, env = "RMATE_PORT", name = "PORT")]
pub port: Option<u16>,

/// UNIX socket path. Takes precedence over host/port if the file exists.
/// Defaults to ~/.rmate.socket
#[structopt(short = "s", long, env = "RMATE_UNIXSOCKET", name = "PATH")]
pub unixsocket: Option<String>,

#[allow(clippy::doc_markdown)]
/// Have *TextMate* retain window focus after file is closed
#[structopt(short, long)]
Expand Down Expand Up @@ -90,13 +96,17 @@ pub struct RcSettings {
pub(crate) unixsocket: Option<String>,
}

// Read host/settings from rmate.rc files
pub(crate) fn read_disk_settings() -> (String, u16) {
// Read host/port/unixsocket settings from rmate.rc files
pub(crate) fn read_disk_settings() -> (String, u16, String) {
trace!("Loading settings from rmate.rc files");
// Environment variables and command-line flags are handled by StructOpt in
// Settings::from_args(); main.rs then preserves those values with get_or_insert().
// This function only handles hardcoded defaults plus rc files.
let host_port = (self::RMATE_HOST.to_string(), self::RMATE_PORT);
let host_port = (
self::RMATE_HOST.to_string(),
self::RMATE_PORT,
self::RMATE_UNIXSOCKET.to_string(),
);
[
"/etc/rmate.rc",
"/usr/local/etc/rmate.rc",
Expand Down Expand Up @@ -149,23 +159,35 @@ pub(crate) fn read_disk_settings() -> (String, u16) {
trace!(" {:?}", s.as_ref().unwrap_err());
} else {
trace!(
" Read disk settings-> {{ host: {:?}\tport: {:?} }}",
" Read disk settings-> {{ host: {:?}\tport: {:?}\tunixsocket: {:?} }}",
s.as_ref().unwrap().host.as_ref(),
s.as_ref().unwrap().port.as_ref(),
s.as_ref().unwrap().unixsocket.as_ref(),
);
}
},
)
.filter(|(s, _)| s.is_ok())
.map(|(s, _)| s.unwrap())
.fold(host_port, |acc, item: self::RcSettings| {
let (mut newhost, mut newport) = acc;
let (mut newhost, mut newport, mut newsocket) = acc;
if let Some(host) = item.host {
newhost = host;
}
if let Some(port) = item.port {
newport = port;
}
(newhost, newport)
if let Some(unixsocket) = item.unixsocket {
newsocket = unixsocket;
}
(newhost, newport, newsocket)
})
}

// Replace a leading "~/" in a path with the home directory
pub(crate) fn expand_home(path: &str) -> PathBuf {
match (path.strip_prefix("~/"), dirs::home_dir()) {
(Some(rest), Some(home)) => home.join(rest),
_ => PathBuf::from(path),
}
}