Merge pull request #45628 from thaJeztah/context_simplify

builder/remotecontext: remove mimeTypes struct, use consts
This commit is contained in:
Sebastiaan van Stijn 2023-05-30 13:40:42 +02:00 committed by GitHub
commit 2cd23ffeec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 16 deletions

View file

@ -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:

View file

@ -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
}

View file

@ -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))
}

View file

@ -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
}

View file

@ -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)))