Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support progressed output in blob push and blob get #1113

Merged
merged 80 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
b088f2b
feat: support progressed output in blob push and blob get
qweeah Sep 12, 2023
e773471
add error when --debug is used with terminal output
qweeah Sep 19, 2023
6044594
bug fix
qweeah Sep 19, 2023
b5d89dd
Merge branch 'main' into progress-bar-reader
qweeah Sep 20, 2023
6324b93
add test for console
qweeah Sep 20, 2023
f1077bd
fix lint & pass tty to manager
qweeah Sep 20, 2023
4c362f8
Merge branch 'main' into progress-bar-reader
qweeah Sep 20, 2023
941188f
rename flag to disable tty
qweeah Sep 20, 2023
c72ebe5
add test for blob push
qweeah Sep 21, 2023
5fc75a8
fix render racing
qweeah Sep 21, 2023
a2d3f8c
fix race
qweeah Sep 21, 2023
4a065fe
add test for `blob fetch`
qweeah Sep 21, 2023
00ca2fc
resolve comment
qweeah Sep 22, 2023
c85e809
resolve comment
qweeah Sep 22, 2023
997a63e
resolve comment
qweeah Sep 22, 2023
144e907
fix lint
qweeah Sep 22, 2023
1b1f23c
refactor spinner mark
qweeah Sep 22, 2023
008a61d
refactor: improve manager and status
qweeah Sep 25, 2023
95ad473
add error for manager functions
qweeah Sep 25, 2023
ed84a69
remove unnecessary lock
qweeah Sep 25, 2023
47ef494
update reader naming
qweeah Sep 25, 2023
7e249ed
remove reader.once
qweeah Sep 25, 2023
ca19602
Merge remote-tracking branch 'origin_src/main' into progress-bar-reader
qweeah Sep 25, 2023
a6476d4
ignore returned error of manager
qweeah Sep 25, 2023
8c7458b
code clean
qweeah Sep 25, 2023
1584fd2
resolve comment
qweeah Sep 28, 2023
8fef80b
Merge remote-tracking branch 'origin_src/main' into progress-bar-reader
qweeah Sep 28, 2023
b44e2e8
code clean
qweeah Sep 28, 2023
ea6476f
code clean
qweeah Sep 28, 2023
ebcc114
Merge remote-tracking branch 'origin_src/main' into progress-bar-reader
qweeah Oct 7, 2023
ad0608d
add test for spinner
qweeah Oct 7, 2023
1aab8bd
add cross-package coverage generation
qweeah Oct 7, 2023
78b0d42
Refactor tests
qweeah Oct 7, 2023
2b13d6a
cover common tests
qweeah Oct 7, 2023
6bf4f3d
add status coverage
qweeah Oct 7, 2023
a7f3f96
code clean
qweeah Oct 7, 2023
3ad8eaa
add doc for stdout
qweeah Oct 7, 2023
243dc37
add status tests
qweeah Oct 7, 2023
5f5a1d9
resolve comment
qweeah Oct 7, 2023
7554534
NIT
qweeah Oct 7, 2023
76de650
align timing and percentage
qweeah Oct 7, 2023
486a8ed
fix unit test
qweeah Oct 7, 2023
39b0247
Merge remote-tracking branch 'origin_src/main' into progress-bar-reader
qweeah Oct 7, 2023
4fe990e
increase coverage
qweeah Oct 7, 2023
3a7ed21
fix: won't show complete when aborted
qweeah Oct 8, 2023
ff2c230
code clean
qweeah Oct 8, 2023
9426fac
code clean
qweeah Oct 8, 2023
50c4181
code clean
qweeah Oct 8, 2023
aa5b847
code clean
qweeah Oct 8, 2023
f49bdcb
align units of processed data to total size
qweeah Oct 8, 2023
8bc5c37
add instant download speed
qweeah Oct 8, 2023
7327022
code clean
qweeah Oct 9, 2023
1abda95
change base for sizing
qweeah Oct 9, 2023
636f424
fix speed display
qweeah Oct 9, 2023
2ea395d
add test coverage
qweeah Oct 9, 2023
4f7ac01
bug fix
qweeah Oct 9, 2023
c47f967
reset render interval to 200ms
qweeah Oct 9, 2023
2489a61
turn humanize into package
qweeah Oct 10, 2023
9e7daec
bug fix
qweeah Oct 10, 2023
c4564fe
rename pts device
qweeah Oct 10, 2023
167adb1
Merge remote-tracking branch 'origin_src/main' into progress-bar-reader
qweeah Oct 10, 2023
6c324d1
reduce buffer size
qweeah Oct 10, 2023
d4298c1
fix typo
qweeah Oct 10, 2023
67574dd
change switch to if-else
qweeah Oct 10, 2023
25d8935
bug fix
qweeah Oct 10, 2023
c76c464
change switch to if-else
qweeah Oct 10, 2023
9403cca
revert rendering
qweeah Oct 10, 2023
0741396
reduce buffer size to 1
qweeah Oct 10, 2023
c5ab1ee
fix: enable pipe when output to stdout
qweeah Oct 11, 2023
6611186
fix display bug
qweeah Oct 11, 2023
e858d1a
revert change
qweeah Oct 11, 2023
e4d4439
fix bug
qweeah Oct 11, 2023
6f36708
resolve comment
qweeah Oct 12, 2023
45aad11
fix nit
qweeah Oct 13, 2023
9e7f876
apply comment
qweeah Oct 13, 2023
57c29f2
resolve comments
qweeah Oct 13, 2023
7c8b2d7
udpate last render time
qweeah Oct 13, 2023
932c5d5
resolve comments
qweeah Oct 13, 2023
223eede
Merge remote-tracking branch 'origin_src/main' into progress-bar-reader
qweeah Oct 13, 2023
edf7ab8
code clean
qweeah Oct 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LDFLAGS += -X $(PROJECT_PKG)/internal/version.GitTreeState=${GIT_DIRTY}

