opts: deprecate ParseEnvFile

It was a wrapper around kvfile.Load, which should be used instead.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-20 14:55:28 +02:00
parent 4d93a6486e
commit e650803f09
5 changed files with 34 additions and 17 deletions

View File

@ -0,0 +1,26 @@
package loader
import (
"os"
"github.com/docker/cli/pkg/kvfile"
)
// parseEnvFile reads a file with environment variables enumerated by lines
//
// “Environment variable names used by the utilities in the Shell and
// Utilities volume of [IEEE Std 1003.1-2001] consist solely of uppercase
// letters, digits, and the '_' (underscore) from the characters defined in
// Portable Character Set and do not begin with a digit. *But*, other
// characters may be permitted by an implementation; applications shall
// tolerate the presence of such names.”
//
// As of [moby-16585], it's up to application inside docker to validate or not
// environment variables, that's why we just strip leading whitespace and
// nothing more.
//
// [IEEE Std 1003.1-2001]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
// [moby-16585]: https://github.com/moby/moby/issues/16585
func parseEnvFile(filename string) ([]string, error) {
return kvfile.Parse(filename, os.LookupEnv)
}

View File

@ -0,0 +1,40 @@
package loader
import (
"os"
"path/filepath"
"testing"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func tmpFileWithContent(t *testing.T, content string) string {
t.Helper()
fileName := filepath.Join(t.TempDir(), "envfile")
err := os.WriteFile(fileName, []byte(content), 0o644)
assert.NilError(t, err)
return fileName
}
// Test parseEnvFile for a non existent file
func TestParseEnvFileNonExistentFile(t *testing.T) {
_, err := parseEnvFile("no_such_file")
assert.Check(t, is.ErrorType(err, os.IsNotExist))
}
// parseEnvFile with environment variable import definitions
func TestParseEnvVariableDefinitionsFile(t *testing.T) {
content := `# comment=
UNDEFINED_VAR
DEFINED_VAR
`
tmpFile := tmpFileWithContent(t, content)
t.Setenv("DEFINED_VAR", "defined-value")
variables, err := parseEnvFile(tmpFile)
assert.NilError(t, err)
expectedLines := []string{"DEFINED_VAR=defined-value"}
assert.Check(t, is.DeepEqual(variables, expectedLines))
}

View File

@ -469,7 +469,7 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l
for _, file := range serviceConfig.EnvFile {
filePath := absPath(workingDir, file)
fileVars, err := opts.ParseEnvFile(filePath)
fileVars, err := parseEnvFile(filePath)
if err != nil {
return err
}