@@ -354,30 +354,31 @@ func versionPrint(commandNames ...string) {
354354 }
355355}
356356
357- // findSiblings returns the array of the siblings of the specified command.
358- func findSiblings (cmd * cobra.Command ) (siblings []* cobra.Command ) {
359- for _ , childCmd := range cmd .Parent ().Commands () {
360- if childCmd .Name () != "version" {
361- siblings = append (siblings , childCmd )
362- }
357+ func initViper () {
358+ logrus .Info ("Initiating viper config" )
359+
360+ defHome , err := common .GetDefaultArduinoHomeFolder ()
361+ if err != nil {
362+ logrus .WithError (err ).Warn ("Cannot get default Arduino Home" )
363+ }
364+ defArduinoData , err := common .GetDefaultArduinoFolder ()
365+ if err != nil {
366+ logrus .WithError (err ).Warn ("Cannot get default Arduino folder" )
363367 }
364- return
365- }
366368
367- func initViper () {
368- defHome , _ := common .GetDefaultArduinoHomeFolder ()
369- defArduinoData , _ := common .GetDefaultArduinoFolder ()
370369 viper .SetConfigName (".cli-config" )
371370 viper .AddConfigPath ("." )
372371 viper .SetConfigType ("yaml" )
373372
374- err := viper .ReadInConfig ()
373+ logrus .Info ("Reading configuration for viper" )
374+ err = viper .ReadInConfig ()
375375 if err != nil {
376- //formatter.PrintError (err)
376+ logrus . WithError (err ). Error ( "Cannot read configuration file in any of the default folders" )
377377 formatter .PrintErrorMessage ("Cannot read configuration file in any of the default folders" )
378378 os .Exit (errNoConfigFile )
379379 }
380380
381+ logrus .Info ("Setting defaults" )
381382 viper .SetDefault ("paths.sketchbook" , defHome )
382383 viper .SetDefault ("paths.arduino_data" , defArduinoData )
383384 viper .SetDefault ("proxy.type" , "auto" )
@@ -387,11 +388,13 @@ func initViper() {
387388
388389 viper .AutomaticEnv ()
389390
391+ logrus .Info ("Setting proxy" )
390392 if viper .GetString ("proxy.type" ) == "manual" {
391393 hostname := viper .GetString ("proxy.hostname" )
392394 if hostname == "" {
395+ logrus .Error ("With manual proxy configuration, hostname is required." )
393396 formatter .PrintErrorMessage ("With manual proxy configuration, hostname is required." )
394- os .Exit (2 )
397+ os .Exit (errCoreConfig )
395398 }
396399
397400 if strings .HasPrefix (hostname , "http" ) {
@@ -406,59 +409,73 @@ func initViper() {
406409
407410 }
408411 }
412+ logrus .Info ("Done viper configuration loading" )
409413}
410414
411415func executeLoginCommand (cmd * cobra.Command , args []string ) {
416+ logrus .Info ("Executing `arduino login`" )
417+
412418 userEmpty := arduinoLoginFlags .User == ""
413419 passwordEmpty := arduinoLoginFlags .Password == ""
414420 isTextMode := formatter .IsCurrentFormat ("text" )
415421 if ! isTextMode && (userEmpty || passwordEmpty ) {
422+ logrus .Error ("User and password must be specified outside of text format" )
416423 formatter .PrintErrorMessage ("User and password must be specified outside of text format" )
417424 return
418425 }
419426
427+ logrus .Info ("Using/Asking credentials" )
420428 if userEmpty {
421429 fmt .Print ("Username: " )
422430 fmt .Scanln (& arduinoLoginFlags .User )
423431 }
424432
425433 if passwordEmpty {
426434 fmt .Print ("Password: " )
427- pass , err := terminal .ReadPassword (int ( syscall .Stdin ) )
435+ pass , err := terminal .ReadPassword (syscall .Stdin )
428436 if err != nil {
437+ logrus .WithError (err ).Error ("Cannot read password, login aborted" )
429438 formatter .PrintErrorMessage ("Cannot read password, login aborted" )
430439 return
431440 }
432441 arduinoLoginFlags .Password = string (pass )
433442 fmt .Println ()
434443 }
435444
445+ logrus .Info ("Getting ~/.netrc file" )
446+
436447 //save into netrc
437448 netRCHome , err := homedir .Dir ()
438449 if err != nil {
439- formatter .PrintError (err )
450+ logrus .WithError (err ).Error ("Cannot get current home directory" )
451+ formatter .PrintErrorMessage ("Cannot get current home directory" )
440452 os .Exit (errGeneric )
441453 }
442454
443455 netRCFile := filepath .Join (netRCHome , ".netrc" )
444456 file , err := os .OpenFile (netRCFile , os .O_RDONLY | os .O_CREATE , 0666 )
445457 if err != nil {
446- formatter .PrintError (err )
458+ logrus .WithError (err ).Error ("Cannot cannot read .netrc file" )
459+ formatter .PrintErrorMessage ("Cannot cannot read .netrc file" )
447460 return
448461 }
449462 defer file .Close ()
450463 NetRC , err := netrc .Parse (file )
451464 if err != nil {
452- formatter .PrintError (err )
465+ logrus .WithError (err ).Error ("Cannot parse read .netrc file" )
466+ formatter .PrintErrorMessage ("Cannot parse read .netrc file" )
453467 os .Exit (errGeneric )
454468 }
455469
470+ logrus .Info ("Trying to login" )
471+
456472 usr := arduinoLoginFlags .User
457473 pwd := arduinoLoginFlags .Password
458474 authConf := auth .New ()
459475
460476 token , err := authConf .Token (usr , pwd )
461477 if err != nil {
478+ logrus .WithError (err ).Error ("Cannot login" )
462479 formatter .PrintError (err )
463480 os .Exit (errNetwork )
464481 }
@@ -467,16 +484,19 @@ func executeLoginCommand(cmd *cobra.Command, args []string) {
467484 NetRC .NewMachine ("arduino.cc" , usr , token .Access , token .Refresh )
468485 content , err := NetRC .MarshalText ()
469486 if err != nil {
470- formatter .PrintError (err )
487+ logrus .WithError (err ).Error ("Cannot parse new .netrc file" )
488+ formatter .PrintErrorMessage ("Cannot parse new .netrc file" )
471489 os .Exit (errGeneric )
472490 }
473491
474492 err = ioutil .WriteFile (netRCFile , content , 0666 )
475493 if err != nil {
476- formatter .PrintError (err )
494+ logrus .WithError (err ).Error ("Cannot cannot write new .netrc file" )
495+ formatter .PrintErrorMessage ("Cannot cannot write new .netrc file" )
477496 os .Exit (errGeneric )
478497 }
479498
480499 formatter .PrintResult (`Successfully logged into the system
481500The session will continue to be refreshed with every call of the CLI and will expire if not used` )
501+ logrus .Info ("Done" )
482502}
0 commit comments