Skip to content

Commit

Permalink
Add update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
moshe-kabala committed Nov 26, 2024
1 parent 9a4438d commit 5e86373
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
6 changes: 3 additions & 3 deletions pkg/api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func DownloadFile(url string, file io.Writer) error {

func UploadFile(url string, file io.Reader, fileSize int64) error {
log.Infof("Upload target: %s", url)
start, stop, _ := log.NewSpinner("Uploading...")
start()
defer stop()
s := log.NewSpinner("Uploading...")
s.Start()
defer s.Stop()

req, err := http.NewRequest(http.MethodPut, url, file)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions pkg/api/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ var ErrorTimeout = errors.New("timeout")
func WaitForCondition(ctx context.Context, message string, condition func() (bool, error), sleepDuration time.Duration, timeoutDuration time.Duration) error {
startTime := time.Now()

start, stop, _ := log.NewSpinner(message)
start()

defer stop()
s := log.NewSpinner(message)
s.Start()
defer s.Stop()

for time.Since(startTime) < timeoutDuration {
select {
Expand Down
12 changes: 6 additions & 6 deletions pkg/hub/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func (hs *HubStorageApi) UploadProjectContent(fileStream io.Reader, meta *Projec

filePaths := GenerateProjectFilePaths(hs.Namespace, meta)

start, stop, _ := log.NewSpinner(fmt.Sprintf("HubStorage - Uploading content of project: '%s' version: %d", name, schemaVersion))
start()
defer stop()
s := log.NewSpinner(fmt.Sprintf("HubStorage - Uploading content of project: '%s' version: %d", name, schemaVersion))
s.Start()
defer s.Stop()
err := hs.FilesClient.UploadFile(filePaths.TarPath, fileStream, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -147,9 +147,9 @@ func (hs *HubStorageApi) DownloadProject(projectName string, projectVersion int,
return fmt.Errorf("not found tar file for project: %s version: %d", projectName, projectVersion)
}

start, stop, _ := log.NewSpinner(fmt.Sprintf("Downloading project: %s, into: %s", projectName, destFileName))
start()
defer stop()
s := log.NewSpinner(fmt.Sprintf("Downloading project: %s, into: %s", projectName, destFileName))
s.Start()
defer s.Stop()
return hs.FilesClient.DownloadFile(fileName, destFileName)
}

Expand Down
39 changes: 32 additions & 7 deletions pkg/log/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,41 @@ func ConnectFileToVerboseLogOutput(filePath string) (close func(), err error) {
return
}

func NewSpinner(message string, a ...any) (start func(), stop func(), s *spinner.Spinner) {
// Spinner is a custom type that wraps a spinner with additional functionality.
type Spinner struct {
s *spinner.Spinner
message string
percentage int
}

// NewSpinner creates a new Spinner instance with the specified message.
func NewSpinner(message string, a ...any) *Spinner {
message = fmt.Sprintf(message, a...)
s = spinner.New(spinner.CharSets[33], 500*time.Millisecond)
s := spinner.New(spinner.CharSets[33], 500*time.Millisecond)
s.Suffix = fmt.Sprintf(" %s", message)
start = s.Start

stop = func() {
s.Stop()
Info(message)
return &Spinner{
s: s,
message: message,
}
}

return
// Start begins the spinner animation.
func (sp *Spinner) Start() {
sp.s.Start()
}

// Stop stops the spinner animation and prints a completion message.
func (sp *Spinner) Stop() {
sp.s.Stop()
Infof("%s - Completed\n", sp.message)
}

// UpdateProgress updates the progress percentage displayed with the spinner.
func (sp *Spinner) UpdateProgress(percent int) {
if percent > 100 {
percent = 100
}
sp.percentage = percent
sp.s.Suffix = fmt.Sprintf(" %s [%d%%]", sp.message, percent)
}
6 changes: 3 additions & 3 deletions pkg/project/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ func ExportProjectIntoFile(ctx context.Context, project *ProjectEntity, outputDi
return err
}
defer file.Close()
start, stop, _ := log.NewSpinner(fmt.Sprintf("Exporting project '%s', into: '%s'", project.Name, filePath))
start()
defer stop()
s := log.NewSpinner(fmt.Sprintf("Exporting project '%s', into: '%s'", project.Name, filePath))
s.Start()
defer s.Stop()
res, _, err := api.ApiClient.GetDownloadSignedUrl(ctx).
GetDownloadSignedUrlParams(*tensorleapapi.NewGetDownloadSignedUrlParams(exportUrl)).
Execute()
Expand Down

0 comments on commit 5e86373

Please sign in to comment.