2020)
2121
2222// Synchronize runs synchronization of all new releases replacing local files reporting the progress.
23- func (a * Application ) Synchronize (ctx context.Context ) error {
23+ func (a * Application ) Synchronize (ctx context.Context , writer io. Writer ) error {
2424 installations , err := a .List (ctx )
2525 if err != nil {
2626 return fmt .Errorf ("sync error: %w" , err )
@@ -35,11 +35,29 @@ func (a *Application) Synchronize(ctx context.Context) error {
3535 var errs error
3636
3737 for i , installation := range installations {
38+ if installation .SkipSync {
39+ _ , _ = fmt .Fprintf (
40+ writer ,
41+ "Skipping sync for installation #%d of %d (%s)\n " ,
42+ i + 1 ,
43+ len (installations ),
44+ installation .Release .RepoURL ,
45+ )
46+
47+ continue
48+ }
49+
3850 // todo: output is a CLI interaction, needs to be moved out to cmd somehow
39- fmt .Printf ("Synchronizing installation #%d of %d\n " , i + 1 , len (installations ))
51+ _ , _ = fmt .Fprintf (
52+ writer ,
53+ "Synchronizing installation #%d of %d (%s)\n " ,
54+ i + 1 ,
55+ len (installations ),
56+ installation .Release .RepoURL ,
57+ )
4058
4159 // todo: parrallelize
42- if err := a .syncInstallation (ctx , installation ); err != nil {
60+ if err := a .syncInstallation (ctx , installation , writer ); err != nil {
4361 errs = errors .Join (errs , err )
4462
4563 continue
@@ -51,7 +69,7 @@ func (a *Application) Synchronize(ctx context.Context) error {
5169
5270//nolint:cyclop,funlen // rewriting makes it less readable
5371func (a * Application ) syncInstallation (
54- ctx context.Context , installation Installation ,
72+ ctx context.Context , installation Installation , extWriter io. Writer ,
5573) error {
5674 a .log ().InfoContext (ctx ,
5775 "Starting sync of installation" ,
@@ -99,8 +117,7 @@ func (a *Application) syncInstallation(
99117 a .log ().Info ("Download started" , "download URL" , downloadURL , "total size, MiB" , totalSize / 1024 / 1024 ) //nolint:mnd
100118
101119 // todo: progress bar is a CLI interaction, needs to be moved out to cmd somehow
102- bar := progressbar .DefaultBytes (- 1 , "Downloading to" + tmpFilePath )
103- bar .ChangeMax (totalSize )
120+ bar := newProgressBar (extWriter , totalSize , "Downloading to " + tmpFilePath )
104121
105122 // cleanup on cancel
106123 go func () {
@@ -138,3 +155,23 @@ func (a *Application) syncInstallation(
138155
139156 return nil
140157}
158+
159+ func newProgressBar (writer io.Writer , maxBytes int , description string ) * progressbar.ProgressBar {
160+ //nolint:mnd // temporary location for progress bar initialization
161+ return progressbar .NewOptions (
162+ maxBytes ,
163+ progressbar .OptionSetDescription (description ),
164+ progressbar .OptionSetWriter (writer ),
165+ progressbar .OptionShowBytes (true ),
166+ progressbar .OptionShowTotalBytes (true ),
167+ progressbar .OptionSetWidth (10 ),
168+ progressbar .OptionThrottle (65 * time .Millisecond ),
169+ progressbar .OptionShowCount (),
170+ progressbar .OptionOnCompletion (func () {
171+ _ , _ = fmt .Fprint (writer , "\n " )
172+ }),
173+ progressbar .OptionSpinnerType (14 ),
174+ progressbar .OptionFullWidth (),
175+ progressbar .OptionSetRenderBlankState (true ),
176+ )
177+ }
0 commit comments