2022-09-14 21:45:47 +00:00
/*
* Vento , a CLI inventory for your files .
* Copyright ( C ) 2022 Lux Aliaga
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https ://www.gnu.org/licenses/>.
*
* /
2022-09-14 22:45:28 +00:00
use super ::common ;
2022-09-19 03:23:54 +00:00
use anyhow ::{ bail , Context , Result } ;
2022-09-17 08:44:25 +00:00
use colored ::Colorize ;
2022-09-17 21:33:40 +00:00
use size_format ::SizeFormatterBinary ;
2022-09-17 08:44:25 +00:00
use std ::io ::{ self , Write } ;
use std ::path ::{ Path , PathBuf } ;
use std ::{ fs , process } ;
2022-09-19 03:23:54 +00:00
pub fn init ( ) -> Result < ( ) > {
2022-09-17 08:44:25 +00:00
// Initializes Vento
2022-09-19 03:23:54 +00:00
let ventodir = & common ::env_config ( ) ? [ 0 ] ;
2022-09-14 12:36:34 +00:00
2022-09-17 08:44:25 +00:00
if ventodir . is_dir ( ) {
// Checks if Vento has already been initialized and prompts the user if they want to initialize it again
2022-09-14 12:36:34 +00:00
let mut answer = String ::new ( ) ;
print! ( " ⚠️ {} {} " , format! ( " WARNING: " ) . bold ( ) . red ( ) , " Vento has already been initialized. Reinitializing will delete all files on the directory for Vento. Do you wish to proceed? (y/N) " ) ;
let _ = io ::stdout ( ) . flush ( ) ;
io ::stdin ( )
. read_line ( & mut answer )
. expect ( " ❌ Failed to read input " ) ;
match answer . as_str ( ) . trim ( ) {
2022-09-19 03:23:54 +00:00
" y " | " Y " = > fs ::remove_dir_all ( & ventodir ) ? ,
_ = > process ::exit ( 0 ) ,
2022-09-14 12:36:34 +00:00
} ;
} ;
2022-09-17 08:44:25 +00:00
2022-09-19 03:23:54 +00:00
create_slots ( ) ? ;
Ok ( ( ) )
2022-09-14 12:36:34 +00:00
}
2022-09-19 03:23:54 +00:00
pub fn list ( slot : & str , dir : & str ) -> Result < ( ) > {
2022-09-17 08:44:25 +00:00
// Lists files in inventory
2022-09-18 03:27:31 +00:00
let mut slotdir : PathBuf = match slot {
2022-09-19 03:23:54 +00:00
" active " | " a " = > common ::env_config ( ) ? [ 1 ] . clone ( ) ,
" inactive " | " i " = > common ::env_config ( ) ? [ 2 ] . clone ( ) ,
2022-09-17 08:44:25 +00:00
_ = > PathBuf ::new ( ) ,
} ;
2022-09-14 12:36:34 +00:00
2022-09-18 03:27:31 +00:00
if dir ! = " " {
slotdir = [ & slotdir , & Path ::new ( dir ) . to_path_buf ( ) ] . iter ( ) . collect ( ) ;
}
if dir . to_string ( ) . contains ( " .. " ) {
2022-09-19 03:23:54 +00:00
bail! ( " ❌ {} " , format! ( " Cannot access parent. " ) . red ( ) ) ;
// process::exit(1);
2022-09-18 03:27:31 +00:00
}
2022-09-17 08:44:25 +00:00
if slotdir . is_dir ( ) {
2022-09-18 01:53:44 +00:00
if fs ::read_dir ( & slotdir ) . unwrap ( ) . count ( ) = = 0 {
println! (
" 🗃️ {} " ,
format! (
2022-09-18 03:27:31 +00:00
" No files in {}{}. " ,
2022-09-18 01:53:44 +00:00
match slot {
" active " = > format! ( " {} " , slot ) . bold ( ) ,
" inactive " | _ = > format! ( " {} " , slot ) . blue ( ) . bold ( ) ,
2022-09-18 03:27:31 +00:00
} ,
if dir ! = " " {
if cfg! ( windows ) {
format! ( " \\ {} " , dir . to_string ( ) )
} else {
format! ( " / {} " , dir . to_string ( ) )
}
} else {
" " . to_string ( )
2022-09-18 01:53:44 +00:00
}
)
. green ( )
) ;
} else {
// Checks if inventory selected exists
2022-09-17 08:44:25 +00:00
println! (
2022-09-18 01:53:44 +00:00
" 🗃️ {} " ,
2022-09-17 21:33:40 +00:00
format! (
2022-09-18 03:27:31 +00:00
" Files in {}{} ({}): " ,
2022-09-18 01:53:44 +00:00
match slot {
" active " = > format! ( " {} " , slot ) . bold ( ) ,
" inactive " | _ = > format! ( " {} " , slot ) . blue ( ) . bold ( ) ,
} ,
2022-09-18 03:27:31 +00:00
if dir ! = " " {
if cfg! ( windows ) {
format! ( " \\ {} " , dir . to_string ( ) )
} else {
format! ( " / {} " , dir . to_string ( ) )
}
} else {
" inventory " . to_string ( )
} ,
2022-09-18 01:53:44 +00:00
format! ( " {} " , fs ::read_dir ( & slotdir ) . unwrap ( ) . count ( ) )
. white ( )
. bold ( )
2022-09-17 21:33:40 +00:00
)
2022-09-18 01:53:44 +00:00
. green ( )
2022-09-17 08:44:25 +00:00
) ;
2022-09-18 01:53:44 +00:00
for file in fs ::read_dir ( & slotdir ) . unwrap ( ) {
let file = file . unwrap ( ) . path ( ) ;
println! (
2022-09-18 03:35:50 +00:00
" - [{}] {}{} " ,
2022-09-18 01:53:44 +00:00
if file . clone ( ) . is_dir ( ) {
format! ( " D " ) . blue ( )
} else if file . clone ( ) . is_symlink ( ) {
format! ( " S " ) . yellow ( )
} else {
format! ( " F " ) . green ( )
} ,
file . clone ( )
. file_name ( )
. unwrap ( )
. to_os_string ( )
. into_string ( )
. unwrap ( ) ,
2022-09-18 03:35:50 +00:00
if file . clone ( ) . is_file ( ) {
format! (
" ({}B) " ,
SizeFormatterBinary ::new ( file . clone ( ) . metadata ( ) . unwrap ( ) . len ( ) )
)
} else {
format! ( " " )
}
2022-09-18 01:53:44 +00:00
) ;
}
2022-09-17 08:44:25 +00:00
}
2022-09-14 19:33:19 +00:00
} else {
2022-09-17 08:44:25 +00:00
println! (
" ❌ {} " ,
format! (
2022-09-18 03:27:31 +00:00
" No such slot or directory. Valid slots are {} and {}. " ,
format! ( " active " ) . green ( ) . bold ( ) ,
format! ( " inactive " ) . blue ( ) . bold ( )
2022-09-17 08:44:25 +00:00
)
. red ( )
) ;
2022-09-14 19:33:19 +00:00
}
2022-09-19 03:23:54 +00:00
Ok ( ( ) )
2022-09-14 12:36:34 +00:00
}
2022-09-19 03:23:54 +00:00
pub fn switch ( ) -> Result < ( ) > {
2022-09-17 08:44:25 +00:00
// Switches between inventory slots
2022-09-19 03:23:54 +00:00
let ventodir = & common ::env_config ( ) ? [ 0 ] ;
let active = & common ::env_config ( ) ? [ 1 ] ;
let inactive = & common ::env_config ( ) ? [ 2 ] ;
2022-09-17 08:44:25 +00:00
let temp : PathBuf = [ ventodir . to_path_buf ( ) , Path ::new ( " temp " ) . to_path_buf ( ) ]
. iter ( )
. collect ( ) ;
fs ::rename ( & active , & temp )
. expect ( " ❌ Vento was unable to switch slots. Try running vento init and try again " ) ;
fs ::rename ( & inactive , & active )
. expect ( " ❌ Vento was unable to switch slots. Try running vento init and try again " ) ;
fs ::rename ( & temp , & inactive )
. expect ( " ❌ Vento was unable to switch slots. Try running vento init and try again " ) ;
2022-09-14 12:36:34 +00:00
println! ( " 🎉 {} " , format! ( " Switched inventory slots! " ) . green ( ) ) ;
2022-09-19 03:23:54 +00:00
Ok ( ( ) )
2022-09-14 12:36:34 +00:00
}
2022-09-19 03:23:54 +00:00
fn create_slots ( ) -> Result < ( ) > {
2022-09-17 08:44:25 +00:00
// Used only on init. Creates all required directories.
2022-09-19 03:23:54 +00:00
let active = & common ::env_config ( ) ? [ 1 ] ;
let inactive = & common ::env_config ( ) ? [ 2 ] ;
2022-09-14 12:36:34 +00:00
2022-09-17 08:44:25 +00:00
fs ::create_dir_all ( active )
2022-09-19 03:23:54 +00:00
. context ( " ❌ Vento was unable to initalize. Do you have the correct permissions? " ) ? ;
2022-09-17 08:44:25 +00:00
fs ::create_dir_all ( inactive )
2022-09-19 03:23:54 +00:00
. context ( " ❌ Vento was unable to initalize. Do you have the correct permissions? " ) ? ;
2022-09-14 12:36:34 +00:00
2022-09-17 08:44:25 +00:00
println! (
2022-09-19 03:23:54 +00:00
" 🎉 {} " , format! ( " Vento has been succesfully initialized! " ) . green ( )
2022-09-17 08:44:25 +00:00
) ;
2022-09-19 03:23:54 +00:00
Ok ( ( ) )
2022-09-14 22:11:42 +00:00
}