.PHONY: test
test: tidy vendor check-encoding ## tidy and run tests
$(GO_EXE) test -race -v -coverprofile=coverage.txt -covermode=atomic ./...
$(GO_EXE) test -race -v -coverprofile=coverage.txt -covermode=atomic -coverpkg=./... ./...

.PHONY: teste2e
teste2e: ## run end to end tests
Expand Down
95 changes: 95 additions & 0 deletions cmd/oras/internal/display/console/console.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package console

import (
"os"

"github.com/containerd/console"
"github.com/morikuni/aec"
)

const (
// MinWidth is the minimal width of supported console.
MinWidth = 80
// MinHeight is the minimal height of supported console.
MinHeight = 10
// cannot use aec.Save since DEC has better compatilibity than SCO
Save = "\0337"
// cannot use aec.Restore since DEC has better compatilibity than SCO
Restore = "\0338"
)

// Console is a wrapper around containerd's console.Console and ANSI escape
// codes.
type Console struct {
console.Console
}

// Size returns the width and height of the console.
// If the console size cannot be determined, returns a default value of 80x10.
func (c *Console) Size() (width, height int) {
width = MinWidth
height = MinHeight
size, err := c.Console.Size()
if err == nil {
if size.Height > MinHeight {
height = int(size.Height)
}
if size.Width > MinWidth {
width = int(size.Width)
}
}
return
}

// New generates a Console from a file.
func New(f *os.File) (*Console, error) {
c, err := console.ConsoleFromFile(f)
if err != nil {
return nil, err
}
return &Console{c}, nil
}

// Save saves the current cursor position.
func (c *Console) Save() {
_, _ = c.Write([]byte(aec.Hide.Apply(Save)))
}

// NewRow allocates a horizontal space to the output area with scroll if needed.
func (c *Console) NewRow() {
_, _ = c.Write([]byte(Restore))
_, _ = c.Write([]byte("\n"))
_, _ = c.Write([]byte(Save))
}

// OutputTo outputs a string to a specific line.
func (c *Console) OutputTo(upCnt uint, str string) {
_, _ = c.Write([]byte(Restore))
_, _ = c.Write([]byte(aec.PreviousLine(upCnt).Apply(str)))
_, _ = c.Write([]byte(" "))
_, _ = c.Write([]byte(aec.EraseLine(aec.EraseModes.Tail).String()))
}

// Restore restores the saved cursor position.
func (c *Console) Restore() {
// cannot use aec.Restore since DEC has better compatilibity than SCO
_, _ = c.Write([]byte(Restore))
_, _ = c.Write([]byte(aec.Column(0).
With(aec.EraseLine(aec.EraseModes.All)).
With(aec.Show).String()))
}
63 changes: 63 additions & 0 deletions cmd/oras/internal/display/console/console_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//go:build darwin || freebsd || linux || netbsd || openbsd || solaris

/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package console

import (
"testing"

"github.com/containerd/console"
)

func validateSize(t *testing.T, gotWidth, gotHeight, wantWidth, wantHeight int) {
t.Helper()
if gotWidth != wantWidth {
t.Errorf("Console.Size() gotWidth = %v, want %v", gotWidth, wantWidth)
}
if gotHeight != wantHeight {
t.Errorf("Console.Size() gotHeight = %v, want %v", gotHeight, wantHeight)
}
}

