@@ -10,6 +10,7 @@ use helpers::generic_input;
1010use helpers:: generic_select;
1111use idf_im_lib:: get_log_directory;
1212use idf_im_lib:: settings:: Settings ;
13+ use idf_im_lib:: utils:: is_valid_idf_directory;
1314use idf_im_lib:: version_manager:: remove_single_idf_version;
1415use idf_im_lib:: version_manager:: select_idf_version;
1516use log:: debug;
@@ -353,7 +354,7 @@ pub async fn run_cli(cli: Cli) -> anyhow::Result<()> {
353354 println ! ( "Available versions:" ) ;
354355 let options = versions. iter ( ) . map ( |v| v. name . clone ( ) ) . collect ( ) ;
355356 match generic_select ( "Which version do you want to remove?" , & options) {
356- Ok ( selected) => match remove_single_idf_version ( & selected) {
357+ Ok ( selected) => match remove_single_idf_version ( & selected, false ) {
357358 Ok ( _) => {
358359 info ! ( "Removed version: {}" , selected) ;
359360 Ok ( ( ) )
@@ -367,7 +368,7 @@ pub async fn run_cli(cli: Cli) -> anyhow::Result<()> {
367368 Err ( err) => Err ( anyhow:: anyhow!( err) ) ,
368369 }
369370 } else {
370- match remove_single_idf_version ( & version. clone ( ) . unwrap ( ) ) {
371+ match remove_single_idf_version ( & version. clone ( ) . unwrap ( ) , false ) {
371372 Ok ( _) => {
372373 println ! ( "Removed version: {}" , version. clone( ) . unwrap( ) ) ;
373374 Ok ( ( ) )
@@ -388,7 +389,7 @@ pub async fn run_cli(cli: Cli) -> anyhow::Result<()> {
388389 let mut failed = false ;
389390 for version in versions {
390391 info ! ( "Removing version: {}" , version. name) ;
391- match remove_single_idf_version ( & version. name ) {
392+ match remove_single_idf_version ( & version. name , false ) {
392393 Ok ( _) => {
393394 info ! ( "Removed version: {}" , version. name) ;
394395 }
@@ -432,6 +433,89 @@ pub async fn run_cli(cli: Cli) -> anyhow::Result<()> {
432433 Err ( err) => Err ( anyhow:: anyhow!( err) ) ,
433434 }
434435 }
436+ Commands :: Fix { path } => {
437+ let path_to_fix = if path. is_some ( ) {
438+ // If a path is provided, fix the IDF installation at that path
439+ let path = path. unwrap ( ) ;
440+ if is_valid_idf_directory ( & path) {
441+ PathBuf :: from ( path)
442+ } else {
443+ error ! ( "Invalid IDF directory: {}" , path) ;
444+ return Err ( anyhow:: anyhow!( "Invalid IDF directory: {}" , path) ) ;
445+ }
446+ } else {
447+ match idf_im_lib:: version_manager:: list_installed_versions ( ) {
448+ Ok ( versions) => {
449+ if versions. is_empty ( ) {
450+ warn ! ( "No versions installed" ) ;
451+ return Ok ( ( ) ) ;
452+ } else {
453+ let options = versions. iter ( ) . map ( |v| v. path . clone ( ) ) . collect ( ) ;
454+ let version_path = match helpers:: generic_select (
455+ "Which version do you want to fix?" ,
456+ & options,
457+ ) {
458+ Ok ( selected) => selected,
459+ Err ( err) => {
460+ error ! ( "Error: {}" , err) ;
461+ return Err ( anyhow:: anyhow!( err) ) ;
462+ }
463+ } ;
464+ PathBuf :: from ( version_path)
465+ }
466+ }
467+ Err ( err) => {
468+ debug ! ( "Error: {}" , err) ;
469+ return Err ( anyhow:: anyhow!( "No versions found. Use eim install to install a new ESP-IDF version." ) ) ;
470+ }
471+ }
472+ } ;
473+ info ! ( "Fixing IDF installation at path: {}" , path_to_fix. display( ) ) ;
474+ // The fix logic is just instalation with use of existing repository
475+ let mut version_name = None ;
476+ match idf_im_lib:: version_manager:: list_installed_versions ( ) {
477+ Ok ( versions) => {
478+ for v in versions {
479+ if v. path == path_to_fix. to_str ( ) . unwrap ( ) {
480+ info ! ( "Found existing IDF version: {}" , v. name) ;
481+ // Remove the existing activation script and eim_idf.json entry
482+ match remove_single_idf_version ( & v. name , true ) {
483+ Ok ( _) => {
484+ info ! ( "Removed existing IDF version from eim_idf.json: {}" , v. name) ;
485+ version_name = Some ( v. name . clone ( ) ) ;
486+ }
487+ Err ( err) => {
488+ error ! ( "Failed to remove existing IDF version {}: {}" , v. name, err) ;
489+ }
490+ }
491+ }
492+ }
493+ }
494+ Err ( _) => {
495+ info ! ( "Failed to list installed versions. Using default naming." ) ;
496+ }
497+ }
498+
499+ let mut settings = Settings :: default ( ) ;
500+ settings. path = Some ( path_to_fix. clone ( ) ) ;
501+ settings. non_interactive = Some ( true ) ;
502+ settings. version_name = version_name;
503+ settings. install_all_prerequisites = Some ( true ) ;
504+ settings. config_file_save_path = None ;
505+ let result = wizard:: run_wizzard_run ( settings) . await ;
506+ match result {
507+ Ok ( r) => {
508+ info ! ( "Fix result: {:?}" , r) ;
509+ info ! ( "Successfully fixed IDF installation at {}" , path_to_fix. display( ) ) ;
510+ }
511+ Err ( err) => {
512+ error ! ( "Failed to fix IDF installation: {}" , err) ;
513+ return Err ( anyhow:: anyhow!( err) ) ;
514+ }
515+ }
516+ info ! ( "Now you can start using IDF tools" ) ;
517+ Ok ( ( ) )
518+ }
435519 #[ cfg( feature = "gui" ) ]
436520 Commands :: Gui ( _install_args) => {
437521 #[ cfg( not( feature = "gui" ) ) ]
0 commit comments