Skip to content

Commit

Permalink
using a functional approach to go and to save the workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybarreto committed Nov 20, 2022
1 parent 9f22f2e commit e61ad42
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
2 changes: 2 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
33 changes: 16 additions & 17 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand All @@ -27,24 +26,24 @@ impl Configuration {
}
}

pub fn load_file() -> Result<Configuration, ron::error::Error> {
Configuration::from_str(&fs::read_to_string(format!(
"{}{}",
env::var("HOME").unwrap(),
CONFIGURATION_FILE
))?)
}
pub fn new_from_file() -> Result<Configuration, ron::error::Error> {
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<Configuration, ron::error::Error> {
ron::from_str::<Configuration>(s)
ron::from_str::<Self>(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<String, ron::error::Error> {
Expand Down
15 changes: 3 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>")
.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')
Expand All @@ -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
Expand Down
39 changes: 20 additions & 19 deletions src/workspace.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
for workspace in Configuration::new().workspaces.iter() {
if workspace.name == name {
return Some(workspace.path.clone());
}
pub fn go(name: &str) -> Option<Workspace> {
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();
}
}

Expand Down

0 comments on commit e61ad42

Please sign in to comment.