func TestConsole_Size(t *testing.T) {
pty, _, err := console.NewPty()
if err != nil {
t.Fatal(err)
}
c := &Console{
Console: pty,
}

// minimal width and height
gotWidth, gotHeight := c.Size()
validateSize(t, gotWidth, gotHeight, MinWidth, MinHeight)

// zero width
_ = pty.Resize(console.WinSize{Width: 0, Height: MinHeight})
gotWidth, gotHeight = c.Size()
validateSize(t, gotWidth, gotHeight, MinWidth, MinHeight)

// zero height
_ = pty.Resize(console.WinSize{Width: MinWidth, Height: 0})
gotWidth, gotHeight = c.Size()
validateSize(t, gotWidth, gotHeight, MinWidth, MinHeight)

// valid zero and height
_ = pty.Resize(console.WinSize{Width: 200, Height: 100})
gotWidth, gotHeight = c.Size()
validateSize(t, gotWidth, gotHeight, 200, 100)
}
71 changes: 71 additions & 0 deletions cmd/oras/internal/display/console/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build darwin || freebsd || linux || netbsd || openbsd || solaris

/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testutils

import (
"bytes"
"fmt"
"io"
"os"
"strings"
"sync"

"github.com/containerd/console"
)

// NewPty creates a new pty pair for testing, caller is responsible for closing
// the returned device file if err is not nil.
func NewPty() (console.Console, *os.File, error) {
qweeah marked this conversation as resolved.
Show resolved Hide resolved
pty, devicePath, err := console.NewPty()
if err != nil {
return nil, nil, err
}
device, err := os.OpenFile(devicePath, os.O_RDWR, 0)
if err != nil {
return nil, nil, err
}
return pty, device, nil
}

// MatchPty checks that the output matches the expected strings in specified
// order.
func MatchPty(pty console.Console, device *os.File, expected ...string) error {
var wg sync.WaitGroup
wg.Add(1)
var buffer bytes.Buffer
go func() {
defer wg.Done()
_, _ = io.Copy(&buffer, pty)
}()
device.Close()
wg.Wait()

return OrderedMatch(buffer.String(), expected...)
}

// OrderedMatch matches the got with the expected strings in order.
func OrderedMatch(got string, want ...string) error {
for _, e := range want {
i := strings.Index(got, e)
if i < 0 {
return fmt.Errorf("failed to find %q in %q", e, got)
}
got = got[i+len(e):]
}
return nil
}
60 changes: 60 additions & 0 deletions cmd/oras/internal/display/progress/humanize/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package humanize

import (
"fmt"
"math"
)

const base = 1024.0

var units = []string{"B", "kB", "MB", "GB", "TB"}

type Bytes struct {
Size float64
Unit string
}

// ToBytes converts size in bytes to human readable format.
func ToBytes(sizeInBytes int64) Bytes {
f := float64(sizeInBytes)
if f < base {
return Bytes{f, units[0]}
}
e := int(math.Floor(math.Log(f) / math.Log(base)))
if e >= len(units) {
// only support up to TB
e = len(units) - 1
}
p := f / math.Pow(base, float64(e))
return Bytes{RoundTo(p), units[e]}
}

// String returns the string representation of Bytes.
func (b Bytes) String() string {
return fmt.Sprintf("%v %2s", b.Size, b.Unit)
}

// RoundTo makes length of the size string to less than or equal to 4.
func RoundTo(size float64) float64 {
if size < 10 {
return math.Round(size*100) / 100
} else if size < 100 {
return math.Round(size*10) / 10
}
return math.Round(size)
}
72 changes: 72 additions & 0 deletions cmd/oras/internal/display/progress/humanize/bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package humanize

import (
"reflect"
"testing"
)

func TestRoundTo(t *testing.T) {
type args struct {
quantity float64
}
tests := []struct {
name string
args args
want float64
}{
{"round to 2 digit", args{1.223}, 1.22},
{"round to 1 digit", args{12.23}, 12.2},
{"round to no digit", args{122.6}, 123},
{"round to no digit", args{1223.123}, 1223},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RoundTo(tt.args.quantity); got != tt.want {
t.Errorf("RoundTo() = %v, want %v", got, tt.want)
}
})
}
}

func TestToBytes(t *testing.T) {
type args struct {
sizeInBytes int64
}
tests := []struct {
name string
args args
want Bytes
}{
{"0 bytes", args{0}, Bytes{0, "B"}},
{"1023 bytes", args{1023}, Bytes{1023, "B"}},
{"1 kB", args{1024}, Bytes{1, "kB"}},
{"1.5 kB", args{1024 + 512}, Bytes{1.5, "kB"}},
{"12.5 kB", args{1024 * 12.5}, Bytes{12.5, "kB"}},
{"512.5 kB", args{1024 * 512.5}, Bytes{513, "kB"}},
{"1 MB", args{1024 * 1024}, Bytes{1, "MB"}},
{"1 GB", args{1024 * 1024 * 1024}, Bytes{1, "GB"}},
{"1 TB", args{1024 * 1024 * 1024 * 1024}, Bytes{1, "TB"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToBytes(tt.args.sizeInBytes); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToBytes() = %v, want %v", got, tt.want)
}
})
}
}
Loading
Loading