fix: validate download filename for github release installer when it should run on current platform

This commit is contained in:
2026-04-13 01:22:10 +03:00
parent 7960e12fb7
commit d31490dbc9
3 changed files with 43 additions and 6 deletions

View File

@@ -224,7 +224,7 @@ func (c *AppConfig) GetConfigDesc() []string {
filterBuilder.WriteString("Filter: ")
if len(c.Filter) > 0 {
for _, f := range c.Filter {
filterBuilder.WriteString(fmt.Sprintf("\n %s", f))
fmt.Fprintf(&filterBuilder, "\n %s", f)
}
} else {
filterBuilder.WriteString("None")

View File

@@ -109,9 +109,9 @@ func (i *GitHubReleaseInstaller) Validate() []ValidationError {
errors = append(errors, ValidationError{FieldName: "destination", Message: validationIsRequired(), InstallerName: *info.Name})
}
}
if opts.DownloadFilename == nil || len(*opts.DownloadFilename.Resolve()) == 0 {
if opts.DownloadFilename == nil {
errors = append(errors, ValidationError{FieldName: "download_filename", Message: validationIsRequired(), InstallerName: *info.Name})
} else if (*opts.DownloadFilename).Resolve() == nil || len(*(*opts.DownloadFilename).Resolve()) == 0 {
} else if info.Platforms.GetShouldRunOnOS(platform.GetPlatform()) && (opts.DownloadFilename.Resolve() == nil || len(*opts.DownloadFilename.Resolve()) == 0) {
errors = append(errors, ValidationError{FieldName: fmt.Sprintf("download_filename.%s", platform.GetPlatform()), Message: validationIsRequired(), InstallerName: *info.Name})
}
if opts.Strategy != nil {
@@ -205,7 +205,7 @@ func (i *GitHubReleaseInstaller) Install() error {
filename := i.GetFilename()
if filename == "" {
return fmt.Errorf("no download filename provided")
return fmt.Errorf("no download filename matched for the current platform (%s/%s)", runtime.GOOS, runtime.GOARCH)
}
var machineAliases map[string]string
if i.Config.MachineAliases != nil {
@@ -778,7 +778,10 @@ func (i *GitHubReleaseInstaller) GetLatestTag() (string, error) {
func (i *GitHubReleaseInstaller) GetFilename() string {
opts := i.GetOpts()
if opts.DownloadFilename != nil {
return *opts.DownloadFilename.Resolve()
resolved := opts.DownloadFilename.Resolve()
if resolved != nil {
return *resolved
}
}
return ""
}

View File

@@ -4,6 +4,7 @@ import (
"archive/zip"
"bytes"
"compress/gzip"
"fmt"
"os"
"path/filepath"
"runtime"
@@ -75,7 +76,40 @@ func TestGitHubReleaseValidation(t *testing.T) {
},
},
}
assertValidationError(t, newTestGitHubReleaseInstaller(emptyPlatformFilename).Validate(), "download_filename")
assertValidationError(t, newTestGitHubReleaseInstaller(emptyPlatformFilename).Validate(), fmt.Sprintf("download_filename.%s", platform.GetPlatform()))
// 🟢 download_filename only defined for another platform, but installer is restricted to that platform — should pass
otherPlatform := platform.PlatformLinux
if platform.GetPlatform() == platform.PlatformLinux {
otherPlatform = platform.PlatformMacos
}
otherPlatformOnly := &appconfig.InstallerData{
Name: lo.ToPtr("ghr-other-platform-only"),
Type: appconfig.InstallerTypeGitHubRelease,
Platforms: &platform.Platforms{Only: &[]platform.Platform{otherPlatform}},
Opts: &map[string]any{
"repository": "owner/repo",
"destination": "/some/path",
"download_filename": map[string]*string{
string(otherPlatform): lo.ToPtr("file.tar.gz"),
},
},
}
assertNoValidationErrors(t, newTestGitHubReleaseInstaller(otherPlatformOnly).Validate())
// 🔴 download_filename missing for current platform when installer runs on current platform
missingCurrentPlatform := &appconfig.InstallerData{
Name: lo.ToPtr("ghr-missing-current-platform"),
Type: appconfig.InstallerTypeGitHubRelease,
Opts: &map[string]any{
"repository": "owner/repo",
"destination": "/some/path",
"download_filename": map[string]*string{
string(otherPlatform): lo.ToPtr("file.tar.gz"),
},
},
}
assertValidationError(t, newTestGitHubReleaseInstaller(missingCurrentPlatform).Validate(), fmt.Sprintf("download_filename.%s", platform.GetPlatform()))
// 🔴 Invalid strategy
invalidStrategy := &appconfig.InstallerData{