@@ -10,6 +10,7 @@ use anyhow::{Context, Result};
1010use clap:: ArgMatches ;
1111use cli:: make_cli;
1212use known_boards:: KnownBoardNames ;
13+ use tockloader_lib:: attributes:: app_attributes:: AppOption ;
1314use tockloader_lib:: board_settings:: BoardSettings ;
1415use tockloader_lib:: connection:: {
1516 Connection , ProbeRSConnection , ProbeTargetInfo , SerialConnection , SerialTargetInfo ,
@@ -19,7 +20,7 @@ use tockloader_lib::known_boards::KnownBoard;
1920use tockloader_lib:: tabs:: tab:: Tab ;
2021use tockloader_lib:: {
2122 list_debug_probes, list_serial_ports, CommandEraseApps , CommandInfo , CommandInstall ,
22- CommandList ,
23+ CommandList , CommandUninstall ,
2324} ;
2425
2526fn get_serial_target_info ( user_options : & ArgMatches ) -> SerialTargetInfo {
@@ -240,6 +241,65 @@ async fn main() -> Result<()> {
240241
241242 conn. erase_apps ( ) . await . context ( "Failed to erase apps." ) ?;
242243 }
244+ Some ( ( "uninstall" , sub_matches) ) => {
245+ cli:: validate ( & mut cmd, sub_matches) ;
246+ let mut conn = open_connection ( sub_matches) . await ?;
247+ let installed_apps = conn. list ( ) . await . context ( "Failed to list apps." ) ?;
248+ let app_name = sub_matches. get_one :: < String > ( "name" ) . map ( String :: as_str) ;
249+
250+ if installed_apps. is_empty ( ) {
251+ panic ! ( "No apps installed" ) ;
252+ }
253+ match app_name {
254+ Some ( app_name) => {
255+ match installed_apps
256+ . iter ( )
257+ . find ( |iter| iter. tbf_header . get_package_name ( ) . unwrap_or ( "" ) == app_name)
258+ {
259+ Some ( _) => conn
260+ . uninstall_app ( Some ( app_name. to_string ( ) ) , None )
261+ . await
262+ . context ( "Failed to uninstall app." ) ?,
263+ None => panic ! ( "Specified app is not installed!" ) ,
264+ }
265+ }
266+ None => loop {
267+ let mut options: Vec < AppOption > = installed_apps
268+ . iter ( )
269+ . enumerate ( )
270+ . map ( |( i, app) | AppOption { index : i + 1 , app } )
271+ . collect ( ) ;
272+
273+ // Delete all option
274+ options. insert (
275+ 0 ,
276+ AppOption {
277+ index : 0 ,
278+ app : & installed_apps[ 0 ] ,
279+ } ,
280+ ) ;
281+ let selected =
282+ inquire:: Select :: new ( "Which app do you want to uninstall?" , options)
283+ . prompt ( )
284+ . context ( "No apps installed" )
285+ . unwrap ( ) ;
286+
287+ if inquire:: Select :: new (
288+ format ! ( "You chose {selected}" , ) . as_str ( ) ,
289+ [ "Cancel" , "Confirm" ] . to_vec ( ) ,
290+ )
291+ . prompt ( )
292+ . unwrap ( )
293+ == "Confirm"
294+ {
295+ conn. uninstall_app ( None , Some ( selected. index ) )
296+ . await
297+ . context ( "Failed to uninstall app" ) ?;
298+ break ;
299+ }
300+ } ,
301+ }
302+ }
243303 _ => {
244304 println ! ( "Could not run the provided subcommand." ) ;
245305 _ = make_cli ( ) . print_help ( ) ;
0 commit comments