@@ -25,9 +25,14 @@ import (
2525 "github.com/pkg/errors"
2626)
2727
28+ // DefaultDockerfileName is the Default filename with Docker commands, read by docker build
29+ //
30+ // Deprecated: this const is no longer used and will be removed in the next release.
31+ const DefaultDockerfileName string = "Dockerfile"
32+
2833const (
29- // DefaultDockerfileName is the Default filename with Docker commands, read by docker build
30- DefaultDockerfileName string = "Dockerfile"
34+ // defaultDockerfileName is the Default filename with Docker commands, read by docker build
35+ defaultDockerfileName string = "Dockerfile"
3136 // archiveHeaderSize is the number of bytes in an archive header
3237 archiveHeaderSize = 512
3338)
@@ -100,7 +105,18 @@ func filepathMatches(matcher *patternmatcher.PatternMatcher, file string) (bool,
100105// of input. If an archive is detected, ok is set to true, and to false
101106// otherwise, in which case it is safe to assume input represents the contents
102107// of a Dockerfile.
108+ //
109+ // Deprecated: this utility was only used internally, and will be removed in the next release.
103110func DetectArchiveReader (input io.ReadCloser ) (rc io.ReadCloser , ok bool , err error ) {
111+ return detectArchiveReader (input )
112+ }
113+
114+ // detectArchiveReader detects whether the input stream is an archive or a
115+ // Dockerfile and returns a buffered version of input, safe to consume in lieu
116+ // of input. If an archive is detected, ok is set to true, and to false
117+ // otherwise, in which case it is safe to assume input represents the contents
118+ // of a Dockerfile.
119+ func detectArchiveReader (input io.ReadCloser ) (rc io.ReadCloser , ok bool , err error ) {
104120 buf := bufio .NewReader (input )
105121
106122 magic , err := buf .Peek (archiveHeaderSize * 2 )
@@ -112,9 +128,18 @@ func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, ok bool, err er
112128}
113129
114130// WriteTempDockerfile writes a Dockerfile stream to a temporary file with a
115- // name specified by DefaultDockerfileName and returns the path to the
131+ // name specified by defaultDockerfileName and returns the path to the
116132// temporary directory containing the Dockerfile.
133+ //
134+ // Deprecated: this utility was only used internally, and will be removed in the next release.
117135func WriteTempDockerfile (rc io.ReadCloser ) (dockerfileDir string , err error ) {
136+ return writeTempDockerfile (rc )
137+ }
138+
139+ // writeTempDockerfile writes a Dockerfile stream to a temporary file with a
140+ // name specified by defaultDockerfileName and returns the path to the
141+ // temporary directory containing the Dockerfile.
142+ func writeTempDockerfile (rc io.ReadCloser ) (dockerfileDir string , err error ) {
118143 // err is a named return value, due to the defer call below.
119144 dockerfileDir , err = os .MkdirTemp ("" , "docker-build-tempdockerfile-" )
120145 if err != nil {
@@ -126,7 +151,7 @@ func WriteTempDockerfile(rc io.ReadCloser) (dockerfileDir string, err error) {
126151 }
127152 }()
128153
129- f , err := os .Create (filepath .Join (dockerfileDir , DefaultDockerfileName ))
154+ f , err := os .Create (filepath .Join (dockerfileDir , defaultDockerfileName ))
130155 if err != nil {
131156 return "" , err
132157 }
@@ -141,7 +166,7 @@ func WriteTempDockerfile(rc io.ReadCloser) (dockerfileDir string, err error) {
141166// Dockerfile or tar archive. Returns a tar archive used as a context and a
142167// path to the Dockerfile inside the tar.
143168func GetContextFromReader (rc io.ReadCloser , dockerfileName string ) (out io.ReadCloser , relDockerfile string , err error ) {
144- rc , ok , err := DetectArchiveReader (rc )
169+ rc , ok , err := detectArchiveReader (rc )
145170 if err != nil {
146171 return nil , "" , err
147172 }
@@ -159,7 +184,7 @@ func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadC
159184 return nil , "" , errors .New ("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles" )
160185 }
161186
162- dockerfileDir , err := WriteTempDockerfile (rc )
187+ dockerfileDir , err := writeTempDockerfile (rc )
163188 if err != nil {
164189 return nil , "" , err
165190 }
@@ -173,7 +198,7 @@ func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadC
173198 err := tarArchive .Close ()
174199 _ = os .RemoveAll (dockerfileDir )
175200 return err
176- }), DefaultDockerfileName , nil
201+ }), defaultDockerfileName , nil
177202}
178203
179204// IsArchive checks for the magic bytes of a tar or any supported compression
@@ -209,7 +234,7 @@ func GetContextFromGitURL(gitURL, dockerfileName string) (string, string, error)
209234 return "" , "" , errors .Wrapf (err , "unable to 'git clone' to temporary context directory" )
210235 }
211236
212- absContextDir , err = ResolveAndValidateContextPath (absContextDir )
237+ absContextDir , err = resolveAndValidateContextPath (absContextDir )
213238 if err != nil {
214239 return "" , "" , err
215240 }
@@ -262,7 +287,7 @@ func getWithStatusError(url string) (resp *http.Response, err error) {
262287// the relative path of the dockerfile in that context directory, and a non-nil
263288// error on success.
264289func GetContextFromLocalDir (localDir , dockerfileName string ) (string , string , error ) {
265- localDir , err := ResolveAndValidateContextPath (localDir )
290+ localDir , err := resolveAndValidateContextPath (localDir )
266291 if err != nil {
267292 return "" , "" , err
268293 }
@@ -282,7 +307,18 @@ func GetContextFromLocalDir(localDir, dockerfileName string) (string, string, er
282307
283308// ResolveAndValidateContextPath uses the given context directory for a `docker build`
284309// and returns the absolute path to the context directory.
310+ //
311+ // Deprecated: this utility was used internally and will be removed in the next
312+ // release. Use [DetectContextType] to detect the context-type, and use
313+ // [GetContextFromLocalDir], [GetContextFromLocalDir], [GetContextFromGitURL],
314+ // or [GetContextFromURL] instead.
285315func ResolveAndValidateContextPath (givenContextDir string ) (string , error ) {
316+ return resolveAndValidateContextPath (givenContextDir )
317+ }
318+
319+ // resolveAndValidateContextPath uses the given context directory for a `docker build`
320+ // and returns the absolute path to the context directory.
321+ func resolveAndValidateContextPath (givenContextDir string ) (string , error ) {
286322 absContextDir , err := filepath .Abs (givenContextDir )
287323 if err != nil {
288324 return "" , errors .Errorf ("unable to get absolute context directory of given context directory %q: %v" , givenContextDir , err )
@@ -326,12 +362,12 @@ func getDockerfileRelPath(absContextDir, givenDockerfile string) (string, error)
326362 if absDockerfile == "" {
327363 // No -f/--file was specified so use the default relative to the
328364 // context directory.
329- absDockerfile = filepath .Join (absContextDir , DefaultDockerfileName )
365+ absDockerfile = filepath .Join (absContextDir , defaultDockerfileName )
330366
331367 // Just to be nice ;-) look for 'dockerfile' too but only
332368 // use it if we found it, otherwise ignore this check
333369 if _ , err = os .Lstat (absDockerfile ); os .IsNotExist (err ) {
334- altPath := filepath .Join (absContextDir , strings .ToLower (DefaultDockerfileName ))
370+ altPath := filepath .Join (absContextDir , strings .ToLower (defaultDockerfileName ))
335371 if _ , err = os .Lstat (altPath ); err == nil {
336372 absDockerfile = altPath
337373 }
0 commit comments