Skip to content

Commit

Permalink
Initial edit implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Dec 11, 2019
1 parent 5c466ae commit 9e03780
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ authors = ["Brian Pearce"]
[workspace]
members = [
"common",
"edit",
"load",
"new",
"snapshot"
]

[dependencies]
common = { path = "./common" }
edit = { path = "./edit" }
load = { path = "./load" }
new = { path = "./new" }
snapshot = { path = "./snapshot" }
Expand Down
2 changes: 2 additions & 0 deletions common/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -21,6 +22,7 @@ pub struct Args {
pub flag_t: Option<String>,
pub flag_v: bool,
pub arg_project: String,
pub cmd_edit: bool,
pub cmd_new: bool,
pub cmd_snapshot: bool,
}
16 changes: 16 additions & 0 deletions edit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
55 changes: 55 additions & 0 deletions edit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf, String> {
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<PathBuf, String> {
Ok(PathBuf::from("/tmp"))
}
56 changes: 56 additions & 0 deletions edit/tests/edit.rs
Original file line number Diff line number Diff line change
@@ -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::<u16>(), 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::<u16>());
let (project_path, muxed_path) = setup(&project_name);
let _ = new(&project_name, &muxed_path);
assert!(&project_path.exists());
cleanup(&project_path);
}
}
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extern crate docopt;

extern crate common;
extern crate edit;
extern crate load;
extern crate new;
extern crate snapshot;
Expand All @@ -27,6 +28,7 @@ macro_rules! try_or_err (
static USAGE: &str = "
Usage:
muxed [options] <project>
muxed edit [options] <project>
muxed new [options] <project>
muxed snapshot [options] <project>
muxed (-h | --help)
Expand All @@ -47,6 +49,7 @@ Args:
<project> The name of your project to open
Subcommands:
edit <project> Edit an existing project file
new <project> To create a new project file
snapshot -t session <project> Capture a running session and create a config file for it
";
Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit 9e03780

Please sign in to comment.