@@ -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}
0 commit comments