mirror of
https://github.com/docker/compose.git
synced 2026-11-20 15:09:50 +00:00
refactor(compose): share the pull and push progress stream decoding
pull and push decoded the daemon's JSON message stream with the same loop and computed the completion percentage the same way. Move both to helpers. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
619ad27cd3
commit
0b5eb38f4e
4 changed files with 81 additions and 35 deletions
|
|
@ -18,7 +18,12 @@ package compose
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/moby/moby/api/types/jsonstream"
|
||||
|
||||
"github.com/docker/compose/v5/internal/progress"
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
|
|
@ -47,3 +52,28 @@ func buildingEvent(id string) api.Resource {
|
|||
func builtEvent(id string) api.Resource {
|
||||
return progress.New("Image "+id, api.Done, api.StatusBuilt)
|
||||
}
|
||||
|
||||
// decodeProgressStream reads the JSON messages the daemon streams while pulling or
|
||||
// pushing an image, handing each of them to onMessage. It stops at the first
|
||||
// message carrying an error, which it returns.
|
||||
func decodeProgressStream(stream io.Reader, onMessage func(jsonstream.Message)) error {
|
||||
dec := json.NewDecoder(stream)
|
||||
for {
|
||||
var jm jsonstream.Message
|
||||
if err := dec.Decode(&jm); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if jm.Error != nil {
|
||||
return errors.New(jm.Error.Message)
|
||||
}
|
||||
onMessage(jm)
|
||||
}
|
||||
}
|
||||
|
||||
// progressPercent is how far current is from total, which must be positive, in percent.
|
||||
func progressPercent(current, total int64) int {
|
||||
return min(int(current*100/total), 100)
|
||||
}
|
||||
|
|
|
|||
44
pkg/compose/progress_test.go
Normal file
44
pkg/compose/progress_test.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
Copyright 2020 Docker Compose CLI 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 compose
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/api/types/jsonstream"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestDecodeProgressStream(t *testing.T) {
|
||||
var ids []string
|
||||
err := decodeProgressStream(strings.NewReader(`{"id":"a"}{"id":"b"}`), func(jm jsonstream.Message) { ids = append(ids, jm.ID) })
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, ids, []string{"a", "b"})
|
||||
|
||||
ids = nil
|
||||
err = decodeProgressStream(strings.NewReader(`{"id":"a"}{"errorDetail":{"message":"boom"}}{"id":"c"}`), func(jm jsonstream.Message) { ids = append(ids, jm.ID) })
|
||||
assert.Error(t, err, "boom")
|
||||
assert.DeepEqual(t, ids, []string{"a"})
|
||||
|
||||
assert.Assert(t, decodeProgressStream(strings.NewReader(`{"id":`), func(jsonstream.Message) {}) != nil)
|
||||
}
|
||||
|
||||
func TestProgressPercent(t *testing.T) {
|
||||
assert.Equal(t, progressPercent(50, 200), 25)
|
||||
assert.Equal(t, progressPercent(300, 200), 100)
|
||||
}
|
||||
|
|
@ -18,10 +18,8 @@ package compose
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
|
@ -355,21 +353,12 @@ func (s *composeService) pullServiceImage(ctx context.Context, service types.Ser
|
|||
return err
|
||||
}
|
||||
|
||||
dec := json.NewDecoder(stream)
|
||||
for {
|
||||
var jm jsonstream.Message
|
||||
if err := dec.Decode(&jm); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
return err
|
||||
}
|
||||
if jm.Error != nil {
|
||||
return errors.New(jm.Error.Message)
|
||||
}
|
||||
if err := decodeProgressStream(stream, func(jm jsonstream.Message) {
|
||||
if !quietPull {
|
||||
toPullProgressEvent(resource, jm, s.events)
|
||||
}
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
s.events.On(progress.New(resource, api.Done, api.StatusPulled))
|
||||
|
||||
|
|
@ -626,7 +615,7 @@ func toPullProgressEvent(parent string, jm jsonstream.Message, events api.EventP
|
|||
current = jm.Progress.Current
|
||||
total = jm.Progress.Total
|
||||
if jm.Progress.Total > 0 {
|
||||
percent = min(int(jm.Progress.Current*100/jm.Progress.Total), 100)
|
||||
percent = progressPercent(jm.Progress.Current, jm.Progress.Total)
|
||||
}
|
||||
}
|
||||
case DownloadCompletePhase, AlreadyExistsPhase, PullCompletePhase:
|
||||
|
|
|
|||
|
|
@ -18,10 +18,7 @@ package compose
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/compose-spec/compose-go/v2/types"
|
||||
|
|
@ -114,25 +111,11 @@ func (s *composeService) pushServiceImage(ctx context.Context, tag string, quiet
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dec := json.NewDecoder(stream)
|
||||
for {
|
||||
var jm jsonstream.Message
|
||||
if err := dec.Decode(&jm); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
return err
|
||||
}
|
||||
if jm.Error != nil {
|
||||
return errors.New(jm.Error.Message)
|
||||
}
|
||||
|
||||
return decodeProgressStream(stream, func(jm jsonstream.Message) {
|
||||
if !quietPush {
|
||||
toPushProgressEvent(tag, jm, s.events)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func toPushProgressEvent(prefix string, jm jsonstream.Message, events api.EventProcessor) {
|
||||
|
|
@ -161,7 +144,7 @@ func toPushProgressEvent(prefix string, jm jsonstream.Message, events api.EventP
|
|||
current = jm.Progress.Current
|
||||
total = jm.Progress.Total
|
||||
if jm.Progress.Total > 0 {
|
||||
percent = min(int(jm.Progress.Current*100/jm.Progress.Total), 100)
|
||||
percent = progressPercent(jm.Progress.Current, jm.Progress.Total)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue