[GH-14493] Accept any GOOS and GOARCH combination in manifest (#14682)

Automatic Merge
Этот коммит содержится в:
Muhammad Kaisar Arkhan
2021-01-13 15:15:17 +01:00
коммит произвёл GitHub
родитель 346c8b01b2
Коммит 29b846be28
2 изменённых файлов: 230 добавлений и 63 удалений

Просмотреть файл

@@ -196,9 +196,21 @@ type Manifest struct {
} }
type ManifestServer struct { type ManifestServer struct {
// Executables are the paths to your executable binaries, specifying multiple entry points // AllExecutables are the paths to your executable binaries, specifying multiple entry
// for different platforms when bundled together in a single plugin. // points for different platforms when bundled together in a single plugin.
Executables *ManifestExecutables `json:"executables,omitempty" yaml:"executables,omitempty"` AllExecutables map[string]string `json:"executables,omitempty" yaml:"executables,omitempty"`
// Executables is a legacy field populated with a subset of supported platform executables.
// When unmarshalling, Executables is authoritative for the platform executable paths it
// contains, overriding any values in AllExecutables. When marshalling, AllExecutables
// is authoritative.
//
// Code duplication is avoided when (un)marshalling by leveraging type aliases in the
// various (Un)Marshal(JSON|YAML) methods, since aliases don't inherit the aliased type's
// methods.
//
// In v6.0, we should remove this field and rename AllExecutables back to Executables.
Executables *ManifestExecutables `json:"-" yaml:"-"`
// Executable is the path to your executable binary. This should be relative to the root // Executable is the path to your executable binary. This should be relative to the root
// of your bundle and the location of the manifest file. // of your bundle and the location of the manifest file.
@@ -210,6 +222,79 @@ type ManifestServer struct {
Executable string `json:"executable" yaml:"executable"` Executable string `json:"executable" yaml:"executable"`
} }
func (ms *ManifestServer) MarshalJSON() ([]byte, error) {
type auxManifestServer ManifestServer
// Populate AllExecutables from Executables, if it exists.
if ms.Executables != nil {
if ms.AllExecutables == nil {
ms.AllExecutables = make(map[string]string)
}
ms.AllExecutables["linux-amd64"] = ms.Executables.LinuxAmd64
ms.AllExecutables["darwin-amd64"] = ms.Executables.DarwinAmd64
ms.AllExecutables["windows-amd64"] = ms.Executables.WindowsAmd64
}
return json.Marshal((*auxManifestServer)(ms))
}
func (ms *ManifestServer) UnmarshalJSON(data []byte) error {
type auxManifestServer ManifestServer
aux := (*auxManifestServer)(ms)
if err := json.Unmarshal(data, aux); err != nil {
return err
}
if len(aux.AllExecutables) > 0 {
ms.Executables = &ManifestExecutables{
LinuxAmd64: aux.AllExecutables["linux-amd64"],
DarwinAmd64: aux.AllExecutables["darwin-amd64"],
WindowsAmd64: aux.AllExecutables["windows-amd64"],
}
}
return nil
}
func (ms *ManifestServer) MarshalYAML() ([]byte, error) {
type auxManifestServer ManifestServer
// Populate AllExecutables from Executables, if it exists.
if ms.Executables != nil {
if ms.AllExecutables == nil {
ms.AllExecutables = make(map[string]string)
}
ms.AllExecutables["linux-amd64"] = ms.Executables.LinuxAmd64
ms.AllExecutables["darwin-amd64"] = ms.Executables.DarwinAmd64
ms.AllExecutables["windows-amd64"] = ms.Executables.WindowsAmd64
}
return yaml.Marshal((*auxManifestServer)(ms))
}
func (ms *ManifestServer) UnmarshalYAML(unmarshal func(interface{}) error) error {
type auxManifestServer ManifestServer
aux := (*auxManifestServer)(ms)
if err := unmarshal(&aux); err != nil {
return err
}
if len(aux.AllExecutables) > 0 {
ms.Executables = &ManifestExecutables{
LinuxAmd64: aux.AllExecutables["linux-amd64"],
DarwinAmd64: aux.AllExecutables["darwin-amd64"],
WindowsAmd64: aux.AllExecutables["windows-amd64"],
}
}
return nil
}
// ManifestExecutables is a legacy structure capturing a subet of the known platform executables.
type ManifestExecutables struct { type ManifestExecutables struct {
// LinuxAmd64 is the path to your executable binary for the corresponding platform // LinuxAmd64 is the path to your executable binary for the corresponding platform
LinuxAmd64 string `json:"linux-amd64,omitempty" yaml:"linux-amd64,omitempty"` LinuxAmd64 string `json:"linux-amd64,omitempty" yaml:"linux-amd64,omitempty"`
@@ -287,14 +372,9 @@ func (m *Manifest) GetExecutableForRuntime(goOs, goArch string) string {
} }
var executable string var executable string
if server.Executables != nil { if len(server.AllExecutables) > 0 {
if goOs == "linux" && goArch == "amd64" { osArch := fmt.Sprintf("%s-%s", goOs, goArch)
executable = server.Executables.LinuxAmd64 executable = server.AllExecutables[osArch]
} else if goOs == "darwin" && goArch == "amd64" {
executable = server.Executables.DarwinAmd64
} else if goOs == "windows" && goArch == "amd64" {
executable = server.Executables.WindowsAmd64
}
} }
if executable == "" { if executable == "" {

Просмотреть файл

@@ -285,6 +285,12 @@ func TestManifestUnmarshal(t *testing.T) {
DarwinAmd64: "theexecutable-darwin-amd64", DarwinAmd64: "theexecutable-darwin-amd64",
WindowsAmd64: "theexecutable-windows-amd64", WindowsAmd64: "theexecutable-windows-amd64",
}, },
AllExecutables: map[string]string{
"linux-amd64": "theexecutable-linux-amd64",
"darwin-amd64": "theexecutable-darwin-amd64",
"windows-amd64": "theexecutable-windows-amd64",
"linux-arm64": "theexecutable-linux-arm64",
},
}, },
Webapp: &ManifestWebapp{ Webapp: &ManifestWebapp{
BundlePath: "thebundlepath", BundlePath: "thebundlepath",
@@ -312,8 +318,9 @@ func TestManifestUnmarshal(t *testing.T) {
}, },
} }
var yamlResult Manifest t.Run("yaml", func(t *testing.T) {
require.NoError(t, yaml.Unmarshal([]byte(` var yamlResult Manifest
require.NoError(t, yaml.Unmarshal([]byte(`
id: theid id: theid
homepage_url: https://example.com homepage_url: https://example.com
support_url: https://example.com/support support_url: https://example.com/support
@@ -325,6 +332,7 @@ server:
linux-amd64: theexecutable-linux-amd64 linux-amd64: theexecutable-linux-amd64
darwin-amd64: theexecutable-darwin-amd64 darwin-amd64: theexecutable-darwin-amd64
windows-amd64: theexecutable-windows-amd64 windows-amd64: theexecutable-windows-amd64
linux-arm64: theexecutable-linux-arm64
webapp: webapp:
bundle_path: thebundlepath bundle_path: thebundlepath
settings_schema: settings_schema:
@@ -342,10 +350,12 @@ settings_schema:
value: thevalue value: thevalue
default: thedefault default: thedefault
`), &yamlResult)) `), &yamlResult))
assert.Equal(t, expected, yamlResult) assert.Equal(t, expected, yamlResult)
})
var jsonResult Manifest t.Run("json", func(t *testing.T) {
require.NoError(t, json.Unmarshal([]byte(`{ var jsonResult Manifest
require.NoError(t, json.Unmarshal([]byte(`{
"id": "theid", "id": "theid",
"homepage_url": "https://example.com", "homepage_url": "https://example.com",
"support_url": "https://example.com/support", "support_url": "https://example.com/support",
@@ -356,7 +366,8 @@ settings_schema:
"executables": { "executables": {
"linux-amd64": "theexecutable-linux-amd64", "linux-amd64": "theexecutable-linux-amd64",
"darwin-amd64": "theexecutable-darwin-amd64", "darwin-amd64": "theexecutable-darwin-amd64",
"windows-amd64": "theexecutable-windows-amd64" "windows-amd64": "theexecutable-windows-amd64",
"linux-arm64": "theexecutable-linux-arm64"
} }
}, },
"webapp": { "webapp": {
@@ -384,7 +395,8 @@ settings_schema:
] ]
} }
}`), &jsonResult)) }`), &jsonResult))
assert.Equal(t, expected, jsonResult) assert.Equal(t, expected, jsonResult)
})
} }
func TestFindManifest_FileErrors(t *testing.T) { func TestFindManifest_FileErrors(t *testing.T) {
@@ -434,6 +446,17 @@ func TestManifestJson(t *testing.T) {
Id: "theid", Id: "theid",
Server: &ManifestServer{ Server: &ManifestServer{
Executable: "theexecutable", Executable: "theexecutable",
Executables: &ManifestExecutables{
LinuxAmd64: "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable",
},
AllExecutables: map[string]string{
"linux-amd64": "linux-amd64/path/to/executable",
"darwin-amd64": "darwin-amd64/path/to/executable",
"windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
},
}, },
Webapp: &ManifestWebapp{ Webapp: &ManifestWebapp{
BundlePath: "thebundlepath", BundlePath: "thebundlepath",
@@ -597,22 +620,7 @@ func TestManifestGetExecutableForRuntime(t *testing.T) {
"path/to/executable", "path/to/executable",
}, },
{ {
"multiple executables, no match", "multiple legacy executables ignored",
&Manifest{
Server: &ManifestServer{
Executables: &ManifestExecutables{
LinuxAmd64: "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable",
},
},
},
"other",
"amd64",
"",
},
{
"multiple executables, linux-amd64 match",
&Manifest{ &Manifest{
Server: &ManifestServer{ Server: &ManifestServer{
Executables: &ManifestExecutables{ Executables: &ManifestExecutables{
@@ -624,16 +632,49 @@ func TestManifestGetExecutableForRuntime(t *testing.T) {
}, },
"linux", "linux",
"amd64", "amd64",
"",
},
{
"multiple executables, no match",
&Manifest{
Server: &ManifestServer{
AllExecutables: map[string]string{
"linux-amd64": "linux-amd64/path/to/executable",
"darwin-amd64": "darwin-amd64/path/to/executable",
"windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
},
},
},
"other",
"amd64",
"",
},
{
"multiple executables, linux-amd64 match",
&Manifest{
Server: &ManifestServer{
AllExecutables: map[string]string{
"linux-amd64": "linux-amd64/path/to/executable",
"darwin-amd64": "darwin-amd64/path/to/executable",
"windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
},
},
},
"linux",
"amd64",
"linux-amd64/path/to/executable", "linux-amd64/path/to/executable",
}, },
{ {
"multiple executables, linux-amd64 match, single executable ignored", "multiple executables, linux-amd64 match, single executable ignored",
&Manifest{ &Manifest{
Server: &ManifestServer{ Server: &ManifestServer{
Executables: &ManifestExecutables{ AllExecutables: map[string]string{
LinuxAmd64: "linux-amd64/path/to/executable", "linux-amd64": "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable", "darwin-amd64": "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable", "windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
}, },
Executable: "path/to/executable", Executable: "path/to/executable",
}, },
@@ -646,10 +687,11 @@ func TestManifestGetExecutableForRuntime(t *testing.T) {
"multiple executables, darwin-amd64 match", "multiple executables, darwin-amd64 match",
&Manifest{ &Manifest{
Server: &ManifestServer{ Server: &ManifestServer{
Executables: &ManifestExecutables{ AllExecutables: map[string]string{
LinuxAmd64: "linux-amd64/path/to/executable", "linux-amd64": "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable", "darwin-amd64": "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable", "windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
}, },
}, },
}, },
@@ -661,10 +703,11 @@ func TestManifestGetExecutableForRuntime(t *testing.T) {
"multiple executables, windows-amd64 match", "multiple executables, windows-amd64 match",
&Manifest{ &Manifest{
Server: &ManifestServer{ Server: &ManifestServer{
Executables: &ManifestExecutables{ AllExecutables: map[string]string{
LinuxAmd64: "linux-amd64/path/to/executable", "linux-amd64": "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable", "darwin-amd64": "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable", "windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
}, },
}, },
}, },
@@ -676,10 +719,11 @@ func TestManifestGetExecutableForRuntime(t *testing.T) {
"multiple executables, no match, single executable fallback", "multiple executables, no match, single executable fallback",
&Manifest{ &Manifest{
Server: &ManifestServer{ Server: &ManifestServer{
Executables: &ManifestExecutables{ AllExecutables: map[string]string{
LinuxAmd64: "linux-amd64/path/to/executable", "linux-amd64": "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable", "darwin-amd64": "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable", "windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
}, },
Executable: "path/to/executable", Executable: "path/to/executable",
}, },
@@ -692,17 +736,18 @@ func TestManifestGetExecutableForRuntime(t *testing.T) {
"deprecated backend field, ignored since server present", "deprecated backend field, ignored since server present",
&Manifest{ &Manifest{
Server: &ManifestServer{ Server: &ManifestServer{
Executables: &ManifestExecutables{ AllExecutables: map[string]string{
LinuxAmd64: "linux-amd64/path/to/executable", "linux-amd64": "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable", "darwin-amd64": "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable", "windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
}, },
}, },
Backend: &ManifestServer{ Backend: &ManifestServer{
Executables: &ManifestExecutables{ AllExecutables: map[string]string{
LinuxAmd64: "linux-amd64/path/to/executable/backend", "linux-amd64": "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable/backend", "darwin-amd64": "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable/backend", "windows-amd64": "windows-amd64/path/to/executable",
}, },
}, },
}, },
@@ -714,16 +759,58 @@ func TestManifestGetExecutableForRuntime(t *testing.T) {
"deprecated backend field used, since no server present", "deprecated backend field used, since no server present",
&Manifest{ &Manifest{
Backend: &ManifestServer{ Backend: &ManifestServer{
Executables: &ManifestExecutables{ AllExecutables: map[string]string{
LinuxAmd64: "linux-amd64/path/to/executable/backend", "linux-amd64": "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable/backend", "darwin-amd64": "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable/backend", "windows-amd64": "windows-amd64/path/to/executable",
}, },
}, },
}, },
"linux", "linux",
"amd64", "amd64",
"linux-amd64/path/to/executable/backend", "linux-amd64/path/to/executable",
},
{
"all executables, linux-arm64",
&Manifest{
Backend: &ManifestServer{
Executables: &ManifestExecutables{
LinuxAmd64: "linux-amd64/path/to/executable",
DarwinAmd64: "darwin-amd64/path/to/executable",
WindowsAmd64: "windows-amd64/path/to/executable",
},
AllExecutables: map[string]string{
"linux-amd64": "linux-amd64/path/to/executable",
"darwin-amd64": "darwin-amd64/path/to/executable",
"windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
},
},
},
"linux",
"arm64",
"linux-arm64/path/to/executable",
},
{
"all executables, linux-amd64, legacy executables ignored",
&Manifest{
Backend: &ManifestServer{
Executables: &ManifestExecutables{
LinuxAmd64: "linux-amd64/ignored/path/to/executable",
DarwinAmd64: "darwin-amd64/ignored/path/to/executable",
WindowsAmd64: "windows-amd64/ignored/path/to/executable",
},
AllExecutables: map[string]string{
"linux-amd64": "linux-amd64/path/to/executable",
"darwin-amd64": "darwin-amd64/path/to/executable",
"windows-amd64": "windows-amd64/path/to/executable",
"linux-arm64": "linux-arm64/path/to/executable",
},
},
},
"linux",
"amd64",
"linux-amd64/path/to/executable",
}, },
} }