@@ -5,12 +5,14 @@ import (
55 "context"
66 "errors"
77 "fmt"
8+ "io"
89 "os"
910 "path/filepath"
1011 "strings"
1112 "time"
1213
1314 "github.com/outcatcher/hipapu/internal/config"
15+ "github.com/schollz/progressbar/v3"
1416)
1517
1618// Public sync errors. Self-explanatory.
1921 ErrMissingAsset = errors .New ("no asset with given name found" )
2022)
2123
22- // Synchronize downloads all new releases replacing local files.
24+ // Synchronize runs synchronization of all new releases replacing local files reporting the progress .
2325func (a * Application ) Synchronize (ctx context.Context ) error {
2426 installations := a .config .GetInstallations ()
2527
@@ -31,7 +33,10 @@ func (a *Application) Synchronize(ctx context.Context) error {
3133
3234 var errs error
3335
34- for _ , installation := range installations {
36+ for i , installation := range installations {
37+ // todo: output is a CLI interaction, needs to be moved out to cmd somehow
38+ fmt .Printf ("Synchronizing installation #%d of %d\n " , i + 1 , len (installations ))
39+
3540 // todo: parrallelize
3641 if err := a .syncInstallation (ctx , installation ); err != nil {
3742 errs = errors .Join (errs , err )
@@ -44,7 +49,9 @@ func (a *Application) Synchronize(ctx context.Context) error {
4449}
4550
4651//nolint:cyclop,funlen // rewriting makes it less readable
47- func (a * Application ) syncInstallation (ctx context.Context , installation config.Installation ) error {
52+ func (a * Application ) syncInstallation (
53+ ctx context.Context , installation config.Installation ,
54+ ) error {
4855 file , err := a .files .GetFileInfo (installation .LocalPath )
4956 if err != nil {
5057 return fmt .Errorf ("failed to get file info: %w" , err )
@@ -53,7 +60,12 @@ func (a *Application) syncInstallation(ctx context.Context, installation config.
5360 urlParts := strings .Split (installation .RepoURL , "/" )
5461 owner , repo := urlParts [len (urlParts )- 2 ], urlParts [len (urlParts )- 1 ]
5562
56- a .log ().Info ("Starting sync of installation" , "owner" , owner , "repo" , repo , "local path" , installation .LocalPath )
63+ a .log ().InfoContext (ctx ,
64+ "Starting sync of installation" ,
65+ "owner" , owner ,
66+ "repo" , repo ,
67+ "local path" , installation .LocalPath ,
68+ )
5769
5870 release , err := a .remote .GetLatestRelease (ctx , owner , repo )
5971 if err != nil {
@@ -96,7 +108,20 @@ func (a *Application) syncInstallation(ctx context.Context, installation config.
96108
97109 a .log ().Info ("Download started" , "download URL" , downloadURL , "total size, MiB" , totalSize / 1024 / 1024 ) //nolint:mnd
98110
99- if err := a .remote .DownloadFile (ctx , downloadURL , tmpFile ); err != nil {
111+ // todo: progress bar is a CLI interaction, needs to be moved out to cmd somehow
112+ bar := progressbar .DefaultBytes (- 1 , "Downloading to" + tmpFilePath )
113+ bar .ChangeMax (totalSize )
114+
115+ // cleanup on cancel
116+ go func () {
117+ <- ctx .Done ()
118+
119+ _ = tmpFile .Close ()
120+ }()
121+
122+ writer := io .MultiWriter (bar , tmpFile )
123+
124+ if err := a .remote .DownloadFile (ctx , downloadURL , writer ); err != nil {
100125 return fmt .Errorf ("failed to dowload to tmp file: %w" , err )
101126 }
102127
0 commit comments