MCPcopy Create free account
hub / github.com/coder/envbuilder / initDockerConfigOverride

Function initDockerConfigOverride

envbuilder.go:1705–1798  ·  view source on GitHub ↗

initDockerConfigOverride sets the DOCKER_CONFIG environment variable to a path within the working directory. If a base64 encoded Docker config is provided, it is written to the path/config.json and the DOCKER_CONFIG environment variable is set to the path. If no base64 encoded Docker config is provi

(bfs billy.Filesystem, logf log.Func, workingDir workingdir.WorkingDir, dockerConfigBase64 string)

Source from the content-addressed store, hash-verified

1703//
1704// If a Docker config file is found, its path is set as DOCKER_CONFIG.
1705func initDockerConfigOverride(bfs billy.Filesystem, logf log.Func, workingDir workingdir.WorkingDir, dockerConfigBase64 string) (func() error, error) {
1706 // If dockerConfigBase64 is set, it will have priority over file
1707 // detection.
1708 var dockerConfigJSON []byte
1709 var err error
1710 if dockerConfigBase64 != "" {
1711 logf(log.LevelInfo, "Using base64 encoded Docker config")
1712
1713 dockerConfigJSON, err = base64.StdEncoding.DecodeString(dockerConfigBase64)
1714 if err != nil {
1715 return nil, fmt.Errorf("decode docker config: %w", err)
1716 }
1717 }
1718
1719 oldDockerConfig := os.Getenv(dockerConfigEnvKey)
1720 var oldDockerConfigFile string
1721 if oldDockerConfig != "" {
1722 oldDockerConfigFile = filepath.Join(oldDockerConfig, dockerConfigFile)
1723 }
1724 for _, path := range []string{
1725 oldDockerConfigFile, // $DOCKER_CONFIG/config.json
1726 oldDockerConfig, // $DOCKER_CONFIG
1727 workingDir.Join(dockerConfigFile), // /.envbuilder/config.json
1728 } {
1729 if path == "" || !fileExists(bfs, path) {
1730 continue
1731 }
1732
1733 logf(log.LevelWarn, "Found Docker config at %s, this file will remain after the build", path)
1734
1735 if dockerConfigJSON == nil {
1736 logf(log.LevelInfo, "Using Docker config at %s", path)
1737
1738 dockerConfigJSON, err = readFile(bfs, path)
1739 if err != nil {
1740 return nil, fmt.Errorf("read docker config: %w", err)
1741 }
1742 } else {
1743 logf(log.LevelWarn, "Ignoring Docker config at %s, using base64 encoded Docker config instead", path)
1744 }
1745 break
1746 }
1747
1748 if dockerConfigJSON == nil {
1749 // No user-provided config available.
1750 return func() error { return nil }, nil
1751 }
1752
1753 dockerConfigJSON, err = hujson.Standardize(dockerConfigJSON)
1754 if err != nil {
1755 return nil, fmt.Errorf("humanize json for docker config: %w", err)
1756 }
1757
1758 if err = logDockerAuthConfigs(logf, dockerConfigJSON); err != nil {
1759 return nil, fmt.Errorf("log docker auth configs: %w", err)
1760 }
1761
1762 // We're going to set the DOCKER_CONFIG environment variable to a

Callers 2

runFunction · 0.85
RunCacheProbeFunction · 0.85

Calls 8

fileExistsFunction · 0.85
readFileFunction · 0.85
logDockerAuthConfigsFunction · 0.85
setAndRestoreEnvFunction · 0.85
writeFileFunction · 0.85
onceErrFuncFunction · 0.85
JoinMethod · 0.80
MkdirAllMethod · 0.65

Tested by

no test coverage detected