Skip to content

Commit

Permalink
Show UI progress for object upload to storage #78
Browse files Browse the repository at this point in the history
  • Loading branch information
simcap committed Mar 21, 2017
1 parent 0f00986 commit dd93339
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Rudimentary security groups port scanner inspector via `awless inspect -i port_scanner`
- Template: compile time check of undefined or unused references
- [#78](https://github.com/wallix/awless/issues/78): Show progress when uploadgin object to storage

### Bugfixes

Expand Down
48 changes: 47 additions & 1 deletion aws/driver/driver_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/mitchellh/ioprogress"
"github.com/wallix/awless/cloud"
"github.com/wallix/awless/console"
)
Expand Down Expand Up @@ -557,6 +558,44 @@ func (d *S3Driver) Create_Storageobject_DryRun(params map[string]interface{}) (i
return nil, nil
}

type progressReadSeeker struct {
file *os.File
reader *ioprogress.Reader
}

func newProgressReader(f *os.File) (*progressReadSeeker, error) {
finfo, err := f.Stat()
if err != nil {
return nil, err
}

draw := func(progress, total int64) string {
// &s3.PutObjectInput.Body will be read twice
// once in memory and a second time for the HTTP upload
// here we only display for the actual HTTP upload
if progress > total {
return ioprogress.DrawTextFormatBytes(progress/2, total)
}
return ""
}

reader := &ioprogress.Reader{
DrawFunc: ioprogress.DrawTerminalf(os.Stdout, draw),
Reader: f,
Size: finfo.Size(),
}

return &progressReadSeeker{file: f, reader: reader}, nil
}

func (pr *progressReadSeeker) Read(p []byte) (int, error) {
return pr.reader.Read(p)
}

func (pr *progressReadSeeker) Seek(offset int64, whence int) (int64, error) {
return pr.file.Seek(offset, whence)
}

func (d *S3Driver) Create_Storageobject(params map[string]interface{}) (interface{}, error) {
input := &s3.PutObjectInput{}

Expand All @@ -565,7 +604,12 @@ func (d *S3Driver) Create_Storageobject(params map[string]interface{}) (interfac
return nil, err
}
defer f.Close()
input.Body = f

progressR, err := newProgressReader(f)
if err != nil {
return nil, err
}
input.Body = progressR

var fileName string
if n, ok := params["name"].(string); ok && n != "" {
Expand All @@ -581,6 +625,8 @@ func (d *S3Driver) Create_Storageobject(params map[string]interface{}) (interfac
return nil, err
}

d.logger.Infof("uploading '%s'", fileName)

output, err := d.PutObject(input)
if err != nil {
return nil, fmt.Errorf("create storageobject: %s", err)
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/mitchellh/ioprogress/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions vendor/github.com/mitchellh/ioprogress/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions vendor/github.com/mitchellh/ioprogress/draw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions vendor/github.com/mitchellh/ioprogress/reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@
"revision": "737072b4e32b7a5018b4a7125da8d12de90e8045",
"revisionTime": "2016-10-12T01:35:12Z"
},
{
"checksumSHA1": "NB5fzIAruRSs7H8vH27kLXEGVBA=",
"path": "github.com/mitchellh/ioprogress",
"revision": "8163955264568045f462ae7e2d6d07b2001fc997",
"revisionTime": "2015-05-21T21:15:56Z"
},
{
"checksumSHA1": "KCJhN9Dx329wAN/SeL4CxeypPyk=",
"path": "github.com/mitchellh/mapstructure",
Expand Down

0 comments on commit dd93339

Please sign in to comment.