diff --git a/scripts/install.sh b/scripts/install.sh index fd39b9e..5fce87d 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -15,8 +15,10 @@ echo "#!/usr/bin/env sh" >> "$binary_path" echo "" >> "$binary_path" echo "path=\$($build_path \$@)" >> "$binary_path" echo "if [ \"\$path\" != \"\" ]; then" >> "$binary_path" +echo " echo \"moving to \$path\"" >> "$binary_path" echo " cd \"\$path\"" >> "$binary_path" echo " \$SHELL" >> "$binary_path" +echo " echo \"exiting from workspace \$path\"" >> "$binary_path" echo "fi" >> "$binary_path" chmod +x "$binary_path" echo "Installed!" diff --git a/src/configuration.rs b/src/configuration.rs index e05d5fb..3809d6b 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -1,9 +1,8 @@ -use ron; use serde::{Deserialize, Serialize}; use std::io; use std::{env, fs}; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug)] pub struct Workspace { pub name: String, pub path: String, @@ -18,7 +17,7 @@ pub struct Configuration { impl Configuration { pub fn new() -> Configuration { - if let Ok(configuration) = Self::load_file() { + if let Ok(configuration) = Self::new_from_file() { configuration } else { Configuration { @@ -27,24 +26,24 @@ impl Configuration { } } - pub fn load_file() -> Result { - Configuration::from_str(&fs::read_to_string(format!( - "{}{}", - env::var("HOME").unwrap(), - CONFIGURATION_FILE - ))?) - } + pub fn new_from_file() -> Result { + let mut path = String::new(); + path.push_str(&env::var("HOME").unwrap()); + path.push_str(CONFIGURATION_FILE); - pub fn save_file(&self) -> Result<(), io::Error> { - print!("{:#?}", Configuration::to_str(&self).unwrap()); - fs::write( - format!("{}{}", env::var("HOME").unwrap(), CONFIGURATION_FILE), - Configuration::to_str(&self).unwrap(), - ) + Configuration::from_str(&fs::read_to_string(path)?) } pub fn from_str(s: &str) -> Result { - ron::from_str::(s) + ron::from_str::(s) + } + + pub fn save_to_file(&self) -> Result<(), io::Error> { + let mut path = String::new(); + path.push_str(&env::var("HOME").unwrap()); + path.push_str(CONFIGURATION_FILE); + + fs::write(path, self.to_str().unwrap()) } pub fn to_str(&self) -> Result { diff --git a/src/main.rs b/src/main.rs index 763bb49..e09ab2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,14 @@ -use std::io; -use std::io::Write; - use clap::{Arg, Command}; mod configuration; mod workspace; -pub fn output(path: &str) { - io::stdout() - .write(format!("{}\n", path).as_bytes()) - .unwrap(); -} - fn main() { let command = Command::new("wk") .version("0.1.0") .about("WK is a CLI tool to create, manager and access workspaces") .author("Henry Barreto ") - .arg(Arg::new("workspace").help("Workspace's name").index(1)) + .arg(Arg::new("workspace").help("Workspaces name").index(1)) .arg( Arg::new("save") .short('s') @@ -39,8 +30,8 @@ fn main() { let matches = command.get_matches(); if matches.is_present("workspace") { let workspace = matches.value_of("workspace").unwrap(); - if let Some(path) = workspace::go(workspace) { - output(&path); + if let Some(found) = workspace::go(workspace) { + print!("{}", found.path); } } else if matches.is_present("save") { let save = matches diff --git a/src/workspace.rs b/src/workspace.rs index 1bb8538..486b4fb 100644 --- a/src/workspace.rs +++ b/src/workspace.rs @@ -1,32 +1,33 @@ -use crate::configuration::Configuration; -use crate::configuration::Workspace as WorkspaceConfiguration; +use crate::configuration::{Configuration, Workspace}; -pub fn go(name: &str) -> Option { - for workspace in Configuration::new().workspaces.iter() { - if workspace.name == name { - return Some(workspace.path.clone()); - } +pub fn go(name: &str) -> Option { + if let Ok(configuration) = Configuration::new_from_file() { + configuration + .workspaces + .iter() + .find(|workspace| workspace.name == name) + .cloned() + } else { + None } - - return None; } pub fn save(name: &str, path: &str) { - let mut found: bool = false; - let mut configuration = Configuration::new(); - for workspace in configuration.workspaces.iter() { - if workspace.name == name { - found = true; - } - } + if let Ok(mut configuration) = Configuration::new_from_file() { + configuration.workspaces.push(Workspace { + name: name.to_string(), + path: path.to_string(), + }); - if !found { - configuration.workspaces.push(WorkspaceConfiguration { + configuration.save_to_file().unwrap(); + } else { + let mut configuration = Configuration::new(); + configuration.workspaces.push(Workspace { name: name.to_string(), path: path.to_string(), }); - configuration.save_file().unwrap(); + configuration.save_to_file().unwrap(); } }