mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-01 00:23:56 +08:00 
			
		
		
		
	Set ConfigFile to parse compose files with bake
				
					
				
			Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
		| @@ -11,15 +11,10 @@ import ( | ||||
| ) | ||||
|  | ||||
| func parseCompose(dt []byte) (*compose.Project, error) { | ||||
| 	config, err := loader.ParseYAML(dt) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	return loader.Load(compose.ConfigDetails{ | ||||
| 		ConfigFiles: []compose.ConfigFile{ | ||||
| 			{ | ||||
| 				Config: config, | ||||
| 				Content: dt, | ||||
| 			}, | ||||
| 		}, | ||||
| 		Environment: envMap(os.Environ()), | ||||
| @@ -80,7 +75,10 @@ func ParseCompose(dt []byte) (*Config, error) { | ||||
| 				Context:    contextPathP, | ||||
| 				Dockerfile: dockerfilePathP, | ||||
| 				Labels:     s.Build.Labels, | ||||
| 				Args:       toMap(s.Build.Args), | ||||
| 				Args: flatten(s.Build.Args.Resolve(func(val string) (string, bool) { | ||||
| 					val, ok := cfg.Environment[val] | ||||
| 					return val, ok | ||||
| 				})), | ||||
| 				CacheFrom: s.Build.CacheFrom, | ||||
| 				// TODO: add platforms | ||||
| 			} | ||||
| @@ -100,14 +98,16 @@ func ParseCompose(dt []byte) (*Config, error) { | ||||
| 	return &c, nil | ||||
| } | ||||
|  | ||||
| func toMap(in compose.MappingWithEquals) map[string]string { | ||||
| 	m := map[string]string{} | ||||
| func flatten(in compose.MappingWithEquals) compose.Mapping { | ||||
| 	if len(in) == 0 { | ||||
| 		return nil | ||||
| 	} | ||||
| 	out := compose.Mapping{} | ||||
| 	for k, v := range in { | ||||
| 		if v != nil { | ||||
| 			m[k] = *v | ||||
| 		} else { | ||||
| 			m[k] = os.Getenv(k) | ||||
| 		if v == nil { | ||||
| 			continue | ||||
| 		} | ||||
| 		out[k] = *v | ||||
| 	} | ||||
| 	return m | ||||
| 	return out | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| package bake | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"sort" | ||||
| 	"testing" | ||||
|  | ||||
| @@ -107,6 +108,35 @@ services: | ||||
| 	require.Equal(t, "webapp", *c.Targets[1].Target) | ||||
| } | ||||
|  | ||||
| func TestBuildArgEnvCompose(t *testing.T) { | ||||
| 	var dt = []byte(` | ||||
| version: "3.8" | ||||
| services: | ||||
|   example: | ||||
|     image: example | ||||
|     build: | ||||
|       context: . | ||||
|       dockerfile: Dockerfile | ||||
|       args: | ||||
|         FOO: | ||||
|         BAR: $ZZZ_BAR | ||||
|         BRB: FOO | ||||
| `) | ||||
|  | ||||
| 	os.Setenv("FOO", "bar") | ||||
| 	defer os.Unsetenv("FOO") | ||||
| 	os.Setenv("BAR", "foo") | ||||
| 	defer os.Unsetenv("BAR") | ||||
| 	os.Setenv("ZZZ_BAR", "zzz_foo") | ||||
| 	defer os.Unsetenv("ZZZ_BAR") | ||||
|  | ||||
| 	c, err := ParseCompose(dt) | ||||
| 	require.NoError(t, err) | ||||
| 	require.Equal(t, c.Targets[0].Args["FOO"], "bar") | ||||
| 	require.Equal(t, c.Targets[0].Args["BAR"], "zzz_foo") | ||||
| 	require.Equal(t, c.Targets[0].Args["BRB"], "FOO") | ||||
| } | ||||
|  | ||||
| func TestBogusCompose(t *testing.T) { | ||||
| 	var dt = []byte(` | ||||
| services: | ||||
|   | ||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							| @@ -9,7 +9,7 @@ require ( | ||||
| 	github.com/bugsnag/panicwrap v1.2.0 // indirect | ||||
| 	github.com/cenkalti/backoff v2.1.1+incompatible // indirect | ||||
| 	github.com/cloudflare/cfssl v0.0.0-20181213083726-b94e044bb51e // indirect | ||||
| 	github.com/compose-spec/compose-go v0.0.0-20210706130854-69459d4976b5 | ||||
| 	github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c | ||||
| 	github.com/containerd/console v1.0.2 | ||||
| 	github.com/containerd/containerd v1.5.4 | ||||
| 	github.com/denisenkom/go-mssqldb v0.0.0-20190315220205-a8ed825ac853 // indirect | ||||
|   | ||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							| @@ -138,8 +138,8 @@ github.com/cloudflare/cfssl v0.0.0-20181213083726-b94e044bb51e/go.mod h1:yMWuSON | ||||
| github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= | ||||
| github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= | ||||
| github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= | ||||
| github.com/compose-spec/compose-go v0.0.0-20210706130854-69459d4976b5 h1:PpI72CT1bcVPNZyqI1HI8UhQVRVtqLb2tdwi5WphN3c= | ||||
| github.com/compose-spec/compose-go v0.0.0-20210706130854-69459d4976b5/go.mod h1:5V65rPnTvvQagtoMxTneJ2QicLq6ZRQQ7fOgPN226fo= | ||||
| github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c h1:lSR4wokZlq+Q8uJpgZuFMs3VoLaYVV07cJOZHa1zRBg= | ||||
| github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c/go.mod h1:5V65rPnTvvQagtoMxTneJ2QicLq6ZRQQ7fOgPN226fo= | ||||
| github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE= | ||||
| github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU= | ||||
| github.com/containerd/aufs v0.0.0-20210316121734-20793ff83c97/go.mod h1:kL5kd6KM5TzQjR79jljyi4olc1Vrx6XBlcyj3gNv2PU= | ||||
|   | ||||
							
								
								
									
										23
									
								
								vendor/github.com/compose-spec/compose-go/loader/loader.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										23
									
								
								vendor/github.com/compose-spec/compose-go/loader/loader.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -217,16 +217,35 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types. | ||||
|  | ||||
| func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) { | ||||
| 	if !opts.SkipInterpolation { | ||||
| 		substitute, err := opts.Interpolate.Substitute(string(b), template.Mapping(opts.Interpolate.LookupValue)) | ||||
| 		withoutComments, err := removeYamlComments(b) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		b = []byte(substitute) | ||||
|  | ||||
| 		substituted, err := opts.Interpolate.Substitute(string(withoutComments), template.Mapping(opts.Interpolate.LookupValue)) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		b = []byte(substituted) | ||||
| 	} | ||||
|  | ||||
| 	return ParseYAML(b) | ||||
| } | ||||
|  | ||||
| // removeYamlComments drop all comments from the yaml file, so we don't try to apply string substitutions on irrelevant places | ||||
| func removeYamlComments(b []byte) ([]byte, error) { | ||||
| 	var cfg interface{} | ||||
| 	err := yaml.Unmarshal(b, &cfg) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	b, err = yaml.Marshal(cfg) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return b, nil | ||||
| } | ||||
|  | ||||
| func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} { | ||||
| 	extras := map[string]interface{}{} | ||||
| 	for key, value := range dict { | ||||
|   | ||||
							
								
								
									
										9
									
								
								vendor/github.com/compose-spec/compose-go/template/template.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										9
									
								
								vendor/github.com/compose-spec/compose-go/template/template.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -20,6 +20,8 @@ import ( | ||||
| 	"fmt" | ||||
| 	"regexp" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/sirupsen/logrus" | ||||
| ) | ||||
|  | ||||
| var delimiter = "\\$" | ||||
| @@ -61,7 +63,7 @@ type Mapping func(string) (string, bool) | ||||
| // the substitution and an error. | ||||
| type SubstituteFunc func(string, Mapping) (string, bool, error) | ||||
|  | ||||
| // SubstituteWith subsitute variables in the string with their values. | ||||
| // SubstituteWith substitute variables in the string with their values. | ||||
| // It accepts additional substitute function. | ||||
| func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) { | ||||
| 	var err error | ||||
| @@ -97,7 +99,10 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su | ||||
| 			return value | ||||
| 		} | ||||
|  | ||||
| 		value, _ := mapping(substitution) | ||||
| 		value, ok := mapping(substitution) | ||||
| 		if !ok { | ||||
| 			logrus.Warnf("The %q variable is not set. Defaulting to a blank string.", substitution) | ||||
| 		} | ||||
| 		return value | ||||
| 	}) | ||||
|  | ||||
|   | ||||
							
								
								
									
										4
									
								
								vendor/github.com/compose-spec/compose-go/types/config.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								vendor/github.com/compose-spec/compose-go/types/config.go
									
									
									
										generated
									
									
										vendored
									
									
								
							| @@ -20,7 +20,6 @@ import ( | ||||
| 	"encoding/json" | ||||
|  | ||||
| 	"github.com/mitchellh/mapstructure" | ||||
| 	"github.com/sirupsen/logrus" | ||||
| ) | ||||
|  | ||||
| // ConfigDetails are the details about a group of ConfigFiles | ||||
| @@ -34,9 +33,6 @@ type ConfigDetails struct { | ||||
| // LookupEnv provides a lookup function for environment variables | ||||
| func (cd ConfigDetails) LookupEnv(key string) (string, bool) { | ||||
| 	v, ok := cd.Environment[key] | ||||
| 	if !ok { | ||||
| 		logrus.Warnf("The %s variable is not set. Defaulting to a blank string.", key) | ||||
| 	} | ||||
| 	return v, ok | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										2
									
								
								vendor/modules.txt
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								vendor/modules.txt
									
									
									
									
										vendored
									
									
								
							| @@ -23,7 +23,7 @@ github.com/beorn7/perks/quantile | ||||
| github.com/cenkalti/backoff/v4 | ||||
| # github.com/cespare/xxhash/v2 v2.1.1 | ||||
| github.com/cespare/xxhash/v2 | ||||
| # github.com/compose-spec/compose-go v0.0.0-20210706130854-69459d4976b5 | ||||
| # github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c | ||||
| github.com/compose-spec/compose-go/errdefs | ||||
| github.com/compose-spec/compose-go/interpolation | ||||
| github.com/compose-spec/compose-go/loader | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 CrazyMax
					CrazyMax