Skip to content

Commit 092790c

Browse files
committed
Add Command now supports option.
1 parent 952d790 commit 092790c

9 files changed

Lines changed: 83 additions & 41 deletions

File tree

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
## Your Shell Bookmark Manager.
44

55
```shell
6-
bm a/add <name> : Adds current directory to bookmarks.
7-
bm a/add <name> <directory> : Adds given directory to bookmarks.
8-
bm s/show : Show all bookmarks.
9-
bm s/show <name> : Show bookmark associated with given name.
10-
bm d/delete <name> : Delete bookmark with given name.
11-
bm c/config set key=value[,k=v] : Edit config values
12-
bm c/config get key[,key2] : Get config values
13-
bm h/help : Prints help text.
14-
bm --help : Prints help text.
6+
bm a/add <name> : Adds current directory to bookmarks.
7+
bm a/add <name> <directory> [option] : Adds given directory to bookmarks.
8+
bm s/show : Show all bookmarks.
9+
bm s/show <name> : Show bookmark associated with given name.
10+
bm d/delete <name> : Delete bookmark with given name.
11+
bm c/config set key=value[,k=v] : Edit config values
12+
bm c/config get key[,key2] : Get config values
13+
bm h/help : Prints help text.
14+
bm --help : Prints help text.
15+
```
16+
17+
### Add Options
18+
```bash
19+
-a, --add-anyway
20+
Adds path to bookmarks even if it does not exist
21+
1522
```
1623

src/commands.rs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,45 @@ impl Command {
4848
match &self.args {
4949
// No arguments
5050
None => {
51-
error::print_error("Command cannot be verified with given args".to_string(), error::ErrorCode::VerificationError);
51+
error::print_error_and_exit("Command cannot be verified with given args".to_string(), error::ErrorCode::VerificationError);
5252
return false;
53-
},
53+
}, // No command
5454
Some(params) => {
5555
match &self.type_of {
5656
CommandType::NONE => {
57-
error::print_error("Impossible command.".to_string(), error::ErrorCode::ImpossibleCommand);
57+
error::print_error_and_exit("Impossible command.".to_string(), error::ErrorCode::ImpossibleCommand);
5858
return false;
59-
}
59+
} // NONE
6060
CommandType::ADD => {
6161
let mut name : String = params[0].clone();
6262
let mut directory : String = std::env::current_dir().unwrap().to_string_lossy().to_string();
63-
// Directory is given
63+
let mut option = params.get(2);
64+
let mut addable = false;
65+
// Directory is given at least 2 params
6466
if params.len() > 1 {
6567
directory = params[1].clone();
6668
let canon_dir = PathBuf::from(directory);
67-
68-
println!("]]] {}", canon_dir.display().to_string());
69-
70-
if !canon_dir.exists() {
71-
println!("Path does NOT exists");
72-
}
73-
69+
// println!("]]] {}", canon_dir.display().to_string());
70+
match option {
71+
// No option is given, don't add if path does not exist
72+
None => {
73+
if !canon_dir.exists() {
74+
error::print_error_and_exit("Given path does not exist. Consider using -a option.".to_string(),
75+
error::ErrorCode::AddCommandPathNotFound);
76+
} else {
77+
addable = true;
78+
}
79+
}
80+
Some(opt) => match opt.as_str() {
81+
"-a" | "--add-anyway" => { addable = true; }
82+
_ => {
83+
error::print_error_and_exit("Given option is not recognized".to_string(),
84+
error::ErrorCode::AddCommandOptionMatchFailed);
85+
}
86+
},
87+
} // add command option
7488
directory = canon_dir.absolutize().unwrap().display().to_string();
75-
76-
89+
// TODO check this part
7790
// directory = fs::canonicalize(canon_dir).unwrap().as_path().display().to_string();
7891
// directory = fs::canonicalize(canon_dir).unwrap().display().to_string();
7992
// directory = directory[4..].to_string();
@@ -83,17 +96,21 @@ impl Command {
8396
println!("[1] -> {}", directory);
8497

8598
// let canon_dir = PathBuf::from(directory);
86-
store.insert(name, directory);
99+
100+
if addable {
101+
store.insert(name, directory);
102+
return true;
103+
}
87104

88105

89-
}
90-
CommandType::SHOW => {}
91-
CommandType::DELETE => {}
92-
CommandType::CONFIG => {}
93-
CommandType::HELP => {}
94-
}
95-
}
96-
}
106+
} // ADD
107+
CommandType::SHOW => {} // SHOW
108+
CommandType::DELETE => {} // DELETE
109+
CommandType::CONFIG => {} // CONFIG
110+
CommandType::HELP => {} // HELP
111+
} // match command types
112+
} // Commands
113+
} // match
97114
false
98115
}
99116
}

