diff --git a/pkg/asset/asset.go b/pkg/asset/asset.go index bd6cad4..62d3f68 100644 --- a/pkg/asset/asset.go +++ b/pkg/asset/asset.go @@ -218,7 +218,9 @@ func (a *Asset) copyFile(srcFile, dstFile string) error { return nil } -func (a *Asset) Install(id, binDir string) error { +// Install installs the asset +// TODO(ek): simplify this function +func (a *Asset) Install(id, binDir string) error { //nolint:funlen found := false logrus.Tracef("files to process: %d", len(a.Files)) diff --git a/pkg/source/github.go b/pkg/source/github.go index bc8c71c..ab677a6 100644 --- a/pkg/source/github.go +++ b/pkg/source/github.go @@ -3,7 +3,6 @@ package source import ( "context" "fmt" - "github.com/sirupsen/logrus" "path/filepath" "strings" @@ -11,6 +10,7 @@ import ( "github.com/google/go-github/v62/github" "github.com/gregjones/httpcache" "github.com/gregjones/httpcache/diskcache" + "github.com/sirupsen/logrus" "github.com/ekristen/distillery/pkg/asset" ) @@ -50,39 +50,40 @@ func (s *GitHub) GetID() string { return strings.Join([]string{s.GetSource(), s.GetOwner(), s.GetRepo(), s.GetOS(), s.GetArch()}, "-") } -// sourceRun - run the source specific logic -func (s *GitHub) sourceRun(ctx context.Context) error { - cacheFile := filepath.Join(s.Options.MetadataDir, fmt.Sprintf("cache-%s", s.GetID())) - - s.client = github.NewClient(httpcache.NewTransport(diskcache.New(cacheFile)).Client()) - githubToken := s.Options.Settings["github-token"].(string) - if githubToken != "" { - log.Debug("auth token provided") - s.client = s.client.WithAuthToken(githubToken) +// Run - run the source +func (s *GitHub) Run(ctx context.Context) error { + if err := s.sourceRun(ctx); err != nil { + return err } - if err := s.FindRelease(ctx); err != nil { + // this is from the Source struct + if err := s.Discover([]string{s.Repo}); err != nil { return err } - if err := s.GetReleaseAssets(ctx); err != nil { + if err := s.commonRun(ctx); err != nil { return err } return nil } -// Run - run the source -func (s *GitHub) Run(ctx context.Context) error { - if err := s.sourceRun(ctx); err != nil { - return err +// sourceRun - run the source specific logic +func (s *GitHub) sourceRun(ctx context.Context) error { + cacheFile := filepath.Join(s.Options.MetadataDir, fmt.Sprintf("cache-%s", s.GetID())) + + s.client = github.NewClient(httpcache.NewTransport(diskcache.New(cacheFile)).Client()) + githubToken := s.Options.Settings["github-token"].(string) + if githubToken != "" { + log.Debug("auth token provided") + s.client = s.client.WithAuthToken(githubToken) } - if err := s.Discover(s.Assets, []string{s.Repo}); err != nil { + if err := s.FindRelease(ctx); err != nil { return err } - if err := s.commonRun(ctx); err != nil { + if err := s.GetReleaseAssets(ctx); err != nil { return err } diff --git a/pkg/source/gitlab.go b/pkg/source/gitlab.go index 43cc29f..e319023 100644 --- a/pkg/source/gitlab.go +++ b/pkg/source/gitlab.go @@ -89,7 +89,7 @@ func (s *GitLab) Run(ctx context.Context) error { return err } - if err := s.Discover(s.Assets, []string{s.Repo}); err != nil { + if err := s.Discover([]string{s.Repo}); err != nil { return err } diff --git a/pkg/source/hashicorp.go b/pkg/source/hashicorp.go index 8114476..06ee526 100644 --- a/pkg/source/hashicorp.go +++ b/pkg/source/hashicorp.go @@ -97,7 +97,7 @@ func (s *Hashicorp) Run(ctx context.Context) error { return err } - if err := s.Discover(s.Assets, []string{s.Repo}); err != nil { + if err := s.Discover([]string{s.Repo}); err != nil { return err } diff --git a/pkg/source/homebrew.go b/pkg/source/homebrew.go index 7cdd4aa..9d7177d 100644 --- a/pkg/source/homebrew.go +++ b/pkg/source/homebrew.go @@ -98,7 +98,7 @@ func (s *Homebrew) Run(ctx context.Context) error { return err } - if err := s.Discover(s.Assets, []string{s.Formula}); err != nil { + if err := s.Discover([]string{s.Formula}); err != nil { return err } diff --git a/pkg/source/source.go b/pkg/source/source.go index d345c54..c062104 100644 --- a/pkg/source/source.go +++ b/pkg/source/source.go @@ -66,6 +66,7 @@ func (s *Source) GetArch() string { return s.Options.Arch } +// commonRun - common run logic for all sources that includes download, extract, install and cleanup func (s *Source) commonRun(ctx context.Context) error { if err := s.Download(ctx); err != nil { return err @@ -91,13 +92,13 @@ func (s *Source) commonRun(ctx context.Context) error { // Discover will attempt to discover and categorize the assets provided // TODO(ek): split up and refactor this function as it's way too complex -func (s *Source) Discover(assets []asset.IAsset, names []string) error { //nolint:funlen,gocyclo +func (s *Source) Discover(names []string) error { //nolint:funlen,gocyclo fileScoring := map[asset.Type][]string{} fileScored := map[asset.Type][]score.Sorted{} - logrus.Tracef("discover: starting - %d", len(assets)) + logrus.Tracef("discover: starting - %d", len(s.Assets)) - for _, a := range assets { + for _, a := range s.Assets { if _, ok := fileScoring[a.GetType()]; !ok { fileScoring[a.GetType()] = []string{} } @@ -167,7 +168,7 @@ func (s *Source) Discover(assets []asset.IAsset, names []string) error { //nolin logrus.Tracef("skipped > (%d) too low: %s (%d)", t, topScored.Key, topScored.Value) continue } - for _, a := range assets { + for _, a := range s.Assets { if topScored.Key == a.GetName() { s.Binary = a break @@ -227,7 +228,7 @@ func (s *Source) Discover(assets []asset.IAsset, names []string) error { //nolin } } - for _, a := range assets { + for _, a := range s.Assets { for k, v := range fileScored { vv := v[0] diff --git a/pkg/source/source_test.go b/pkg/source/source_test.go index edb7213..ad8f0b6 100644 --- a/pkg/source/source_test.go +++ b/pkg/source/source_test.go @@ -2,13 +2,13 @@ package source_test import ( "fmt" - "github.com/ekristen/distillery/pkg/asset" - "github.com/ekristen/distillery/pkg/osconfig" - "github.com/sirupsen/logrus" "testing" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "github.com/ekristen/distillery/pkg/asset" + "github.com/ekristen/distillery/pkg/osconfig" "github.com/ekristen/distillery/pkg/source" ) @@ -135,7 +135,7 @@ func TestSourceDiscover(t *testing.T) { "SHA512SUMS", "SHA512SUMS.sig", }, - matrix: []testSourceDiscoverMatrix{ + matrix: []testSourceDiscoverMatrix{ //nolint:dupl { os: "darwin", arch: "amd64", @@ -300,7 +300,7 @@ func TestSourceDiscover(t *testing.T) { "cosign_checksums.txt-keyless.sig", "release-cosign.pub", }, - matrix: []testSourceDiscoverMatrix{ + matrix: []testSourceDiscoverMatrix{ //nolint:dupl { os: "darwin", arch: "amd64", @@ -362,18 +362,14 @@ func TestSourceDiscover(t *testing.T) { os: "darwin", arch: "amd64", expected: testSourceDiscoverExpected{ - binary: "acorn-v0.10.1-macOS-universal.tar.gz", - signature: "", - checksum: "", + binary: "acorn-v0.10.1-macOS-universal.tar.gz", }, }, { os: "darwin", arch: "arm64", expected: testSourceDiscoverExpected{ - binary: "acorn-v0.10.1-macOS-universal.tar.gz", - signature: "", - checksum: "", + binary: "acorn-v0.10.1-macOS-universal.tar.gz", }, }, { @@ -435,7 +431,7 @@ func TestSourceDiscover(t *testing.T) { Assets: assets, } - err := testSource.Discover(assets, []string{tc.name}) + err := testSource.Discover([]string{tc.name}) assert.NoError(t, err) if m.expected.binary != "" {