From 9e03780d277a4eba75b6fdea5a95dd029295290c Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Tue, 10 Dec 2019 00:24:21 +0100 Subject: [PATCH] Initial edit implementation --- Cargo.lock | 10 +++++++++ Cargo.toml | 2 ++ common/src/args.rs | 2 ++ edit/Cargo.toml | 16 +++++++++++++ edit/src/lib.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++ edit/tests/edit.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 ++++ 7 files changed, 145 insertions(+) create mode 100644 edit/Cargo.toml create mode 100644 edit/src/lib.rs create mode 100644 edit/tests/edit.rs diff --git a/Cargo.lock b/Cargo.lock index 6686b03..42a7da4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,16 @@ name = "dtoa" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "edit" +version = "0.7.2-development" +dependencies = [ + "common 0.7.2-development", + "dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "failure" version = "0.1.5" diff --git a/Cargo.toml b/Cargo.toml index 753db66..2051fe5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ authors = ["Brian Pearce"] [workspace] members = [ "common", + "edit", "load", "new", "snapshot" @@ -13,6 +14,7 @@ members = [ [dependencies] common = { path = "./common" } +edit = { path = "./edit" } load = { path = "./load" } new = { path = "./new" } snapshot = { path = "./snapshot" } diff --git a/common/src/args.rs b/common/src/args.rs index 8965bc2..771be40 100644 --- a/common/src/args.rs +++ b/common/src/args.rs @@ -9,6 +9,7 @@ /// `flag_t` the session to read from /// `flag_debug` run inline print statements for debugging /// `arg_project` the project file to read +/// `cmd_edit` /// `cmd_new` literally nothing /// `cmd_snapshot` not sure why I have these /// @@ -21,6 +22,7 @@ pub struct Args { pub flag_t: Option, pub flag_v: bool, pub arg_project: String, + pub cmd_edit: bool, pub cmd_new: bool, pub cmd_snapshot: bool, } diff --git a/edit/Cargo.toml b/edit/Cargo.toml new file mode 100644 index 0000000..29ee2cb --- /dev/null +++ b/edit/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "edit" +version = "0.7.2-development" +authors = ["Brian Pearce"] +publish = false + +[lib] +doctest = false + +[dependencies] +common = { path = "../common" } +dirs = "1.0.5" +libc = "0.2.21" + +[dev-dependencies] +rand = "0.3.15" diff --git a/edit/src/lib.rs b/edit/src/lib.rs new file mode 100644 index 0000000..4f3e579 --- /dev/null +++ b/edit/src/lib.rs @@ -0,0 +1,55 @@ +//! Muxednew. A Muxed project Template Generator +extern crate common; +extern crate dirs; +extern crate libc; +#[cfg(test)] +extern crate rand; + +use common::args::Args; + +#[cfg(not(test))] +use dirs::home_dir; +use libc::system; +#[cfg(test)] +use rand::random; +use std::ffi::CString; +#[cfg(test)] +use std::fs; +use std::io; +use std::path::PathBuf; + +static MUXED_FOLDER: &str = "muxed"; + +pub fn exec(args: Args) -> Result<(), io::Error> { + let home = homedir().expect("Can't find home dir"); + let default_dir = format!("{}/.{}", home.display(), MUXED_FOLDER); + let project_name = format!("{}.yml", &args.arg_project); + let muxed_dir = match args.flag_p { + Some(ref x) => x.as_str(), + _ => default_dir.as_str(), + }; + + let command = format!("{} {}/{}", "$EDITOR", muxed_dir, project_name); + let system_call = CString::new(command).unwrap(); + + unsafe { + system(system_call.as_ptr()); + }; + + Ok(()) +} + +/// Return the users homedir as a string. +#[cfg(not(test))] +fn homedir() -> Result { + match home_dir() { + Some(dir) => Ok(dir), + None => Err(String::from("We couldn't find your home directory.")), + } +} + +/// Return the temp dir as the users home dir during testing. +#[cfg(test)] +fn homedir() -> Result { + Ok(PathBuf::from("/tmp")) +} diff --git a/edit/tests/edit.rs b/edit/tests/edit.rs new file mode 100644 index 0000000..75906e4 --- /dev/null +++ b/edit/tests/edit.rs @@ -0,0 +1,56 @@ +//! The integration suite for inspecting sessions. + +extern crate common; +extern crate new; +extern crate rand; + +#[cfg(test)] +mod test { + mod new { + use common::args::Args; + use new; + use rand::random; + use std::fs; + use std::path::PathBuf; + + pub fn new(project: &str, project_root: &PathBuf) -> Result<(), String> { + let args = Args { + flag_debug: false, + flag_dryrun: false, + flag_d: true, + flag_v: false, + flag_f: false, + flag_p: Some(format!("{}", project_root.display())), + flag_t: None, + arg_project: project.to_string(), + cmd_new: false, + cmd_snapshot: false, + }; + + new::exec(args) + } + + fn setup(project_name: &str) -> (PathBuf, PathBuf) { + let project_file = format!("/tmp/muxed_{}/{}.yml", random::(), project_name); + let project_path = PathBuf::from(&project_file); + + let m = project_path.clone(); + let muxed_path = project_path.parent().unwrap(); + (m, muxed_path.to_path_buf()) + } + + fn cleanup(config_path: &PathBuf) { + let _ = fs::remove_file(config_path); + let _ = fs::remove_dir(config_path.parent().unwrap()); + } + + #[test] + fn creates_new_file_muxed() { + let project_name = format!("muxed_int_test_{}", random::()); + let (project_path, muxed_path) = setup(&project_name); + let _ = new(&project_name, &muxed_path); + assert!(&project_path.exists()); + cleanup(&project_path); + } + } +} diff --git a/src/main.rs b/src/main.rs index 15bce6f..59229bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate docopt; extern crate common; +extern crate edit; extern crate load; extern crate new; extern crate snapshot; @@ -27,6 +28,7 @@ macro_rules! try_or_err ( static USAGE: &str = " Usage: muxed [options] + muxed edit [options] muxed new [options] muxed snapshot [options] muxed (-h | --help) @@ -47,6 +49,7 @@ Args: The name of your project to open Subcommands: + edit Edit an existing project file new To create a new project file snapshot -t session Capture a running session and create a config file for it "; @@ -87,6 +90,7 @@ pub fn main() { if let Some(x) = input.nth(1) { match x.as_ref() { + "edit" => try_or_err!(edit::exec(args)), "new" => try_or_err!(new::exec(args)), "snapshot" => try_or_err!(snapshot::exec(args)), _ => try_or_err!(load::exec(args)),