From e410e27547aebc5fdf0b7d5d2afe0bb9305d2d1f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 26 May 2023 02:28:35 +0200 Subject: [PATCH] builder/remotecontext: remove mimeTypes struct, use consts This struct was never modified; let's just use consts for these. Also remove the args return from detectContentType(), as it was not used anywhere. Signed-off-by: Sebastiaan van Stijn --- builder/remotecontext/detect.go | 2 +- builder/remotecontext/mimetype.go | 19 +++++++++---------- builder/remotecontext/mimetype_test.go | 4 ++-- builder/remotecontext/remote.go | 4 ++-- builder/remotecontext/remote_test.go | 2 +- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/builder/remotecontext/detect.go b/builder/remotecontext/detect.go index 93cceef6b3..38d4fed321 100644 --- a/builder/remotecontext/detect.go +++ b/builder/remotecontext/detect.go @@ -102,7 +102,7 @@ func newURLRemote(url string, dockerfilePath string, progressReader func(in io.R defer content.Close() switch contentType { - case mimeTypes.TextPlain: + case mimeTypeTextPlain: res, err := parser.Parse(progressReader(content)) return nil, res, errdefs.InvalidParameter(err) default: diff --git a/builder/remotecontext/mimetype.go b/builder/remotecontext/mimetype.go index e8a6210e9c..3d29b0d476 100644 --- a/builder/remotecontext/mimetype.go +++ b/builder/remotecontext/mimetype.go @@ -5,11 +5,11 @@ import ( "net/http" ) -// mimeTypes stores the MIME content type. -var mimeTypes = struct { - TextPlain string - OctetStream string -}{"text/plain", "application/octet-stream"} +// MIME content types. +const ( + mimeTypeTextPlain = "text/plain" + mimeTypeOctetStream = "application/octet-stream" +) // detectContentType returns a best guess representation of the MIME // content type for the bytes at c. The value detected by @@ -17,11 +17,10 @@ var mimeTypes = struct { // application/octet-stream when a better guess cannot be made. The // result of this detection is then run through mime.ParseMediaType() // which separates the actual MIME string from any parameters. -func detectContentType(c []byte) (string, map[string]string, error) { - ct := http.DetectContentType(c) - contentType, args, err := mime.ParseMediaType(ct) +func detectContentType(c []byte) (string, error) { + contentType, _, err := mime.ParseMediaType(http.DetectContentType(c)) if err != nil { - return "", nil, err + return "", err } - return contentType, args, nil + return contentType, nil } diff --git a/builder/remotecontext/mimetype_test.go b/builder/remotecontext/mimetype_test.go index cbcf31807a..f7c62f4948 100644 --- a/builder/remotecontext/mimetype_test.go +++ b/builder/remotecontext/mimetype_test.go @@ -10,7 +10,7 @@ import ( func TestDetectContentType(t *testing.T) { input := []byte("That is just a plain text") - contentType, _, err := detectContentType(input) + contentType, err := detectContentType(input) assert.NilError(t, err) - assert.Check(t, is.Equal("text/plain", contentType)) + assert.Check(t, is.Equal(mimeTypeTextPlain, contentType)) } diff --git a/builder/remotecontext/remote.go b/builder/remotecontext/remote.go index 8f09ed0997..6bac5d1d62 100644 --- a/builder/remotecontext/remote.go +++ b/builder/remotecontext/remote.go @@ -105,8 +105,8 @@ func inspectResponse(ct string, r io.Reader, clen int64) (string, io.Reader, err // content type for files without an extension (e.g. 'Dockerfile') // so if we receive this value we better check for text content contentType := ct - if len(ct) == 0 || ct == mimeTypes.OctetStream { - contentType, _, err = detectContentType(preamble) + if len(ct) == 0 || ct == mimeTypeOctetStream { + contentType, err = detectContentType(preamble) if err != nil { return contentType, bodyReader, err } diff --git a/builder/remotecontext/remote_test.go b/builder/remotecontext/remote_test.go index a945181183..0f86168b9e 100644 --- a/builder/remotecontext/remote_test.go +++ b/builder/remotecontext/remote_test.go @@ -189,7 +189,7 @@ func TestDownloadRemote(t *testing.T) { contentType, content, err := downloadRemote(remoteURL) assert.NilError(t, err) - assert.Check(t, is.Equal(mimeTypes.TextPlain, contentType)) + assert.Check(t, is.Equal(mimeTypeTextPlain, contentType)) raw, err := io.ReadAll(content) assert.NilError(t, err) assert.Check(t, is.Equal(dockerfileContents, string(raw)))