Skip to content

Commit 59d228c

Browse files
authored
Merge pull request #6550 from thaJeztah/deprecate_builder_utils
cli/command/image/build: deprecate IsArchive utility
2 parents a3e9545 + 9e646f6 commit 59d228c

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed

cli/command/image/build.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ func (out *lastProgressOutput) WriteProgress(prog progress.Progress) error {
181181
//nolint:gocyclo
182182
func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions) error {
183183
var (
184-
err error
185184
buildCtx io.ReadCloser
186185
dockerfileCtx io.ReadCloser
187186
contextDir string
@@ -263,7 +262,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
263262
}
264263

265264
if err := build.ValidateContextDirectory(contextDir, excludes); err != nil {
266-
return fmt.Errorf("error checking context: %w", err)
265+
return fmt.Errorf("checking context: %w", err)
267266
}
268267

269268
// And canonicalize dockerfile name to a platform-independent one
@@ -287,9 +286,6 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
287286
}
288287
}
289288

290-
ctx, cancel := context.WithCancel(ctx)
291-
defer cancel()
292-
293289
if options.compress {
294290
buildCtx, err = build.Compress(buildCtx)
295291
if err != nil {
@@ -330,17 +326,18 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
330326
}
331327
}
332328
buildOpts := imageBuildOptions(dockerCli, options)
333-
buildOpts.Version = buildtypes.BuilderV1
334329
buildOpts.Dockerfile = relDockerfile
335330
buildOpts.AuthConfigs = authConfigs
336331
buildOpts.RemoteContext = remote
337332

333+
ctx, cancel := context.WithCancel(ctx)
334+
defer cancel()
335+
338336
response, err := dockerCli.Client().ImageBuild(ctx, body, buildOpts)
339337
if err != nil {
340338
if options.quiet {
341339
_, _ = fmt.Fprintf(dockerCli.Err(), "%s", progBuff)
342340
}
343-
cancel()
344341
return err
345342
}
346343
defer response.Body.Close()
@@ -357,7 +354,8 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
357354

