client: add utilities to encode platforms

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-10-31 13:46:57 +01:00
parent 8c3945c761
commit 96039276b6
5 changed files with 107 additions and 10 deletions

View File

@@ -17,11 +17,11 @@ func (cli *Client) ImageHistory(ctx context.Context, imageID string, opts image.
return nil, err
}
p, err := json.Marshal(*opts.Platform)
p, err := encodePlatform(opts.Platform)
if err != nil {
return nil, fmt.Errorf("invalid platform: %v", err)
}
query.Set("platform", string(p))
query.Set("platform", p)
}
var history []image.HistoryResponseItem

View File

@@ -2,7 +2,6 @@ package client // import "github.com/docker/docker/client"
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
@@ -28,11 +27,11 @@ func (cli *Client) ImageLoad(ctx context.Context, input io.Reader, opts image.Lo
return image.LoadResponse{}, err
}
p, err := json.Marshal(*opts.Platform)
p, err := encodePlatform(opts.Platform)
if err != nil {
return image.LoadResponse{}, err
}
query.Set("platform", string(p))
query.Set("platform", p)
}
resp, err := cli.postRaw(ctx, "/images/load", query, input, http.Header{

View File

@@ -2,8 +2,6 @@ package client // import "github.com/docker/docker/client"
import (
"context"
"encoding/json"
"fmt"
"io"
"net/url"
@@ -22,11 +20,11 @@ func (cli *Client) ImageSave(ctx context.Context, imageIDs []string, opts image.
return nil, err
}
p, err := json.Marshal(*opts.Platform)
p, err := encodePlatform(opts.Platform)
if err != nil {
return nil, fmt.Errorf("invalid platform: %v", err)
return nil, err
}
query.Set("platform", string(p))
query.Set("platform", p)
}
resp, err := cli.get(ctx, "/images/get", query, nil)

View File

@@ -1,10 +1,14 @@
package client // import "github.com/docker/docker/client"
import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/errdefs"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
var headerRegexp = regexp.MustCompile(`\ADocker/.+\s\((.+)\)\z`)
@@ -32,3 +36,43 @@ func getFiltersQuery(f filters.Args) (url.Values, error) {
}
return query, nil
}
// encodePlatforms marshals the given platform(s) to JSON format, to
// be used for query-parameters for filtering / selecting platforms.
func encodePlatforms(platform ...ocispec.Platform) ([]string, error) {
if len(platform) == 0 {
return []string{}, nil
}
if len(platform) == 1 {
p, err := encodePlatform(&platform[0])
if err != nil {
return nil, err
}
return []string{p}, nil
}
seen := make(map[string]struct{}, len(platform))
out := make([]string, 0, len(platform))
for i := range platform {
p, err := encodePlatform(&platform[i])
if err != nil {
return nil, err
}
if _, ok := seen[p]; !ok {
out = append(out, p)
seen[p] = struct{}{}
}
}
return out, nil
}
// encodePlatforms marshals the given platform to JSON format, to
// be used for query-parameters for filtering / selecting platforms. It
// is used as a helper for encodePlatforms,
func encodePlatform(platform *ocispec.Platform) (string, error) {
p, err := json.Marshal(platform)
if err != nil {
return "", errdefs.InvalidParameter(fmt.Errorf("invalid platform: %v", err))
}
return string(p), nil
}

56
client/utils_test.go Normal file
View File

@@ -0,0 +1,56 @@
package client
import (
"testing"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"gotest.tools/v3/assert"
)
func TestEncodePlatforms(t *testing.T) {
tests := []struct {
doc string
platforms []ocispec.Platform
expected []string
}{
{
doc: "single platform",
platforms: []ocispec.Platform{
{Architecture: "arm64", OS: "windows", Variant: "v8", OSVersion: "99.99.99"},
},
expected: []string{
`{"architecture":"arm64","os":"windows","os.version":"99.99.99","variant":"v8"}`,
},
},
{
doc: "multiple platforms",
platforms: []ocispec.Platform{
{Architecture: "arm64", OS: "linux", Variant: "v8"},
{Architecture: "arm64", OS: "windows", Variant: "v8", OSVersion: "99.99.99"},
},
expected: []string{
`{"architecture":"arm64","os":"linux","variant":"v8"}`,
`{"architecture":"arm64","os":"windows","os.version":"99.99.99","variant":"v8"}`,
},
},
{
doc: "multiple platforms with duplicates",
platforms: []ocispec.Platform{
{Architecture: "arm64", OS: "linux", Variant: "v8"},
{Architecture: "arm64", OS: "windows", Variant: "v8", OSVersion: "99.99.99"},
{Architecture: "arm64", OS: "linux", Variant: "v8"},
},
expected: []string{
`{"architecture":"arm64","os":"linux","variant":"v8"}`,
`{"architecture":"arm64","os":"windows","os.version":"99.99.99","variant":"v8"}`,
},
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
out, err := encodePlatforms(tc.platforms...)
assert.NilError(t, err)
assert.DeepEqual(t, out, tc.expected)
})
}
}