Merge pull request #1628 from ulyssessouza/add-bypass-envvar

Support bypass envvars
This commit is contained in:
Nicolas De loof 2021-05-07 07:25:14 +02:00 committed by GitHub
commit db6978aa57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 20 deletions

View File

@ -247,22 +247,6 @@ func (e Event) String() string {
} }
// EnvironmentMap return RunOptions.Environment as a MappingWithEquals
func (opts *RunOptions) EnvironmentMap() types.MappingWithEquals {
environment := types.MappingWithEquals{}
for _, s := range opts.Environment {
parts := strings.SplitN(s, "=", 2)
key := parts[0]
switch {
case len(parts) == 1:
environment[key] = nil
default:
environment[key] = &parts[1]
}
}
return environment
}
// ListOptions group options of the ls API // ListOptions group options of the ls API
type ListOptions struct { type ListOptions struct {
All bool All bool

View File

@ -19,6 +19,7 @@ package compose
import ( import (
"testing" "testing"
"github.com/compose-spec/compose-go/types"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
) )
@ -30,7 +31,7 @@ func TestRunOptionsEnvironmentMap(t *testing.T) {
"QIX", "QIX",
}, },
} }
env := opts.EnvironmentMap() env := types.NewMappingWithEquals(opts.Environment)
assert.Equal(t, *env["FOO"], "BAR") assert.Equal(t, *env["FOO"], "BAR")
assert.Equal(t, *env["ZOT"], "") assert.Equal(t, *env["ZOT"], "")
assert.Check(t, env["QIX"] == nil) assert.Check(t, env["QIX"] == nil)

View File

@ -42,7 +42,7 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
return 0, err return 0, err
} }
applyRunOptions(&service, opts) applyRunOptions(project, &service, opts)
slug := moby.GenerateRandomID() slug := moby.GenerateRandomID()
if service.ContainerName == "" { if service.ContainerName == "" {
@ -107,7 +107,7 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
} }
func applyRunOptions(service *types.ServiceConfig, opts compose.RunOptions) { func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts compose.RunOptions) {
service.Tty = opts.Tty service.Tty = opts.Tty
service.ContainerName = opts.Name service.ContainerName = opts.Name
@ -124,7 +124,12 @@ func applyRunOptions(service *types.ServiceConfig, opts compose.RunOptions) {
service.Entrypoint = opts.Entrypoint service.Entrypoint = opts.Entrypoint
} }
if len(opts.Environment) > 0 { if len(opts.Environment) > 0 {
service.Environment.OverrideBy(opts.EnvironmentMap()) env := types.NewMappingWithEquals(opts.Environment)
projectEnv := env.Resolve(func(s string) (string, bool) {
v, ok := project.Environment[s]
return v, ok
}).RemoveEmpty()
service.Environment.OverrideBy(projectEnv)
} }
for k, v := range opts.Labels { for k, v := range opts.Labels {
service.Labels.Add(k, v) service.Labels.Add(k, v)