support for image pull logs
This commit is contained in:
61
internal/docker/jsonmessage/jsonmessage.go
Normal file
61
internal/docker/jsonmessage/jsonmessage.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Polyform License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package jsonmessage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type jsonError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func (e *jsonError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
type jsonMessage struct {
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Error *jsonError `json:"errorDetail"`
|
||||
Progress *jsonProgress `json:"progressDetail"`
|
||||
}
|
||||
|
||||
type jsonProgress struct {
|
||||
}
|
||||
|
||||
// Copy copies a json message string to the io.Writer.
|
||||
func Copy(in io.Reader, out io.Writer) error {
|
||||
dec := json.NewDecoder(in)
|
||||
for {
|
||||
var jm jsonMessage
|
||||
if err := dec.Decode(&jm); err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if jm.Error != nil {
|
||||
if jm.Error.Code == 401 {
|
||||
return fmt.Errorf("authentication is required")
|
||||
}
|
||||
return jm.Error
|
||||
}
|
||||
|
||||
if jm.Progress != nil {
|
||||
continue
|
||||
}
|
||||
if jm.ID == "" {
|
||||
fmt.Fprintf(out, "%s\n", jm.Status)
|
||||
} else {
|
||||
fmt.Fprintf(out, "%s: %s\n", jm.ID, jm.Status)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
12
internal/docker/jsonmessage/testdata/alpine.json
vendored
Normal file
12
internal/docker/jsonmessage/testdata/alpine.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
{"status":"Pulling from library/alpine","id":"3.6"}
|
||||
{"status":"Pulling fs layer","progressDetail":{},"id":"5a3ea8efae5d"}
|
||||
{"status":"Downloading","progressDetail":{"current":21019,"total":2017774},"progress":"[\u003e ] 21.02kB/2.018MB","id":"5a3ea8efae5d"}
|
||||
{"status":"Downloading","progressDetail":{"current":1194721,"total":2017774},"progress":"[=============================\u003e ] 1.195MB/2.018MB","id":"5a3ea8efae5d"}
|
||||
{"status":"Verifying Checksum","progressDetail":{},"id":"5a3ea8efae5d"}
|
||||
{"status":"Download complete","progressDetail":{},"id":"5a3ea8efae5d"}
|
||||
{"status":"Extracting","progressDetail":{"current":32768,"total":2017774},"progress":"[\u003e ] 32.77kB/2.018MB","id":"5a3ea8efae5d"}
|
||||
{"status":"Extracting","progressDetail":{"current":786432,"total":2017774},"progress":"[===================\u003e ] 786.4kB/2.018MB","id":"5a3ea8efae5d"}
|
||||
{"status":"Extracting","progressDetail":{"current":2017774,"total":2017774},"progress":"[==================================================\u003e] 2.018MB/2.018MB","id":"5a3ea8efae5d"}
|
||||
{"status":"Pull complete","progressDetail":{},"id":"5a3ea8efae5d"}
|
||||
{"status":"Digest: sha256:66790a2b79e1ea3e1dabac43990c54aca5d1ddf268d9a5a0285e4167c8b24475"}
|
||||
{"status":"Status: Downloaded newer image for alpine:3.6"}
|
||||
Reference in New Issue
Block a user