mirror of
https://github.com/docker/compose.git
synced 2026-11-17 13:45:39 +00:00
Rename-only change, no behavior difference, no exported identifier renamed (only parameter names of a few exported functions, which are not part of the Go API). - names that hide what a value is: parameters holding a *name* or *ID* while named like the object — project→projectName, service→serviceName, container→containerID — so string vs struct is readable at every call site without opening the signature - misleading receivers: monitor methods used c (reads as client/container/CLI) → m; jsonWriter used p while its ttyWriter sibling uses w → w - cryptic abbreviations with non-trivial scope: nw (meant both network.Summary and types.NetworkConfig) → network/networkConfig, svc → service/serviceName/serviceCopy, oc/ocs → observedContainer(s), cnts → serviceContainers, cnx → attachResponse - container.Summary loop variables unified on ctr (was a c/ctr mix); fillBindMounts also dropped its p/s/m single-letters, one of which was shadowed by a loop variable of a different type - option-struct identifiers unified on options (opts remains only for variadic functional options); ambiguous ones get a precise name (bindOptions, networkCreateOptions, projectOptionsFns) Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
/*
|
|
Copyright 2024 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 display
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/docker/compose/v5/pkg/api"
|
|
)
|
|
|
|
func JSON(out io.Writer) api.EventProcessor {
|
|
return &jsonWriter{
|
|
out: out,
|
|
}
|
|
}
|
|
|
|
type jsonWriter struct {
|
|
out io.Writer
|
|
dryRun bool
|
|
}
|
|
|
|
type jsonMessage struct {
|
|
DryRun bool `json:"dry-run,omitempty"`
|
|
Tail bool `json:"tail,omitempty"`
|
|
ID string `json:"id,omitempty"`
|
|
ParentID string `json:"parent_id,omitempty"`
|
|
Status string `json:"status,omitempty"`
|
|
Text string `json:"text,omitempty"`
|
|
Details string `json:"details,omitempty"`
|
|
Current int64 `json:"current,omitempty"`
|
|
Total int64 `json:"total,omitempty"`
|
|
Percent int `json:"percent,omitempty"`
|
|
}
|
|
|
|
func (w *jsonWriter) Start(ctx context.Context, operation string) {
|
|
}
|
|
|
|
func (w *jsonWriter) Event(e api.Resource) {
|
|
message := &jsonMessage{
|
|
DryRun: w.dryRun,
|
|
Tail: false,
|
|
ID: e.ID,
|
|
Status: e.StatusText(),
|
|
Text: e.Text,
|
|
Details: e.Details,
|
|
ParentID: e.ParentID,
|
|
Current: e.Current,
|
|
Total: e.Total,
|
|
Percent: e.Percent,
|
|
}
|
|
marshal, err := json.Marshal(message)
|
|
if err == nil {
|
|
_, _ = fmt.Fprintln(w.out, string(marshal))
|
|
}
|
|
}
|
|
|
|
func (w *jsonWriter) On(events ...api.Resource) {
|
|
for _, e := range events {
|
|
w.Event(e)
|
|
}
|
|
}
|
|
|
|
func (w *jsonWriter) Done(_ string, _ bool) {
|
|
}
|