Skip to content

Commit

Permalink
Checksum selection
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed May 12, 2023
1 parent 936d04c commit f28728b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 38 deletions.
14 changes: 1 addition & 13 deletions pkg/ghet/download/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package download
import (
"context"
"fmt"
"strings"

pkggithub "github.com/cardil/ghet/pkg/github"
githubapi "github.com/cardil/ghet/pkg/github/api"
Expand Down Expand Up @@ -54,7 +53,7 @@ func CreatePlan(ctx context.Context, args Args) (*Plan, error) {
log.WithFields(slog.Fields{"assets": namesOf(rr.Assets)}).
Debug("Checking assets")
for _, asset := range rr.Assets {
if assetMatches(asset, args) {
if args.Asset.Matches(asset.GetName()) {
a := githubapi.Asset{
ID: asset.GetID(),
Name: asset.GetName(),
Expand Down Expand Up @@ -144,14 +143,3 @@ func fetchRelease(
}
return rr, r, nil
}

func assetMatches(asset *github.ReleaseAsset, args Args) bool {
name := strings.ToLower(asset.GetName())
basename := strings.ToLower(args.Asset.BaseName)
coords := strings.TrimPrefix(name, basename)
return strings.Contains(name, args.Checksums.ToString()) ||
(strings.HasPrefix(name, basename) &&
args.Architecture.Matches(coords) &&
args.OperatingSystem.Matches(coords))

}
19 changes: 0 additions & 19 deletions pkg/ghet/download/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ func TestCreatePlan(t *testing.T) {
}, {
Name: "kn-event-darwin-arm64",
Size: 58_727_008,
}, {
Name: "checksums.txt.pem",
Size: 1_348,
ContentType: "application/x-x509-ca-cert",
}, {
Name: "checksums.txt.sig",
Size: 96,
ContentType: "application/pgp-signature",
}},
}},
}, {
Expand All @@ -59,14 +51,6 @@ func TestCreatePlan(t *testing.T) {
}, {
Name: "kn-event-darwin-arm64",
Size: 58_542_306,
}, {
Name: "checksums.txt.pem",
Size: 1_348,
ContentType: "application/octet-stream",
}, {
Name: "checksums.txt.sig",
Size: 96,
ContentType: "application/octet-stream",
}},
}},
}, {
Expand Down Expand Up @@ -240,9 +224,6 @@ func (tc resolvedCreatePlanTestCase) performTest(ctx context.Context, t testingT
Repo: tc.args.repo,
},
},
Checksums: github.Checksums{
FileName: github.FileName{BaseName: "checksums", Extension: "txt"},
},
},
Site: config.Site{Type: config.TypeGitHub},
},
Expand Down
6 changes: 0 additions & 6 deletions pkg/ghet/install/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ func (a Args) WithDefaults() Args {
Release: github.Release{
Tag: "latest",
},
Checksums: github.Checksums{
FileName: github.FileName{
BaseName: "checksums",
Extension: "txt",
},
},
},
Site: config.Site{
Type: config.TypeGitHub,
Expand Down
17 changes: 17 additions & 0 deletions pkg/github/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ const (
ArchS390X Architecture = "s390x"
)

func noArchMatches(name string) bool {
archs := []Architecture{
ArchX86,
ArchAMD64,
ArchARM,
ArchARM64,
ArchPPC64LE,
ArchS390X,
}
for _, arch := range archs {
if arch.Matches(name) {
return false
}
}
return true
}

func (a Architecture) Matches(name string) bool {
return matchWith(name, archMatchers[a])
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/github/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package github
import (
"path"
"strings"

"github.com/cardil/ghet/pkg/match"
)

type FileName struct {
Expand All @@ -21,10 +23,47 @@ func (n FileName) ToString() string {
return n.BaseName + joiner + n.Extension
}

func (n FileName) isEmpty() bool {
return n.BaseName == "" && n.Extension == ""
}

type Checksums struct {
FileName
}

func emptyChecksums() []Checksums {
return []Checksums{
{NewFileName("checksums.txt")},
{NewFileName("checksums.out")},
{NewFileName("sha256sum.txt")},
{NewFileName("sha256sum.out")},
{NewFileName("sha512sum.txt")},
{NewFileName("sha512sum.out")},
{FileName{Extension: "sha256"}},
{FileName{Extension: "sha256sum"}},
{FileName{Extension: "sha512"}},
{FileName{Extension: "sha512sum"}},
}
}

func (c Checksums) matcher(basename string, arch Architecture, sys OperatingSystem) match.Matcher {
if c.isEmpty() {
cc := emptyChecksums()
mm := make([]match.Matcher, len(cc))
for i, ch := range cc {
mm[i] = ch.matcher(basename, arch, sys)
}
return match.Any(mm...)
}
return match.MatcherFn(func(name string) bool {
return c.ToString() == name ||
strings.HasPrefix(name, basename) &&
strings.HasSuffix(name, c.ToString()) &&
((arch.Matches(name) && sys.Matches(name)) ||
(noArchMatches(name) && noOsMatches(name)))
})
}

type Asset struct {
FileName
Architecture
Expand All @@ -44,3 +83,14 @@ func NewFileName(s string) FileName {
Extension: ext,
}
}

func (a Asset) Matches(filename string) bool {
name := strings.ToLower(filename)
basename := strings.ToLower(a.FileName.BaseName)
coords := strings.TrimPrefix(name, basename)
cm := a.Checksums.matcher(basename, a.Architecture, a.OperatingSystem)
return cm.Matches(name) ||
(strings.HasPrefix(name, basename) &&
a.Architecture.Matches(coords) &&
a.OperatingSystem.Matches(coords))
}
15 changes: 15 additions & 0 deletions pkg/github/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ func (os OperatingSystem) Matches(name string) bool {
return matchWith(name, osMatchers[os])
}

func noOsMatches(name string) bool {
oss := []OperatingSystem{
OSDarwin,
OSLinuxMusl,
OSLinuxGnu,
OSWindows,
}
for _, os := range oss {
if os.Matches(name) {
return false
}
}
return true
}

func CurrentOS() OperatingSystem {
family := OsFamily(runtime.GOOS)
//goland:noinspection GoBoolExpressions
Expand Down

0 comments on commit f28728b

Please sign in to comment.