mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-10-31 16:13:45 +08:00 
			
		
		
		
	build: enhance metadata json output
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
		| @@ -13,7 +13,6 @@ import ( | |||||||
| 	"github.com/docker/buildx/util/progress" | 	"github.com/docker/buildx/util/progress" | ||||||
| 	"github.com/docker/buildx/util/tracing" | 	"github.com/docker/buildx/util/tracing" | ||||||
| 	"github.com/docker/cli/cli/command" | 	"github.com/docker/cli/cli/command" | ||||||
| 	"github.com/docker/docker/pkg/ioutils" |  | ||||||
| 	"github.com/moby/buildkit/util/appcontext" | 	"github.com/moby/buildkit/util/appcontext" | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| 	"github.com/spf13/cobra" | 	"github.com/spf13/cobra" | ||||||
| @@ -152,16 +151,20 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if len(in.metadataFile) > 0 && resp != nil { | 	if len(in.metadataFile) > 0 && resp != nil { | ||||||
| 		mdata := map[string]map[string]string{} | 		if len(resp) == 1 { | ||||||
| 		for k, r := range resp { | 			for _, r := range resp { | ||||||
| 			mdata[k] = r.ExporterResponse | 				if err := writeMetadataFile(in.metadataFile, decodeExporterResponse(r.ExporterResponse)); err != nil { | ||||||
| 		} | 					return err | ||||||
| 		mdatab, err := json.MarshalIndent(mdata, "", "  ") | 				} | ||||||
| 		if err != nil { | 			} | ||||||
| 			return err | 		} else { | ||||||
| 		} | 			dt := make(map[string]interface{}) | ||||||
| 		if err := ioutils.AtomicWriteFile(in.metadataFile, mdatab, 0644); err != nil { | 			for t, r := range resp { | ||||||
| 			return err | 				dt[t] = decodeExporterResponse(r.ExporterResponse) | ||||||
|  | 			} | ||||||
|  | 			if err := writeMetadataFile(in.metadataFile, dt); err != nil { | ||||||
|  | 				return err | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ package commands | |||||||
| import ( | import ( | ||||||
| 	"bytes" | 	"bytes" | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"encoding/base64" | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| @@ -251,11 +252,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if len(metadataFile) > 0 && resp != nil { | 	if len(metadataFile) > 0 && resp != nil { | ||||||
| 		mdatab, err := json.MarshalIndent(resp[defaultTargetName].ExporterResponse, "", "  ") | 		if err := writeMetadataFile(metadataFile, decodeExporterResponse(resp[defaultTargetName].ExporterResponse)); err != nil { | ||||||
| 		if err != nil { |  | ||||||
| 			return "", err |  | ||||||
| 		} |  | ||||||
| 		if err := ioutils.AtomicWriteFile(metadataFile, mdatab, 0644); err != nil { |  | ||||||
| 			return "", err | 			return "", err | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -496,6 +493,32 @@ func parseContextNames(values []string) (map[string]string, error) { | |||||||
| 	return result, nil | 	return result, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func writeMetadataFile(filename string, dt interface{}) error { | ||||||
|  | 	b, err := json.MarshalIndent(dt, "", "  ") | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	return ioutils.AtomicWriteFile(filename, b, 0644) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func decodeExporterResponse(exporterResponse map[string]string) map[string]interface{} { | ||||||
|  | 	out := make(map[string]interface{}) | ||||||
|  | 	for k, v := range exporterResponse { | ||||||
|  | 		dt, err := base64.StdEncoding.DecodeString(v) | ||||||
|  | 		if err != nil { | ||||||
|  | 			out[k] = v | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  | 		var raw map[string]interface{} | ||||||
|  | 		if err = json.Unmarshal(dt, &raw); err != nil || len(raw) == 0 { | ||||||
|  | 			out[k] = v | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  | 		out[k] = json.RawMessage(dt) | ||||||
|  | 	} | ||||||
|  | 	return out | ||||||
|  | } | ||||||
|  |  | ||||||
| func wrapBuildError(err error) error { | func wrapBuildError(err error) error { | ||||||
| 	if err == nil { | 	if err == nil { | ||||||
| 		return nil | 		return nil | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 CrazyMax
					CrazyMax