fix: Improve output for build operations

This commit is contained in:
Anton Ovchinnikov
2025-06-22 19:26:25 +02:00
parent 89fb9efcfa
commit 26f34df3e6
3 changed files with 15 additions and 33 deletions
+4 -1
View File
@@ -31,7 +31,10 @@ jobs:
go-version: "1.23.2" go-version: "1.23.2"
- name: Install dependencies - name: Install dependencies
run: go mod tidy run: |
go mod tidy
git diff --exit-code ||
(echo "go.mod or go.sum has changed. Please run 'go mod tidy' and commit the changes." && exit 1)
- name: Run tests - name: Run tests
run: make test run: make test
+1 -1
View File
@@ -37,6 +37,7 @@ require (
github.com/jmoiron/sqlx v1.4.0 github.com/jmoiron/sqlx v1.4.0
github.com/lmittmann/tint v1.0.5 github.com/lmittmann/tint v1.0.5
github.com/miekg/dns v1.1.65 github.com/miekg/dns v1.1.65
github.com/moby/term v0.5.0
github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0 github.com/opencontainers/image-spec v1.1.0
github.com/siderolabs/discovery-api v0.1.4 github.com/siderolabs/discovery-api v0.1.4
@@ -215,7 +216,6 @@ require (
github.com/moby/sys/signal v0.7.1 // indirect github.com/moby/sys/signal v0.7.1 // indirect
github.com/moby/sys/user v0.3.0 // indirect github.com/moby/sys/user v0.3.0 // indirect
github.com/moby/sys/userns v0.1.0 // indirect github.com/moby/sys/userns v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect github.com/morikuni/aec v1.0.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect github.com/mr-tron/base58 v1.2.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
+10 -31
View File
@@ -5,7 +5,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"os" "os"
composetypes "github.com/compose-spec/compose-go/v2/types" composetypes "github.com/compose-spec/compose-go/v2/types"
@@ -15,7 +14,9 @@ import (
"github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/image"
dockerclient "github.com/docker/docker/client" dockerclient "github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/registry" "github.com/docker/docker/registry"
"github.com/moby/term"
) )
type BuildOptions struct { type BuildOptions struct {
@@ -102,19 +103,10 @@ func buildSingleService(ctx context.Context, dockerCli *dockerclient.Client, ser
} }
defer buildResponse.Body.Close() defer buildResponse.Body.Close()
// Print the build output // Display the build response
decoder := json.NewDecoder(buildResponse.Body) fd, isTerminal := term.GetFdInfo(os.Stdout)
for { if err := jsonmessage.DisplayJSONMessagesStream(buildResponse.Body, os.Stdout, fd, isTerminal, nil); err != nil {
var message map[string]interface{} return "", fmt.Errorf("failed to display build response for service %s: %w", service.Name, err)
if err := decoder.Decode(&message); err == io.EOF {
break
} else if err != nil {
return "", fmt.Errorf("failed to decode build output for service %s: %w", service.Name, err)
}
if stream, ok := message["stream"]; ok {
fmt.Print(stream)
}
} }
return imageName, nil return imageName, nil
@@ -161,24 +153,11 @@ func pushSingleServiceImage(ctx context.Context, dockerCli *dockerclient.Client,
} }
defer pushResponse.Close() defer pushResponse.Close()
fmt.Printf("Pushing image %s for service %s\n", imageName, serviceName) fmt.Printf("Pushing image %s for service %s...\n", imageName, serviceName)
// Handle output and errors fd, isTerminal := term.GetFdInfo(os.Stdout)
decoder := json.NewDecoder(pushResponse) if err := jsonmessage.DisplayJSONMessagesStream(pushResponse, os.Stdout, fd, isTerminal, nil); err != nil {
for { return fmt.Errorf("failed to display push response for image %s: %w", imageName, err)
var message map[string]interface{}
if err := decoder.Decode(&message); err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("failed to decode push output for image %s: %w", imageName, err)
}
if stream, ok := message["stream"]; ok {
fmt.Print(stream)
} else if errorMessage, ok := message["error"]; ok {
return fmt.Errorf("error pushing image %s: %s", imageName, errorMessage)
} else if status, ok := message["status"]; ok {
fmt.Printf(" %s\n", status)
}
} }
fmt.Printf("Image %s pushed successfully.\n", imageName) fmt.Printf("Image %s pushed successfully.\n", imageName)