Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support using go_install or go_build if the asset for the platform isn't released in GitHub Releases #2317

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions json-schema/registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@
"name"
]
},
"Build": {
"properties": {
"type": {
"type": "string",
"enum": [
"enum=go_install",
"go_build"
]
},
"path": {
"type": "string"
},
"files": {
"items": {
"$ref": "#/$defs/File"
},
"type": "array"
},
"excluded_envs": {
"$ref": "#/$defs/SupportedEnvs"
},
"enabled": {
"type": "boolean"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"type"
]
},
"Cargo": {
"properties": {
"features": {
Expand Down Expand Up @@ -447,6 +478,12 @@
"private": {
"type": "boolean"
},
"build": {
"$ref": "#/$defs/Build"
},
"append_ext": {
"type": "boolean"
},
"version_constraint": {
"type": "string"
},
Expand All @@ -455,9 +492,6 @@
"$ref": "#/$defs/VersionOverride"
},
"type": "array"
},
"append_ext": {
"type": "boolean"
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -642,6 +676,9 @@
},
"append_ext": {
"type": "boolean"
},
"build": {
"$ref": "#/$defs/Build"
}
},
"additionalProperties": false,
Expand Down
99 changes: 67 additions & 32 deletions pkg/config/registry/package_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,60 @@ const (
)

type PackageInfo struct {
Name string `json:"name,omitempty" yaml:",omitempty"`
Aliases []*Alias `yaml:",omitempty" json:"aliases,omitempty"`
SearchWords []string `json:"search_words,omitempty" yaml:"search_words,omitempty"`
Type string `validate:"required" json:"type" jsonschema:"enum=github_release,enum=github_content,enum=github_archive,enum=http,enum=go,enum=go_install,enum=cargo,enum=go_build"`
RepoOwner string `yaml:"repo_owner,omitempty" json:"repo_owner,omitempty"`
RepoName string `yaml:"repo_name,omitempty" json:"repo_name,omitempty"`
Description string `json:"description,omitempty" yaml:",omitempty"`
Link string `json:"link,omitempty" yaml:",omitempty"`
Asset string `json:"asset,omitempty" yaml:",omitempty"`
Crate string `json:"crate,omitempty" yaml:",omitempty"`
Cargo *Cargo `json:"cargo,omitempty"`
URL string `json:"url,omitempty" yaml:",omitempty"`
Path string `json:"path,omitempty" yaml:",omitempty"`
Format string `json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip,example=dmg" yaml:",omitempty"`
Overrides []*Override `json:"overrides,omitempty" yaml:",omitempty"`
FormatOverrides []*FormatOverride `yaml:"format_overrides,omitempty" json:"format_overrides,omitempty"`
Files []*File `json:"files,omitempty" yaml:",omitempty"`
Replacements Replacements `json:"replacements,omitempty" yaml:",omitempty"`
SupportedEnvs SupportedEnvs `yaml:"supported_envs,omitempty" json:"supported_envs,omitempty"`
VersionFilter string `yaml:"version_filter,omitempty" json:"version_filter,omitempty"`
VersionPrefix string `yaml:"version_prefix,omitempty" json:"version_prefix,omitempty"`
Rosetta2 bool `yaml:",omitempty" json:"rosetta2,omitempty"`
NoAsset bool `yaml:"no_asset,omitempty" json:"no_asset,omitempty"`
VersionSource string `json:"version_source,omitempty" yaml:"version_source,omitempty" jsonschema:"enum=github_tag"`
CompleteWindowsExt *bool `json:"complete_windows_ext,omitempty" yaml:"complete_windows_ext,omitempty"`
WindowsExt string `json:"windows_ext,omitempty" yaml:"windows_ext,omitempty"`
Checksum *Checksum `json:"checksum,omitempty"`
Cosign *Cosign `json:"cosign,omitempty"`
SLSAProvenance *SLSAProvenance `json:"slsa_provenance,omitempty" yaml:"slsa_provenance,omitempty"`
Private bool `json:"private,omitempty"`
Name string `json:"name,omitempty" yaml:",omitempty"`
Aliases []*Alias `yaml:",omitempty" json:"aliases,omitempty"`
SearchWords []string `json:"search_words,omitempty" yaml:"search_words,omitempty"`
Type string `validate:"required" json:"type" jsonschema:"enum=github_release,enum=github_content,enum=github_archive,enum=http,enum=go,enum=go_install,enum=cargo,enum=go_build"`
RepoOwner string `yaml:"repo_owner,omitempty" json:"repo_owner,omitempty"`
RepoName string `yaml:"repo_name,omitempty" json:"repo_name,omitempty"`
Description string `json:"description,omitempty" yaml:",omitempty"`
Link string `json:"link,omitempty" yaml:",omitempty"`
Asset string `json:"asset,omitempty" yaml:",omitempty"`
Crate string `json:"crate,omitempty" yaml:",omitempty"`
Cargo *Cargo `json:"cargo,omitempty"`
URL string `json:"url,omitempty" yaml:",omitempty"`
Path string `json:"path,omitempty" yaml:",omitempty"`
Format string `json:"format,omitempty" jsonschema:"example=tar.gz,example=raw,example=zip,example=dmg" yaml:",omitempty"`
Overrides []*Override `json:"overrides,omitempty" yaml:",omitempty"`
FormatOverrides []*FormatOverride `yaml:"format_overrides,omitempty" json:"format_overrides,omitempty"`
Files []*File `json:"files,omitempty" yaml:",omitempty"`
Replacements Replacements `json:"replacements,omitempty" yaml:",omitempty"`
SupportedEnvs SupportedEnvs `yaml:"supported_envs,omitempty" json:"supported_envs,omitempty"`
VersionFilter string `yaml:"version_filter,omitempty" json:"version_filter,omitempty"`
VersionPrefix string `yaml:"version_prefix,omitempty" json:"version_prefix,omitempty"`
Rosetta2 bool `yaml:",omitempty" json:"rosetta2,omitempty"`
NoAsset bool `yaml:"no_asset,omitempty" json:"no_asset,omitempty"`
VersionSource string `json:"version_source,omitempty" yaml:"version_source,omitempty" jsonschema:"enum=github_tag"`
CompleteWindowsExt *bool `json:"complete_windows_ext,omitempty" yaml:"complete_windows_ext,omitempty"`
WindowsExt string `json:"windows_ext,omitempty" yaml:"windows_ext,omitempty"`
Checksum *Checksum `json:"checksum,omitempty"`
Cosign *Cosign `json:"cosign,omitempty"`
SLSAProvenance *SLSAProvenance `json:"slsa_provenance,omitempty" yaml:"slsa_provenance,omitempty"`
Private bool `json:"private,omitempty"`
Build *Build `json:"build,omitempty" yaml:",omitempty"`
ErrorMessage string `json:"-" yaml:"-"`
AppendExt *bool `json:"append_ext,omitempty" yaml:"append_ext,omitempty"`

VersionConstraints string `yaml:"version_constraint,omitempty" json:"version_constraint,omitempty"`
VersionOverrides []*VersionOverride `yaml:"version_overrides,omitempty" json:"version_overrides,omitempty"`
ErrorMessage string `json:"-" yaml:"-"`
AppendExt *bool `json:"append_ext,omitempty" yaml:"append_ext,omitempty"`
}

type Build struct {
Type string `validate:"required" json:"type" jsonschema:"enum=enum=go_install,enum=go_build"`
Path string `json:"path,omitempty" yaml:",omitempty"`
Files []*File `json:"files,omitempty" yaml:",omitempty"`
ExcludedEnvs SupportedEnvs `yaml:"excluded_envs,omitempty" json:"excluded_envs,omitempty"`
Enabled *bool `json:"enabled,omitempty" yaml:",omitempty"`
}

func (b *Build) CheckEnabled() bool {
if b == nil {
return false
}
if b.Enabled == nil {
return true
}
return *b.Enabled
}

func (p *PackageInfo) GetAppendExt() bool {
Expand Down Expand Up @@ -93,6 +113,7 @@ type VersionOverride struct {
ErrorMessage *string `json:"error_message,omitempty" yaml:"error_message,omitempty"`
NoAsset *bool `yaml:"no_asset,omitempty" json:"no_asset,omitempty"`
AppendExt *bool `json:"append_ext,omitempty" yaml:"append_ext,omitempty"`
Build *Build `json:"build,omitempty" yaml:",omitempty"`
}

type Override struct {
Expand Down Expand Up @@ -150,6 +171,7 @@ func (p *PackageInfo) Copy() *PackageInfo {
ErrorMessage: p.ErrorMessage,
NoAsset: p.NoAsset,
AppendExt: p.AppendExt,
Build: p.Build,
}
return pkg
}
Expand Down Expand Up @@ -295,6 +317,9 @@ func (p *PackageInfo) overrideVersion(child *VersionOverride) *PackageInfo { //n
if child.AppendExt != nil {
pkg.AppendExt = child.AppendExt
}
if child.Build != nil {
pkg.Build = child.Build
}
return pkg
}

Expand Down Expand Up @@ -382,6 +407,16 @@ func (p *PackageInfo) OverrideByRuntime(rt *runtime.Runtime) { //nolint:cyclop,f
}
}

func (p *PackageInfo) OverrideByBuild() {
p.Type = p.Build.Type
if p.Build.Path != "" {
p.Path = p.Build.Path
}
if p.Build.Files != nil {
p.Files = p.Build.Files
}
}

type FormatOverrides []*FormatOverride

func (o FormatOverrides) IsZero() bool {
Expand Down
34 changes: 26 additions & 8 deletions pkg/config/registry/supported_envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,43 @@ import (
)

func (p *PackageInfo) CheckSupported(rt *runtime.Runtime, env string) (bool, error) {
if p.SupportedEnvs != nil {
return p.CheckSupportedEnvs(rt.GOOS, rt.GOARCH, env), nil
if p.CheckSupportedEnvs(rt.GOOS, rt.GOARCH, env) {
return true, nil
}
return true, nil
if !p.Build.CheckEnabled() {
return false, nil
}
if p.checkExcludedEnvs(rt.GOOS, rt.GOARCH, env) {
p.OverrideByBuild()
return true, nil
}
return false, nil
}

func (p *PackageInfo) CheckSupportedEnvs(goos, goarch, env string) bool {
if p.SupportedEnvs == nil {
return true
}
for _, supportedEnv := range p.SupportedEnvs {
switch supportedEnv {
return matchEnvs(p.SupportedEnvs, goos, goarch, env, p.Rosetta2)
}

func (p *PackageInfo) checkExcludedEnvs(goos, goarch, env string) bool {
if p.Build.ExcludedEnvs == nil {
return true
}
return !matchEnvs(p.Build.ExcludedEnvs, goos, goarch, env, p.Rosetta2)
}

func matchEnvs(envs []string, goos, goarch, env string, rosetta2 bool) bool {
for _, elem := range envs {
switch elem {
case goos, goarch, env, "all":
return true
}
}
if goos == "darwin" && goarch == "arm64" && p.Rosetta2 {
for _, supportedEnv := range p.SupportedEnvs {
switch supportedEnv {
if goos == "darwin" && goarch == "arm64" && rosetta2 {
for _, elem := range envs {
switch elem {
case "amd64", "darwin/amd64":
return true
}
Expand Down