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 replacements #247

Merged
merged 2 commits into from
Sep 12, 2021
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
62 changes: 39 additions & 23 deletions pkg/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,42 +99,54 @@ type PackageInfo interface {
RenderAsset(pkg *Package) (string, error)
GetLink() string
GetDescription() string
GetReplacements() map[string]string
}

type mergedPackageInfo struct {
Name string
Type string
RepoOwner string `yaml:"repo_owner"`
RepoName string `yaml:"repo_name"`
Asset *text.Template
Name string
Type string
RepoOwner string `yaml:"repo_owner"`
RepoName string `yaml:"repo_name"`
Asset *text.Template
ArchiveType string `yaml:"archive_type"`
Files []*File
URL *text.Template
Description string
Link string
Replacements map[string]string
ArchiveTypeOverrides []*ArchiveTypeOverride `yaml:"archive_type_overrides"`
}

type ArchiveTypeOverride struct {
GOOS string
ArchiveType string `yaml:"archive_type"`
Files []*File
URL *text.Template
Description string
Link string
}

func (pkgInfo *mergedPackageInfo) GetPackageInfo() (PackageInfo, error) {
switch pkgInfo.Type {
case pkgInfoTypeGitHubRelease:
return &GitHubReleasePackageInfo{
Name: pkgInfo.Name,
RepoOwner: pkgInfo.RepoOwner,
RepoName: pkgInfo.RepoName,
Asset: pkgInfo.Asset,
ArchiveType: pkgInfo.ArchiveType,
Files: pkgInfo.Files,
Link: pkgInfo.Link,
Description: pkgInfo.Description,
Name: pkgInfo.Name,
RepoOwner: pkgInfo.RepoOwner,
RepoName: pkgInfo.RepoName,
Asset: pkgInfo.Asset,
ArchiveType: pkgInfo.ArchiveType,
ArchiveTypeOverrides: pkgInfo.ArchiveTypeOverrides,
Files: pkgInfo.Files,
Link: pkgInfo.Link,
Description: pkgInfo.Description,
Replacements: pkgInfo.Replacements,
}, nil
case pkgInfoTypeHTTP:
return &HTTPPackageInfo{
Name: pkgInfo.Name,
ArchiveType: pkgInfo.ArchiveType,
URL: pkgInfo.URL,
Files: pkgInfo.Files,
Link: pkgInfo.Link,
Description: pkgInfo.Description,
Name: pkgInfo.Name,
ArchiveType: pkgInfo.ArchiveType,
ArchiveTypeOverrides: pkgInfo.ArchiveTypeOverrides,
URL: pkgInfo.URL,
Files: pkgInfo.Files,
Link: pkgInfo.Link,
Description: pkgInfo.Description,
Replacements: pkgInfo.Replacements,
}, nil
default:
return nil, logerr.WithFields(errInvalidType, logrus.Fields{ //nolint:wrapcheck
Expand All @@ -156,6 +168,10 @@ func (file *File) RenderSrc(pkg *Package, pkgInfo PackageInfo) (string, error) {
"OS": runtime.GOOS,
"Arch": runtime.GOARCH,
"File": file,
"Replacements": map[string]interface{}{
"OS": replace(runtime.GOOS, pkgInfo.GetReplacements()),
"Arch": replace(runtime.GOARCH, pkgInfo.GetReplacements()),
},
})
}

Expand Down
29 changes: 22 additions & 7 deletions pkg/controller/github_release_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
)

type GitHubReleasePackageInfo struct {
Name string `validate:"required"`
ArchiveType string `yaml:"archive_type"`
Link string
Description string
Files []*File `validate:"required,dive"`
Name string `validate:"required"`
ArchiveType string
Link string
Description string
Files []*File `validate:"required,dive"`
Replacements map[string]string
ArchiveTypeOverrides []*ArchiveTypeOverride

RepoOwner string `yaml:"repo_owner" validate:"required"`
RepoName string `yaml:"repo_name" validate:"required"`
RepoOwner string
RepoName string
Asset *text.Template `validate:"required"`
}

Expand All @@ -37,9 +39,18 @@ func (pkgInfo *GitHubReleasePackageInfo) GetDescription() string {
}

func (pkgInfo *GitHubReleasePackageInfo) GetArchiveType() string {
for _, arcTypeOverride := range pkgInfo.ArchiveTypeOverrides {
if arcTypeOverride.GOOS == runtime.GOOS {
return arcTypeOverride.ArchiveType
}
}
return pkgInfo.ArchiveType
}

func (pkgInfo *GitHubReleasePackageInfo) GetReplacements() map[string]string {
return pkgInfo.Replacements
}

func (pkgInfo *GitHubReleasePackageInfo) GetPkgPath(rootDir string, pkg *Package) (string, error) {
assetName, err := pkgInfo.RenderAsset(pkg)
if err != nil {
Expand Down Expand Up @@ -76,5 +87,9 @@ func (pkgInfo *GitHubReleasePackageInfo) RenderAsset(pkg *Package) (string, erro
"PackageInfo": pkgInfo,
"OS": runtime.GOOS,
"Arch": runtime.GOARCH,
"Replacements": map[string]interface{}{
"OS": replace(runtime.GOOS, pkgInfo.Replacements),
"Arch": replace(runtime.GOARCH, pkgInfo.Replacements),
},
})
}
25 changes: 20 additions & 5 deletions pkg/controller/http_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
)

type HTTPPackageInfo struct {
Name string `validate:"required"`
ArchiveType string `yaml:"archive_type"`
Description string
Link string
Files []*File `validate:"required,dive"`
Name string `validate:"required"`
ArchiveType string
Description string
Link string
Files []*File `validate:"required,dive"`
Replacements map[string]string
ArchiveTypeOverrides []*ArchiveTypeOverride

URL *text.Template `validate:"required"`
}
Expand All @@ -36,9 +38,18 @@ func (pkgInfo *HTTPPackageInfo) GetDescription() string {
}

func (pkgInfo *HTTPPackageInfo) GetArchiveType() string {
for _, arcTypeOverride := range pkgInfo.ArchiveTypeOverrides {
if arcTypeOverride.GOOS == runtime.GOOS {
return arcTypeOverride.ArchiveType
}
}
return pkgInfo.ArchiveType
}

func (pkgInfo *HTTPPackageInfo) GetReplacements() map[string]string {
return pkgInfo.Replacements
}

func (pkgInfo *HTTPPackageInfo) GetPkgPath(rootDir string, pkg *Package) (string, error) {
uS, err := pkgInfo.URL.Execute(map[string]interface{}{
"Package": pkg,
Expand Down Expand Up @@ -84,6 +95,10 @@ func (pkgInfo *HTTPPackageInfo) RenderURL(pkg *Package) (string, error) {
"PackageInfo": pkgInfo,
"OS": runtime.GOOS,
"Arch": runtime.GOARCH,
"Replacements": map[string]interface{}{
"OS": replace(runtime.GOOS, pkgInfo.Replacements),
"Arch": replace(runtime.GOARCH, pkgInfo.Replacements),
},
})
if err != nil {
return "", fmt.Errorf("render URL: %w", err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/controller/replacements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package controller

func replace(key string, replacements map[string]string) string {
a := replacements[key]
if a == "" {
return key
}
return a
}