Skip to content

Commit

Permalink
allow disabling upx on a single tool
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Oct 21, 2024
1 parent b2e4c2f commit fe9e48e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
26 changes: 13 additions & 13 deletions pkg/fetcher/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,15 @@ func (f *fetcher) downloadViaGithub(tb *types.Toolbox, tool *types.Tool, ghr *ty
tool.CouldNotBeFound = true
if matching != nil {
tool.CouldNotBeFound = false
if err := f.fetchTool(tmp, tool.Name, tool.Name, matching.BrowserDownloadURL, tb.Target, tool.Check); err != nil {
if err := f.fetchTool(tool, tool.Name, matching.BrowserDownloadURL, tmp, tb.Target); err != nil {
return err
}
}
for _, add := range tool.Additional {
matching := findMatching(nil, add, ghr.Assets)
if matching != nil {
tool.CouldNotBeFound = false
if err := f.fetchTool(tmp, add, add, matching.BrowserDownloadURL, tb.Target, tool.Check); err != nil {
if err := f.fetchTool(tool, add, matching.BrowserDownloadURL, tmp, tb.Target); err != nil {
return err
}
}
Expand Down Expand Up @@ -301,7 +301,7 @@ func (f *fetcher) downloadFromURL(client *resty.Client, tb *types.Toolbox, ver m
log.Printf("✅ Skipping since already latest version\n")
return nil
}
return f.fetchTool(tmp, tool.Name, tool.Name, parseTemplate(tool.DownloadURL, tool.Version), tb.Target, tool.Check)
return f.fetchTool(tool, tool.Name, parseTemplate(tool.DownloadURL, tool.Version), tmp, tb.Target)
}

func findMatching(tb *types.Toolbox, toolName string, assets []types.Asset) *types.Asset {
Expand Down Expand Up @@ -381,8 +381,8 @@ func templateData(version string) map[string]string {
}
}

func (f *fetcher) fetchTool(tmp string, remoteToolName string, trueToolName string, url string, targetDir string, check string) error {
dir := fmt.Sprintf("%s/%s", tmp, remoteToolName)
func (f *fetcher) fetchTool(tool *types.Tool, toolName string, url string, tmpDir string, targetDir string) error {
dir := fmt.Sprintf("%s/%s", tmpDir, toolName)
paths := strings.Split(url, "/")
fileName := paths[len(paths)-1]
path := fmt.Sprintf("%s/%s", dir, fileName)
Expand All @@ -395,10 +395,10 @@ func (f *fetcher) fetchTool(tmp string, remoteToolName string, trueToolName stri
return err
}
if !extracted {
remoteToolName = fileName
toolName = fileName
}

return f.moveToTarget(targetDir, trueToolName, dir, remoteToolName, check)
return f.moveToTarget(tool, toolName, targetDir, dir, toolName)
}

func (f *fetcher) validate(targetPath string, check string) error {
Expand Down Expand Up @@ -427,7 +427,7 @@ func (f *fetcher) validate(targetPath string, check string) error {
return nil
}

func (f *fetcher) moveToTarget(targetDir string, trueToolName string, dir string, remoteToolName string, check string) error {
func (f *fetcher) moveToTarget(tool *types.Tool, trueToolName string, targetDir string, dir string, remoteToolName string) error {
targetFilePath, err := filepath.Abs(filepath.Join(targetDir, trueToolName))
if err != nil {
return err
Expand All @@ -440,7 +440,7 @@ func (f *fetcher) moveToTarget(targetDir string, trueToolName string, dir string
}
log.Printf("🔀 Rename current executable to %s", renameTo)
}
ok, err := f.copyTool(dir, remoteToolName, targetDir, trueToolName, check)
ok, err := f.copyTool(tool, dir, remoteToolName, targetDir, trueToolName)
if err != nil {
return err
}
Expand All @@ -450,7 +450,7 @@ func (f *fetcher) moveToTarget(targetDir string, trueToolName string, dir string
return nil
}

func (f *fetcher) copyTool(dir string, fileName string, targetDir string, targetName string, check string) (bool, error) {
func (f *fetcher) copyTool(tool *types.Tool, dir string, fileName string, targetDir string, targetName string) (bool, error) {
files, err := os.ReadDir(dir)
if err != nil {
return false, err
Expand All @@ -468,19 +468,19 @@ func (f *fetcher) copyTool(dir string, fileName string, targetDir string, target
if err := copyFile(sourcePath, targetPath); err != nil {
return false, err
}
if err := f.validate(targetPath, check); err != nil {
if err := f.validate(targetPath, tool.Check); err != nil {
return true, err
}

if f.upx {
if f.upx && !tool.SkipUpx {
f.upxCompress(targetPath)
}

return true, nil
}
}
for _, d := range dirs {
ok, err := f.copyTool(filepath.Join(dir, d.Name()), fileName, targetDir, targetName, check)
ok, err := f.copyTool(tool, filepath.Join(dir, d.Name()), fileName, targetDir, targetName)
if ok || err != nil {
return ok, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Tool struct {
Version string `yaml:"version,omitempty"`
Additional []string `yaml:"additional,omitempty"`
Check string `yaml:"check,omitempty"`
SkipUpx bool `yaml:"skipUpx,omitempty"`
CouldNotBeFound bool `yaml:"-"`
Invalid bool `yaml:"-"`
}
Expand Down

0 comments on commit fe9e48e

Please sign in to comment.