@@ -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,63 @@ 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+ println ! ( "No apps installed" ) ;
252+ return Ok ( ( ) ) ;
253+ }
254+ match app_name {
255+ Some ( app_name) => {
256+ installed_apps
257+ . iter ( )
258+ . find ( |iter| iter. tbf_header . get_package_name ( ) . unwrap_or ( "" ) == app_name)
259+ . expect ( "Specified app is not installed" ) ;
260+ conn. uninstall_app ( Some ( app_name. to_string ( ) ) , None )
261+ . await
262+ . context ( "Failed to uninstall app." ) ?;
263+ }
264+ None => loop {
265+ let mut options: Vec < AppOption > = installed_apps
266+ . iter ( )
267+ . enumerate ( )
268+ . map ( |( i, app) | AppOption { index : i + 1 , app } )
269+ . collect ( ) ;
270+
271+ // Delete all option
272+ options. insert (
273+ 0 ,
274+ AppOption {
275+ index : 0 ,
276+ app : & installed_apps[ 0 ] ,
277+ } ,
278+ ) ;
279+ let selected =
280+ inquire:: Select :: new ( "Which app do you want to uninstall?" , options)
281+ . prompt ( )
282+ . context ( "No apps installed" )
283+ . unwrap ( ) ;
284+
285+ if inquire:: Select :: new (
286+ format ! ( "You chose {selected}" , ) . as_str ( ) ,
287+ [ "Cancel" , "Confirm" ] . to_vec ( ) ,
288+ )
289+ . prompt ( )
290+ . unwrap ( )
291+ == "Confirm"
292+ {
293+ conn. uninstall_app ( None , Some ( selected. index ) )
294+ . await
295+ . context ( "Failed to uninstall app" ) ?;
296+ break ;
297+ }
298+ } ,
299+ }
300+ }
243301 _ => {
244302 println ! ( "Could not run the provided subcommand." ) ;
245303 _ = make_cli ( ) . print_help ( ) ;
0 commit comments