Skip to content

Commit

Permalink
Merge pull request #17 from ekristen/alpha-improvements
Browse files Browse the repository at this point in the history
refactor: cleanup and refactoring
  • Loading branch information
ekristen authored Sep 19, 2024
2 parents f404af6 + b83efd6 commit 1dbffa6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
4 changes: 3 additions & 1 deletion pkg/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
37 changes: 19 additions & 18 deletions pkg/source/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package source
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"path/filepath"
"strings"

"github.com/apex/log"
"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"
)
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/source/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/source/hashicorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/source/homebrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 6 additions & 5 deletions pkg/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down
20 changes: 8 additions & 12 deletions pkg/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -135,7 +135,7 @@ func TestSourceDiscover(t *testing.T) {
"SHA512SUMS",
"SHA512SUMS.sig",
},
matrix: []testSourceDiscoverMatrix{
matrix: []testSourceDiscoverMatrix{ //nolint:dupl
{
os: "darwin",
arch: "amd64",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
},
},
{
Expand Down Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 1dbffa6

Please sign in to comment.