@@ -402,9 +402,10 @@ func runComponentsInstall(cmd *cobra.Command, args []string) (err error) {
402402
403403func installComponent (cmd * cobra.Command , args []string ) (err error ) {
404404 var (
405- componentName string = args [0 ]
406- params map [string ]interface {} = make (map [string ]interface {})
407- start time.Time
405+ componentName string = args [0 ]
406+ downloadComplete = make (chan int8 )
407+ params map [string ]interface {} = make (map [string ]interface {})
408+ start time.Time
408409 )
409410
410411 cli .Event .Component = componentName
@@ -433,12 +434,18 @@ func installComponent(cmd *cobra.Command, args []string) (err error) {
433434
434435 start = time .Now ()
435436
436- stageClose , err := catalog .Stage (component , versionArg )
437+ progressClosure := func (path string , sizeB int64 ) {
438+ downloadProgress (downloadComplete , path , sizeB )
439+ }
440+
441+ stageClose , err := catalog .Stage (component , versionArg , progressClosure )
437442 if err != nil {
438443 return
439444 }
440445 defer stageClose ()
441446
447+ downloadComplete <- 0
448+
442449 params ["stage_duration_ms" ] = time .Since (start ).Milliseconds ()
443450 cli .Event .FeatureData = params
444451
@@ -568,10 +575,11 @@ func runComponentsUpdate(_ *cobra.Command, args []string) (err error) {
568575
569576func updateComponent (args []string ) (err error ) {
570577 var (
571- componentName string = args [0 ]
572- params map [string ]interface {} = make (map [string ]interface {})
573- start time.Time
574- targetVersion * semver.Version
578+ componentName string = args [0 ]
579+ downloadComplete = make (chan int8 )
580+ params map [string ]interface {} = make (map [string ]interface {})
581+ start time.Time
582+ targetVersion * semver.Version
575583 )
576584
577585 cli .StartProgress ("Loading components Catalog..." )
@@ -619,12 +627,18 @@ func updateComponent(args []string) (err error) {
619627
620628 start = time .Now ()
621629
622- stageClose , err := catalog .Stage (component , versionArg )
630+ progressClosure := func (path string , sizeB int64 ) {
631+ downloadProgress (downloadComplete , path , sizeB )
632+ }
633+
634+ stageClose , err := catalog .Stage (component , versionArg , progressClosure )
623635 if err != nil {
624636 return
625637 }
626638 defer stageClose ()
627639
640+ downloadComplete <- 0
641+
628642 params ["stage_duration_ms" ] = time .Since (start ).Milliseconds ()
629643 cli .Event .FeatureData = params
630644
@@ -873,10 +887,11 @@ func componentsToTable() [][]string {
873887
874888func prototypeRunComponentsInstall (cmd * cobra.Command , args []string ) (err error ) {
875889 var (
876- componentName string = args [0 ]
877- version string = versionArg
878- params map [string ]interface {} = make (map [string ]interface {})
879- start time.Time
890+ componentName string = args [0 ]
891+ downloadComplete = make (chan int8 )
892+ version string = versionArg
893+ params map [string ]interface {} = make (map [string ]interface {})
894+ start time.Time
880895 )
881896
882897 cli .Event .Component = componentName
@@ -906,13 +921,20 @@ func prototypeRunComponentsInstall(cmd *cobra.Command, args []string) (err error
906921
907922 start = time .Now ()
908923
924+ progressClosure := func (path string , sizeB int64 ) {
925+ downloadProgress (downloadComplete , path , sizeB )
926+ }
927+
909928 cli .StartProgress (fmt .Sprintf ("Installing component %s..." , component .Name ))
910- err = cli .LwComponents .Install (component , version )
929+ err = cli .LwComponents .Install (component , version , progressClosure )
911930 cli .StopProgress ()
912931 if err != nil {
913932 err = errors .Wrap (err , "unable to install component" )
914933 return
915934 }
935+
936+ downloadComplete <- 0
937+
916938 cli .OutputChecklist (successIcon , "Component %s installed\n " , color .HiYellowString (component .Name ))
917939
918940 params ["install_duration_ms" ] = time .Since (start ).Milliseconds ()
@@ -999,13 +1021,22 @@ func prototypeRunComponentsUpdate(args []string) (err error) {
9991021 return nil
10001022 }
10011023
1024+ downloadComplete := make (chan int8 )
1025+
1026+ progressClosure := func (path string , sizeB int64 ) {
1027+ downloadProgress (downloadComplete , path , sizeB )
1028+ }
1029+
10021030 cli .StartProgress (fmt .Sprintf ("Updating component %s to version %s..." , component .Name , & updateTo ))
1003- err = cli .LwComponents .Install (component , updateTo .String ())
1031+ err = cli .LwComponents .Install (component , updateTo .String (), progressClosure )
10041032 cli .StopProgress ()
10051033 if err != nil {
10061034 err = errors .Wrap (err , "unable to update component" )
10071035 return
10081036 }
1037+
1038+ downloadComplete <- 0
1039+
10091040 cli .OutputChecklist (successIcon , "Component %s updated to %s\n " ,
10101041 color .HiYellowString (component .Name ),
10111042 color .HiCyanString (fmt .Sprintf ("v%s" , updateTo .String ())))
@@ -1103,3 +1134,57 @@ func prototypeRunComponentsDelete(args []string) (err error) {
11031134 cli .
OutputHuman (
"Reach out to us at %s\n " ,
color .
HiCyanString (
"[email protected] " ))
11041135 return
11051136}
1137+
1138+ func downloadProgress (complete chan int8 , path string , sizeB int64 ) {
1139+ file , err := os .Open (path )
1140+ if err != nil {
1141+ cli .Log .Errorf ("Failed to open component file: %s" , err .Error ())
1142+ return
1143+ }
1144+ defer file .Close ()
1145+
1146+ var (
1147+ previous float64 = 0
1148+ stop bool = false
1149+ )
1150+
1151+ spinnerSuffix := cli .spinner .Suffix
1152+
1153+ for ! stop {
1154+ select {
1155+ case <- complete :
1156+ stop = true
1157+ default :
1158+ info , err := file .Stat ()
1159+ if err != nil {
1160+ cli .Log .Errorf ("Failed to stat component file: %s" , err .Error ())
1161+ return
1162+ }
1163+
1164+ size := info .Size ()
1165+ if size == 0 {
1166+ size = 1
1167+ }
1168+
1169+ if sizeB == 0 {
1170+ mb := float64 (size ) / (1 << 20 )
1171+
1172+ if mb > previous {
1173+ cli .spinner .Suffix = fmt .Sprintf ("%s Downloaded: %.0fmb" , spinnerSuffix , mb )
1174+
1175+ previous = mb
1176+ }
1177+ } else {
1178+ percent := float64 (size ) / float64 (sizeB ) * 100
1179+
1180+ if percent > previous {
1181+ cli .spinner .Suffix = fmt .Sprintf ("%s Downloaded: %.0f%s" , spinnerSuffix , percent , "%" )
1182+
1183+ previous = percent
1184+ }
1185+ }
1186+ }
1187+
1188+ time .Sleep (time .Second )
1189+ }
1190+ }
0 commit comments