358355
err = jsonstream.Display(ctx, response.Body, streams.NewOut(buildBuff), jsonstream.WithAuxCallback(aux))
359356
if err != nil {
360-
if jerr, ok := err.(*jsonstream.JSONError); ok {
357+
var jerr *jsonstream.JSONError
358+
if errors.As(err, &jerr) {
361359
// If no error code is set, default to 1
362360
if jerr.Code == 0 {
363361
jerr.Code = 1
@@ -402,6 +400,7 @@ func validateTag(rawRepo string) (string, error) {
402400
func imageBuildOptions(dockerCli command.Cli, options buildOptions) client.ImageBuildOptions {
403401
configFile := dockerCli.ConfigFile()
404402
return client.ImageBuildOptions{
403+
Version: buildtypes.BuilderV1,
405404
Memory: options.memory.Value(),
406405
MemorySwap: options.memorySwap.Value(),
407406
Tags: options.tags.GetSlice(),

cli/command/image/build/context.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func ValidateContextDirectory(srcPath string, excludes []string) error {
8080
if err != nil && os.IsPermission(err) {
8181
return fmt.Errorf("no permission to read from '%s'", filePath)
8282
}
83-
currentFile.Close()
83+
_ = currentFile.Close()
8484
}
8585
return nil
8686
})
@@ -97,18 +97,18 @@ func filepathMatches(matcher *patternmatcher.PatternMatcher, file string) (bool,
9797

9898
// DetectArchiveReader detects whether the input stream is an archive or a
9999
// Dockerfile and returns a buffered version of input, safe to consume in lieu
100-
// of input. If an archive is detected, isArchive is set to true, and to false
100+
// of input. If an archive is detected, ok is set to true, and to false
101101
// otherwise, in which case it is safe to assume input represents the contents
102102
// of a Dockerfile.
103-
func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, isArchive bool, err error) {
103+
func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, ok bool, err error) {
104104
buf := bufio.NewReader(input)
105105

106106
magic, err := buf.Peek(archiveHeaderSize * 2)
107107
if err != nil && err != io.EOF {
108108
return nil, false, fmt.Errorf("failed to peek context header from STDIN: %w", err)
109109
}
110110

111-
return newReadCloserWrapper(buf, func() error { return input.Close() }), IsArchive(magic), nil
111+
return newReadCloserWrapper(buf, func() error { return input.Close() }), isArchive(magic), nil
112112
}
113113

114114
// WriteTempDockerfile writes a Dockerfile stream to a temporary file with a
@@ -141,12 +141,12 @@ func WriteTempDockerfile(rc io.ReadCloser) (dockerfileDir string, err error) {
141141
// Dockerfile or tar archive. Returns a tar archive used as a context and a
142142
// path to the Dockerfile inside the tar.
143143
func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadCloser, relDockerfile string, err error) {
144-
rc, isArchive, err := DetectArchiveReader(rc)
144+
rc, ok, err := DetectArchiveReader(rc)
145145
if err != nil {
146146
return nil, "", err
147147
}
148148

149-
if isArchive {
149+
if ok {
150150
return rc, dockerfileName, nil
151151
}
152152

@@ -171,14 +171,22 @@ func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadC
171171

172172
return newReadCloserWrapper(tarArchive, func() error {
173173
err := tarArchive.Close()
174-
os.RemoveAll(dockerfileDir)
174+
_ = os.RemoveAll(dockerfileDir)
175175
return err
176176
}), DefaultDockerfileName, nil
177177
}
178178

179179
// IsArchive checks for the magic bytes of a tar or any supported compression
180180
// algorithm.
181+
//
182+
// Deprecated: this utility was used internally and will be removed in the next release.
181183
func IsArchive(header []byte) bool {
184+
return isArchive(header)
185+
}
186+
187+
// isArchive checks for the magic bytes of a tar or any supported compression
188+
// algorithm.
189+
func isArchive(header []byte) bool {
182190
if compression.Detect(header) != compression.None {
183191
return true
184192
}
@@ -242,7 +250,7 @@ func getWithStatusError(url string) (resp *http.Response, err error) {
242250
}
243251
msg := fmt.Sprintf("failed to GET %s with status %s", url, resp.Status)
244252
body, err := io.ReadAll(resp.Body)
245-
resp.Body.Close()
253+
_ = resp.Body.Close()
246254
if err != nil {
247255
return nil, fmt.Errorf("%s: error reading body: %w", msg, err)
248256
}
@@ -374,7 +382,7 @@ func isUNC(path string) bool {
374382
// the relative path to the dockerfile in the context.
375383
func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCloser) (io.ReadCloser, string, error) {
376384
file, err := io.ReadAll(dockerfileCtx)
377-
dockerfileCtx.Close()
385+
_ = dockerfileCtx.Close()
378386
if err != nil {
379387
return nil, "", err
380388
}
@@ -438,17 +446,19 @@ func Compress(buildCtx io.ReadCloser) (io.ReadCloser, error) {
438446
go func() {
439447
compressWriter, err := compression.CompressStream(pipeWriter, archive.Gzip)
440448
if err != nil {
441-
pipeWriter.CloseWithError(err)
449+
_ = pipeWriter.CloseWithError(err)
442450
}
443-
defer buildCtx.Close()
451+
defer func() {
452+
_ = buildCtx.Close()
453+
}()
444454

445455
if _, err := io.Copy(compressWriter, buildCtx); err != nil {
446-
pipeWriter.CloseWithError(fmt.Errorf("failed to compress context: %w", err))
447-
compressWriter.Close()
456+
_ = pipeWriter.CloseWithError(fmt.Errorf("failed to compress context: %w", err))
457+
_ = compressWriter.Close()
448458
return
449459
}
450-
compressWriter.Close()
451-
pipeWriter.Close()
460+
_ = compressWriter.Close()
461+
_ = pipeWriter.Close()
452462
}()
453463

454464
return pipeReader, nil

cli/command/image/build/context_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestGetContextFromReaderString(t *testing.T) {
135135
}
136136

137137
buff := new(bytes.Buffer)
138-
buff.ReadFrom(tarReader)
138+
_, _ = buff.ReadFrom(tarReader)
139139
contents := buff.String()
140140

141141
_, err = tarReader.Next()
@@ -182,7 +182,7 @@ func TestGetContextFromReaderTar(t *testing.T) {
182182
}
183183

184184
buff := new(bytes.Buffer)
185-
buff.ReadFrom(tarReader)
185+
_, _ = buff.ReadFrom(tarReader)
186186
contents := buff.String()
187187

188188
_, err = tarReader.Next()
@@ -263,7 +263,7 @@ func chdir(t *testing.T, dir string) {
263263
}
264264

265265
func TestIsArchive(t *testing.T) {
266-
testcases := []struct {
266+
tests := []struct {
267267
doc string
268268
header []byte
269269
expected bool
@@ -289,13 +289,15 @@ func TestIsArchive(t *testing.T) {
289289
expected: false,
290290
},
291291
}
292-
for _, testcase := range testcases {
293-
assert.Check(t, is.Equal(testcase.expected, IsArchive(testcase.header)), testcase.doc)
292+
for _, tc := range tests {
293+
t.Run(tc.doc, func(t *testing.T) {
294+
assert.Check(t, is.Equal(tc.expected, isArchive(tc.header)), tc.doc)
295+
})
294296
}
295297
}
296298

297299
func TestDetectArchiveReader(t *testing.T) {
298-
testcases := []struct {
300+
tests := []struct {
299301
file string
300302
desc string
301303
expected bool
@@ -316,14 +318,18 @@ func TestDetectArchiveReader(t *testing.T) {
316318
expected: false,
317319
},
318320
}
319-
for _, testcase := range testcases {
320-
content, err := os.Open(testcase.file)
321-
assert.NilError(t, err)
322-
defer content.Close()
323-
324-
_, isArchive, err := DetectArchiveReader(content)
325-
assert.NilError(t, err)
326-
assert.Check(t, is.Equal(testcase.expected, isArchive), testcase.file)
321+
for _, tc := range tests {
322+
t.Run(tc.desc, func(t *testing.T) {
323+
content, err := os.Open(tc.file)
324+
assert.NilError(t, err)
325+
defer func() {
326+
_ = content.Close()
327+
}()
328+
329+
_, isArchive, err := DetectArchiveReader(content)
330+
assert.NilError(t, err)
331+
assert.Check(t, is.Equal(tc.expected, isArchive), tc.file)
332+
})
327333
}
328334
}
329335

cli/command/image/build/dockerignore.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ func ReadDockerignore(contextDir string) ([]string, error) {
2121
case err != nil:
2222
return nil, err
2323
}
24-
defer f.Close()
24+
defer func() {
25+
_ = f.Close()
26+
}()
2527

2628
patterns, err := ignorefile.ReadAll(f)
2729
if err != nil {

0 commit comments

Comments
 (0)