Skip to content

Commit

Permalink
Enable progress bar config in upload from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
radito3 committed Oct 25, 2023
1 parent f522d37 commit bb09e5e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
17 changes: 11 additions & 6 deletions commands/deploy_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,16 @@ func (c *DeployCommand) executeInternal(positionalArgs []string, dsHost string,

namespace := strings.TrimSpace(GetStringOpt(namespaceOpt, flags))
force := GetBoolOpt(forceOpt, flags)
uploadChunkSizeInMB := configuration.NewSnapshot().GetUploadChunkSizeInMB()
fileUploader := NewFileUploader(mtaClient, namespace, uploadChunkSizeInMB)
conf := configuration.NewSnapshot()
uploadChunkSize := conf.GetUploadChunkSizeInMB()
sequentialUpload := conf.GetUploadChunksSequentially()
disableProgressBar := conf.GetDisableUploadProgressBar()
fileUploader := NewFileUploader(mtaClient, namespace, uploadChunkSize, sequentialUpload, disableProgressBar)

if isUrl {
var fileId string
var isFailure bool
fileId, mtaId, isFailure = c.uploadFromUrl(mtaArchive, mtaClient, namespace)
fileId, mtaId, isFailure = c.uploadFromUrl(mtaArchive, mtaClient, namespace, disableProgressBar)
if isFailure {
return Failure
}
Expand Down Expand Up @@ -326,8 +329,9 @@ func parseMtaArchiveArgument(rawMtaArchive interface{}) (bool, string) {
return false, ""
}

func (c *DeployCommand) uploadFromUrl(url string, mtaClient mtaclient.MtaClientOperations, namespace string) (fileId, mtaId string, failure bool) {
progressBar := c.tryFetchMtarSize(url)
func (c *DeployCommand) uploadFromUrl(url string, mtaClient mtaclient.MtaClientOperations, namespace string,
disableProgressBar bool) (fileId, mtaId string, failure bool) {
progressBar := c.tryFetchMtarSize(url, disableProgressBar)

encodedFileUrl := base64.URLEncoding.EncodeToString([]byte(url))
responseHeaders, err := mtaClient.StartUploadMtaArchiveFromUrl(encodedFileUrl, &namespace)
Expand Down Expand Up @@ -457,7 +461,7 @@ func (c *DeployCommand) tryReadingFileUrl() string {
return ""
}

func (c *DeployCommand) tryFetchMtarSize(url string) *pb.ProgressBar {
func (c *DeployCommand) tryFetchMtarSize(url string, disableProgressBar bool) *pb.ProgressBar {
client := http.Client{Timeout: c.FileUrlReadTimeout}
resp, err := client.Head(url)
if err != nil {
Expand All @@ -471,6 +475,7 @@ func (c *DeployCommand) tryFetchMtarSize(url string) *pb.ProgressBar {
bar := pb.New64(resp.ContentLength).SetUnits(pb.U_BYTES)
bar.ShowElapsedTime = true
bar.ShowTimeLeft = false
bar.NotPrint = disableProgressBar
return bar
}

Expand Down
9 changes: 4 additions & 5 deletions commands/file_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/baseclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/configuration"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/log"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/util"
Expand Down Expand Up @@ -64,14 +63,14 @@ func (r *progressBarReader) Close() error {
}

// NewFileUploader creates a new file uploader for the specified namespace
func NewFileUploader(mtaClient mtaclient.MtaClientOperations, namespace string, uploadChunkSizeInMB uint64) *FileUploader {
conf := configuration.NewSnapshot()
func NewFileUploader(mtaClient mtaclient.MtaClientOperations, namespace string, uploadChunkSizeInMB uint64,
sequentialUpload, shouldDisableProgressBar bool) *FileUploader {
return &FileUploader{
mtaClient: mtaClient,
namespace: namespace,
uploadChunkSizeInMB: uploadChunkSizeInMB,
sequentialUpload: conf.GetUploadChunksSequentially(),
shouldDisableProgressBar: conf.GetDisableUploadProgressBar(),
sequentialUpload: sequentialUpload,
shouldDisableProgressBar: shouldDisableProgressBar,
}
}

Expand Down
18 changes: 12 additions & 6 deletions commands/file_uploader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ var _ = Describe("FileUploader", func() {
client := fakeMtaClientBuilder.GetMtaFiles([]*models.FileMetadata{}, nil).Build()

output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB)
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB,
properties.DefaultUploadChunksSequentially, properties.DefaultDisableProgressBar)
uploadedFiles, status = fileUploader.UploadFiles([]string{})
})
ex.ExpectSuccessWithOutput(status.ToInt(), output, []string{""})
Expand All @@ -57,7 +58,8 @@ var _ = Describe("FileUploader", func() {
client := fakeMtaClientBuilder.GetMtaFiles([]*models.FileMetadata{&testutil.SimpleFile}, nil).Build()
var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB)
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB,
properties.DefaultUploadChunksSequentially, properties.DefaultDisableProgressBar)
uploadedFiles, status = fileUploader.UploadFiles([]string{})
})
ex.ExpectSuccessWithOutput(status.ToInt(), output, []string{""})
Expand All @@ -73,7 +75,8 @@ var _ = Describe("FileUploader", func() {
UploadMtaFile(testFile, testutil.GetFile(testFile, testFileDigest, namespace), nil).Build()
var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB)
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB,
properties.DefaultUploadChunksSequentially, properties.DefaultDisableProgressBar)
uploadedFiles, status = fileUploader.UploadFiles([]string{testFileAbsolutePath})
})
Expect(len(uploadedFiles)).To(Equal(1))
Expand All @@ -95,7 +98,8 @@ var _ = Describe("FileUploader", func() {
UploadMtaFile(testFile, testutil.GetFile(testFile, testFileDigest, "namespace"), nil).Build()
var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB)
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB,
properties.DefaultUploadChunksSequentially, properties.DefaultDisableProgressBar)
uploadedFiles, status = fileUploader.UploadFiles([]string{testFileAbsolutePath})
})
ex.ExpectSuccessWithOutput(status.ToInt(), output, []string{
Expand All @@ -113,7 +117,8 @@ var _ = Describe("FileUploader", func() {
UploadMtaFile(testFile, fileMetadata, nil).Build()
var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB)
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB,
properties.DefaultUploadChunksSequentially, properties.DefaultDisableProgressBar)
uploadedFiles, status = fileUploader.UploadFiles([]string{testFileAbsolutePath})
})
Expect(len(uploadedFiles)).To(Equal(1))
Expand All @@ -135,7 +140,8 @@ var _ = Describe("FileUploader", func() {
UploadMtaFile(testFile, &models.FileMetadata{}, errors.New("Unexpected error from the backend")).Build()
// var uploadedFiles []*models.FileMetadata
output := oc.CaptureOutput(func() {
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB)
fileUploader = commands.NewFileUploader(client, namespace, properties.DefaultUploadChunkSizeInMB,
properties.DefaultUploadChunksSequentially, properties.DefaultDisableProgressBar)
_, status = fileUploader.UploadFiles([]string{testFileAbsolutePath})
})
// Expect(len(uploadedFiles)).To(Equal(1))
Expand Down
4 changes: 3 additions & 1 deletion configuration/properties/disable_progress_bar_property.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package properties

const DefaultDisableProgressBar = false

var DisableProgressBar = ConfigurableProperty{
Name: "MULTIAPPS_DISABLE_UPLOAD_PROGRESS_BAR",
Parser: booleanParser{},
ParsingSuccessMessage: "Attention: You've specified %v for the environment variable %s.\n",
ParsingFailureMessage: "Invalid boolean value (%s) for environment variable %s. Using default value %v.\n",
DefaultValue: false,
DefaultValue: DefaultDisableProgressBar,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package properties

import "strconv"

const DefaultUploadChunksSequentially = false

var UploadChunksSequentially = ConfigurableProperty{
Name: "MULTIAPPS_UPLOAD_CHUNKS_SEQUENTIALLY",
Parser: booleanParser{},
ParsingSuccessMessage: "Attention: You've specified %v for the environment variable %s.\n",
ParsingFailureMessage: "Invalid boolean value (%s) for environment variable %s. Using default value %v.\n",
DefaultValue: false,
DefaultValue: DefaultUploadChunksSequentially,
}

type booleanParser struct{}
Expand Down

0 comments on commit bb09e5e

Please sign in to comment.