src/file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod file_reader;
2+
pub mod file_writer;
23
pub mod dir;

src/file/dir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn create_dir_and_file_if_not_exist() -> String {
3535
return store_path.display().to_string();
3636
},
3737
None => {
38-
error::print_error(String::from("Home directory cannot be accessed."), error::ErrorCode::HomeDirAccess);
38+
error::print_error_and_exit(String::from("Home directory cannot be accessed."), error::ErrorCode::HomeDirAccess);
3939
String::from("") // Empty line, does not really do anything, added for compiler error
4040
}
4141
}

src/file/file_writer.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::fs;
2+
3+
// https://stackoverflow.com/a/31193386
4+
5+
///
6+
/// Reads contents of file as string
7+
///
8+
/// ## Arguments
9+
/// - `filepath` - String representation of store file's path.
10+
pub fn read_file(filepath : String) -> String {
11+
fs::read_to_string(filepath).expect("Unable to read file.")
12+
}

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ fn main() {
1919
println!("--> {:?}", store_map);
2020
let command = utils::startup::parse_options();
2121
command.print_command();
22-
command.execute(&mut store_map);
22+
let added = command.execute(&mut store_map);
23+
if added {
24+
25+
}
2326
println!("--> {:?}", store_map);
2427

2528

src/parser/toml_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn parse_string(toml_string : String) -> HashMap<String, String> {
2222
bm_map = bookmarks.into_iter().map(|bm| (bm.name_clone(), bm.dir_clone()) ).collect();
2323
},
2424
Err(e) => {
25-
print_error(format!("Store file parsing error: {:?}", e.to_string()), ErrorCode::StoreFileParseError);
25+
print_error_and_exit(format!("Store file parsing error: {:?}", e.to_string()), ErrorCode::StoreFileParseError);
2626
}
2727
}
2828
return bm_map;

src/utils/error.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ pub enum ErrorCode {
33
HomeDirAccess,
44
StoreFileParseError,
55
VerificationError,
6-
ImpossibleCommand
6+
ImpossibleCommand,
7+
AddCommandPathNotFound,
8+
AddCommandOptionMatchFailed
79
}
810

9-
pub fn print_error(error_msg : String, error_code : ErrorCode) {
11+
pub fn print_error_and_exit(error_msg : String, error_code : ErrorCode) {
1012
eprintln!("{}", error_msg);
1113
std::process::exit(error_code as i32);
1214
}

src/utils/startup.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ pub fn parse_options() -> Command {
8989
command = Command::new(CommandType::CONFIG, args_options);
9090
}
9191
Some(x) => {
92-
error::print_error(format!("unrecognized argument: {}\n", x),
93-
error::ErrorCode::UnrecognizedArgument);
92+
error::print_error_and_exit(format!("unrecognized argument: {}\n", x),
93+
error::ErrorCode::UnrecognizedArgument);
9494
}
9595
_ => {
9696
print_usage();
97-
error::print_error(String::from(""),
98-
error::ErrorCode::UnrecognizedArgument);
97+
error::print_error_and_exit(String::from(""),
98+
error::ErrorCode::UnrecognizedArgument);
9999
}
100100
}
101101
command

0 commit comments

Comments
 (0)