@@ -12,6 +12,25 @@ import (
1212 "github.com/moby/moby/api/types/jsonstream"
1313)
1414
15+ // jsonMessage defines a message struct. It describes
16+ // the created time, where it from, status, ID of the
17+ // message. It's used for docker events.
18+ //
19+ // It is a reduced set of [jsonmessage.JSONMessage].
20+ type jsonMessage struct {
21+ Stream string `json:"stream,omitempty"`
22+ Status string `json:"status,omitempty"`
23+ Progress * jsonmessage.JSONProgress `json:"progressDetail,omitempty"` // TODO(thaJeztah): can this be a [jsonstream.Progress]?
24+ ID string `json:"id,omitempty"`
25+ Error * jsonstream.Error `json:"errorDetail,omitempty"`
26+ Aux * json.RawMessage `json:"aux,omitempty"` // Aux contains out-of-band data, such as digests for push signing and image id after building.
27+
28+ // ErrorMessage contains errors encountered during the operation.
29+ //
30+ // Deprecated: this field is deprecated since docker v0.6.0 / API v1.4. Use [Error.Message] instead. This field will be omitted in a future release.
31+ ErrorMessage string `json:"error,omitempty"` // deprecated
32+ }
33+
1534const streamNewline = "\r \n "
1635
1736type jsonProgressFormatter struct {}
@@ -23,7 +42,7 @@ func appendNewline(source []byte) []byte {
2342// FormatStatus formats the specified objects according to the specified format (and id).
2443func FormatStatus (id , format string , a ... interface {}) []byte {
2544 str := fmt .Sprintf (format , a ... )
26- b , err := json .Marshal (& jsonmessage. JSONMessage {ID : id , Status : str })
45+ b , err := json .Marshal (& jsonMessage {ID : id , Status : str })
2746 if err != nil {
2847 return FormatError (err )
2948 }
@@ -36,7 +55,7 @@ func FormatError(err error) []byte {
3655 if ! ok {
3756 jsonError = & jsonstream.Error {Message : err .Error ()}
3857 }
39- if b , err := json .Marshal (& jsonmessage. JSONMessage {Error : jsonError , ErrorMessage : err .Error ()}); err == nil {
58+ if b , err := json .Marshal (& jsonMessage {Error : jsonError , ErrorMessage : err .Error ()}); err == nil {
4059 return appendNewline (b )
4160 }
4261 return []byte (`{"error":"format error"}` + streamNewline )
@@ -47,9 +66,9 @@ func (sf *jsonProgressFormatter) formatStatus(id, format string, a ...interface{
4766}
4867
4968// formatProgress formats the progress information for a specified action.
50- func (sf * jsonProgressFormatter ) formatProgress (id , action string , progress * jsonmessage. JSONProgress , aux interface {}) []byte {
69+ func (sf * jsonProgressFormatter ) formatProgress (id , action string , progress * jsonstream. Progress , aux interface {}) []byte {
5170 if progress == nil {
52- progress = & jsonmessage. JSONProgress {}
71+ progress = & jsonstream. Progress {}
5372 }
5473 var auxJSON * json.RawMessage
5574 if aux != nil {
@@ -60,12 +79,11 @@ func (sf *jsonProgressFormatter) formatProgress(id, action string, progress *jso
6079 auxJSON = new (json.RawMessage )
6180 * auxJSON = auxJSONBytes
6281 }
63- b , err := json .Marshal (& jsonmessage.JSONMessage {
64- Status : action ,
65- ProgressMessage : progress .String (),
66- Progress : progress ,
67- ID : id ,
68- Aux : auxJSON ,
82+ b , err := json .Marshal (& jsonMessage {
83+ Status : action ,
84+ Progress : & jsonmessage.JSONProgress {Progress : * progress },
85+ ID : id ,
86+ Aux : auxJSON ,
6987 })
7088 if err != nil {
7189 return nil
@@ -79,15 +97,16 @@ func (sf *rawProgressFormatter) formatStatus(id, format string, a ...interface{}
7997 return []byte (fmt .Sprintf (format , a ... ) + streamNewline )
8098}
8199
82- func (sf * rawProgressFormatter ) formatProgress (id , action string , progress * jsonmessage. JSONProgress , aux interface {}) []byte {
100+ func (sf * rawProgressFormatter ) formatProgress (id , action string , progress * jsonstream. Progress , aux interface {}) []byte {
83101 if progress == nil {
84- progress = & jsonmessage. JSONProgress {}
102+ progress = & jsonstream. Progress {}
85103 }
86104 endl := "\r "
87- if progress .String () == "" {
105+ out := (& jsonmessage.JSONProgress {Progress : * progress }).String ()
106+ if out == "" {
88107 endl += "\n "
89108 }
90- return []byte (action + " " + progress . String () + endl )
109+ return []byte (action + " " + out + endl )
91110}
92111
93112// NewProgressOutput returns a progress.Output object that can be passed to
@@ -104,7 +123,7 @@ func NewJSONProgressOutput(out io.Writer, newLines bool) progress.Output {
104123
105124type formatProgress interface {
106125 formatStatus (id , format string , a ... interface {}) []byte
107- formatProgress (id , action string , progress * jsonmessage. JSONProgress , aux interface {}) []byte
126+ formatProgress (id , action string , progress * jsonstream. Progress , aux interface {}) []byte
108127}
109128
110129type progressOutput struct {
@@ -120,13 +139,11 @@ func (out *progressOutput) WriteProgress(prog progress.Progress) error {
120139 if prog .Message != "" {
121140 formatted = out .sf .formatStatus (prog .ID , prog .Message )
122141 } else {
123- jsonProgress := jsonmessage.JSONProgress {
124- Progress : jsonstream.Progress {
125- Current : prog .Current ,
126- Total : prog .Total ,
127- HideCounts : prog .HideCounts ,
128- Units : prog .Units ,
129- },
142+ jsonProgress := jsonstream.Progress {
143+ Current : prog .Current ,
144+ Total : prog .Total ,
145+ HideCounts : prog .HideCounts ,
146+ Units : prog .Units ,
130147 }
131148 formatted = out .sf .formatProgress (prog .ID , prog .Action , & jsonProgress , prog .Aux )
132149 }
@@ -159,7 +176,7 @@ func (sf *AuxFormatter) Emit(id string, aux interface{}) error {
159176 }
160177 auxJSON := new (json.RawMessage )
161178 * auxJSON = auxJSONBytes
162- msgJSON , err := json .Marshal (& jsonmessage. JSONMessage {ID : id , Aux : auxJSON })
179+ msgJSON , err := json .Marshal (& jsonMessage {ID : id , Aux : auxJSON })
163180 if err != nil {
164181 return err
165182 }
0 commit comments