compose/internal/envresolver/envresolver.go
Nicolas De Loof 31984b13d4 refactor(compose): move the image build to internal/build
The classic and bake builders, with the dispatch between them, become
internal/build, which declares what it needs from the engine as a small
Backend. composeService implements it and keeps a one-line build
forwarder, so Build, watch and the image phase of up and run do not change.

The environment variable resolver, shared with run, becomes its own
internal/envresolver package, and the Building/Built events move to
internal/progress next to the other events.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-11-16 15:11:08 +01:00

67 lines
2.3 KiB
Go

/*
Copyright 2020 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 envresolver resolves environment variables the way the platform does:
// case-sensitively, except on Windows.
package envresolver
import (
"runtime"
"strings"
)
// isCaseInsensitiveEnvVars is true on platforms where environment variable names are treated case-insensitively.
var isCaseInsensitiveEnvVars = (runtime.GOOS == "windows")
// New returns resolver for environment variables suitable for the current platform.
// Expected to be used with `MappingWithEquals.Resolve`.
// Updates in `environment` may not be reflected.
func New(environment map[string]string) func(string) (string, bool) {
return NewWithCase(environment, isCaseInsensitiveEnvVars)
}
// NewWithCase returns resolver for environment variables with the specified case-sensitive condition.
// Expected to be used with `MappingWithEquals.Resolve`.
// Updates in `environment` may not be reflected.
func NewWithCase(environment map[string]string, caseInsensitive bool) func(string) (string, bool) {
if environment == nil {
return func(s string) (string, bool) {
return "", false
}
}
if !caseInsensitive {
return func(s string) (string, bool) {
v, ok := environment[s]
return v, ok
}
}
// variable names must be treated case-insensitively.
// Resolves in this way:
// * Return the value if its name matches with the passed name case-sensitively.
// * Otherwise, return the value if its name case-insensitively matches the passed name.
// * The value is indefinite if multiple variable matches.
return func(s string) (string, bool) {
if v, ok := environment[s]; ok {
return v, ok
}
for k, v := range environment {
if strings.EqualFold(k, s) {
return v, true
}
}
return "", false
}
}