From 4c152d55edc20ac55fd749a2b32b204134a664e3 Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Dec 05 2018 17:03:08 +0000 Subject: Initial commit. --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8741fe0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +*beheer.json diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..fdbf543 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,37 @@ +[[package]] +name = "beheer" +version = "0.1.0" +dependencies = [ + "serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "itoa" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ryu" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_json" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" +"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" +"checksum serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)" = "15c141fc7027dd265a47c090bf864cf62b42c4d228bbcf4e51a0c9e2b0d3f7ef" +"checksum serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)" = "c37ccd6be3ed1fdf419ee848f7c758eb31b054d7cd3ae3600e3bae0adf569811" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..56011b3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "beheer" +version = "0.1.0" +authors = ["Jan Allersma "] + +[dependencies] +serde_json = "1.0" diff --git a/README.md b/README.md new file mode 100644 index 0000000..3f087ab --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Beheer diff --git a/src/arguments/config/mod.rs b/src/arguments/config/mod.rs new file mode 100644 index 0000000..7edf8e1 --- /dev/null +++ b/src/arguments/config/mod.rs @@ -0,0 +1,23 @@ +use std::io::{Result, Error, ErrorKind, Write}; +use std::fs::File; +use std::path::PathBuf; + +pub fn create(mut path: PathBuf) -> Result<()> { + let content = json!({ + "project-name": path.file_name().unwrap().to_str().unwrap(), + "version": 0.1 + }); + + path.push("beheer.json"); + + match File::open(path.to_str().unwrap()) { + Ok(_) => return Err(Error::new(ErrorKind::AlreadyExists, "Already found a 'beheer.json' file.")), + Err(_) => { + match File::create(path) { + Ok(mut file) => file.write_all(content.to_string().as_bytes())?, + Err(e) => return Err(e) + } + } + } + Ok(()) +} diff --git a/src/arguments/mod.rs b/src/arguments/mod.rs new file mode 100644 index 0000000..26ddd0f --- /dev/null +++ b/src/arguments/mod.rs @@ -0,0 +1,45 @@ +mod config; + +use std::env; + +pub fn parse() { + let mut argv = env::args(); + let _ = argv.next(); + let arg = argv.next(); + + match arg { + Some(argument) => { + if &argument == "--help" || &argument == "-h" { + show_help(); + } + else if &argument == "init" { + init(&mut argv); + } + }, + None => show_help() + } +} + +fn show_help() { + println!("Syntax:"); + println!("$ beheer [FLAG] [COMMAND [ARGUMENTS]]"); + println!(""); + + println!("--help -h\t\tShow this message"); + println!(""); + + println!("init [DIRECTORY]\tInitialize new project in specified directory. Defaults to current directory."); +} + +fn init(args: &mut env::Args) { + let mut directory = env::current_dir().unwrap(); + + if let Some(projectname) = args.next() { + directory.push(&projectname); + } + + match config::create(directory) { + Ok(_) => println!("Initialized project!"), + Err(e) => println!("Initializing project failed: {}", e) + } +} diff --git a/src/filesystem.rs b/src/filesystem.rs new file mode 100644 index 0000000..e61a3c6 --- /dev/null +++ b/src/filesystem.rs @@ -0,0 +1,38 @@ +use std::{path, env}; + +fn get_root (mut path: path::PathBuf) -> Option { + loop { + let mut config = path.clone(); + config.push("beheer.json"); + + if config.as_path().is_file() { + return Some(path); + } + + if !path.pop() { + return None; + } + } +} + +pub fn get_module_root() -> Option { + get_root(env::current_dir().unwrap()) +} + +pub fn get_project_root() -> Option { + let mut path = get_root(env::current_dir().unwrap()); + let mut parentdir = path.clone(); + + loop { + match parentdir { + Some(mut p) => { + path = Some(path::PathBuf::from(p.clone())); + if !p.pop() { + return path; + } + parentdir = get_root(p); + }, + None => return path + } + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..74833f9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,14 @@ +#[macro_use] +extern crate serde_json; + +mod filesystem; +mod arguments; + +fn main() { + match filesystem::get_project_root() { + Some(_) => println!("You are in a project."), + None => println!("You are not in a project.") + } + + arguments::parse(); +}