1+ use std:: path:: Path ;
12use std:: path:: PathBuf ;
23
34use anyhow:: Context ;
@@ -11,6 +12,9 @@ use helpers::generic_select;
1112use idf_im_lib:: get_log_directory;
1213use idf_im_lib:: idf_config:: IdfConfig ;
1314use idf_im_lib:: settings:: Settings ;
15+ use idf_im_lib:: utils:: find_by_name_and_extension;
16+ use idf_im_lib:: utils:: parse_esp_idf_json;
17+ use idf_im_lib:: utils:: try_import_existing_idf;
1418use idf_im_lib:: version_manager:: remove_single_idf_version;
1519use idf_im_lib:: version_manager:: select_idf_version;
1620use log:: debug;
@@ -316,9 +320,73 @@ pub async fn run_cli(cli: Cli) -> anyhow::Result<()> {
316320 }
317321 }
318322 }
319- Commands :: Discover => {
323+ Commands :: Discover { path } => {
320324 info ! ( "Discovering available versions... (This can take couple of minutes)" ) ;
321- let idf_dirs = idf_im_lib:: version_manager:: find_esp_idf_folders ( "/tmp" ) ;
325+ // TODO: add warning and confirmation
326+ let path = path. unwrap_or_else ( || {
327+ let default_path = match std:: env:: consts:: OS {
328+ "windows" => {
329+ "C:\\ " . to_string ( )
330+ }
331+ _ => {
332+ "/" . to_string ( )
333+ }
334+ } ;
335+
336+
337+ debug ! ( "No path provided, using default: {}" , default_path) ;
338+ default_path
339+ } ) ;
340+ // first parse existing esp_idf.json (using parse_esp_idf_json) || previous VSCode installations
341+ info ! ( "Searching for esp_idf.json files..." ) ;
342+ let search_patch = Path :: new ( & path) ;
343+ let esp_idf_json_path = find_by_name_and_extension (
344+ search_patch,
345+ "esp_idf" ,
346+ "json" ,
347+ ) ;
348+ if esp_idf_json_path. is_empty ( ) {
349+ info ! ( "No esp_idf.json found" ) ;
350+ } else {
351+ info ! ( "Found {} esp_idf.json files:" , esp_idf_json_path. len( ) ) ;
352+ }
353+ for path in esp_idf_json_path {
354+ info ! ( "- {} " , & path) ;
355+ match parse_esp_idf_json ( & path) {
356+ Ok ( _) => {
357+ info ! ( "Parsed config: {:?}" , path) ;
358+ }
359+ Err ( err) => {
360+ info ! ( "Failed to parse esp_idf.json: {}" , err) ;
361+ }
362+ }
363+ }
364+ // second try to find tool_set_config.json (using parse_tool_set_config) || previous Eclipse installations
365+ info ! ( "Searching for tool_set_config.json files..." ) ;
366+ let tool_set_config_path = find_by_name_and_extension (
367+ search_patch,
368+ "tool_set_config" ,
369+ "json" ,
370+ ) ;
371+ if tool_set_config_path. is_empty ( ) {
372+ info ! ( "No tool_set_config.json found" ) ;
373+ } else {
374+ info ! ( "Found {} tool_set_config.json files:" , tool_set_config_path. len( ) ) ;
375+ }
376+ for path in tool_set_config_path {
377+ info ! ( "- {} " , & path) ;
378+ match idf_im_lib:: utils:: parse_tool_set_config ( & path) {
379+ Ok ( _) => {
380+ info ! ( "Parsed config: {:?}" , path) ;
381+ }
382+ Err ( err) => {
383+ info ! ( "Failed to parse tool_set_config.json: {}" , err) ;
384+ }
385+ }
386+ }
387+ // third try to find IDF directories (using find_esp_idf_folders) || previous instalation from cli
388+ info ! ( "Searching for any other IDF directories..." ) ;
389+ let idf_dirs = idf_im_lib:: version_manager:: find_esp_idf_folders ( & path) ;
322390 if idf_dirs. is_empty ( ) {
323391 info ! ( "No IDF directories found" ) ;
324392 } else {
@@ -349,6 +417,17 @@ pub async fn run_cli(cli: Cli) -> anyhow::Result<()> {
349417 }
350418
351419 }
420+ // TODO: ask the user to select which IDFs to add
421+ for p in paths_to_add {
422+ match try_import_existing_idf ( & p) {
423+ Ok ( _) => {
424+ info ! ( "Added to config: {}" , p) ;
425+ }
426+ Err ( err) => {
427+ error ! ( "Failed to add: {} - reason :{}" , p, err) ;
428+ }
429+ }
430+ }
352431 Ok ( ( ) )
353432 }
354433 Commands :: Import { path } => match path {
0 commit comments