Skip to content

Commit

Permalink
improve toolselection
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Sep 18, 2022
1 parent 8bf6ff6 commit 8969107
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 107 deletions.
7 changes: 0 additions & 7 deletions .toolbox.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ tools:
downloadURL: https://get.helm.sh/helm-{{ .Version }}-{{ .OS }}-{{ .Arch }}.tar.gz
jq:
github: stedolan/jq
fileNames:
linux: jq-linux64
windows: jq-win64.exe
yq:
github: mikefarah/yq
fileNames:
linux: yq_linux_amd64
windows: yq_windows_amd64.exe

target: ./download
76 changes: 53 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"text/template"
Expand All @@ -18,6 +19,14 @@ import (
"gopkg.in/yaml.v3"
)

var (
aliases = map[string][]string{
"amd64": {"x86_64", "64"},
"windows": {"win", "win64"},
"linux": {"linux64"},
}
)

func main() {

tb, err := readToolbox()
Expand All @@ -28,6 +37,9 @@ func main() {
if tb.Target == "" {
tb.Target = "./tools"
}
if tb.Aliases != nil {
aliases = *tb.Aliases
}

if _, err := os.Stat(tb.Target); err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -92,29 +104,47 @@ func main() {
panic(err)
}
} else if ghr != nil {
for _, a := range ghr.Assets {
if tool.FileNameForOS() != "" {
if a.Name == tool.FileNameForOS() {
if err := fetchTool(tmp, a.Name, tool.Name, a.BrowserDownloadUrl, tb.Target); err != nil {
panic(err)
}
}
} else if strings.Contains(a.Name, tool.Name) && matches(runtime.GOOS, a.Name) && matches(runtime.GOARCH, a.Name) {
if err := fetchTool(tmp, tool.Name, tool.Name, a.BrowserDownloadUrl, tb.Target); err != nil {
panic(err)
}
matching := findMatching(tool.Name, ghr.Assets)
if matching != nil {
if err := fetchTool(tmp, tool.Name, tool.Name, matching.BrowserDownloadUrl, tb.Target); err != nil {
panic(err)
}
for _, add := range tool.Additional {

if strings.Contains(a.Name, add) && matches(runtime.GOOS, a.Name) && matches(runtime.GOARCH, a.Name) {
if err := fetchTool(tmp, add, add, a.BrowserDownloadUrl, tb.Target); err != nil {
panic(err)
}
}
for _, add := range tool.Additional {
matching := findMatching(add, ghr.Assets)
if matching != nil {
if err := fetchTool(tmp, add, add, matching.BrowserDownloadUrl, tb.Target); err != nil {
panic(err)
}
}
}
}
println()
}
}

func findMatching(toolName string, assets []types.Asset) *types.Asset {
var matching []*types.Asset
for i := range assets {
a := assets[i]
if strings.Contains(a.Name, toolName) && matches(runtime.GOOS, a.Name) {
matching = append(matching, &a)
}
}
sort.Slice(matching, func(i, j int) bool {
mi := matches(runtime.GOARCH, matching[i].Name)
mj := matches(runtime.GOARCH, matching[j].Name)

if mi == mj {
return true
}

return mi
})
if len(matching) == 0 {
return nil
}
return matching[0]
}

func parseTemplate(templ string, version string) string {
Expand Down Expand Up @@ -149,9 +179,13 @@ func fetchTool(tmp string, remoteToolName string, trueToolName string, url strin
if err := downloadFile(path, url); err != nil {
return err
}
if err := extract.File(path, dir); err != nil {
extracted, err := extract.File(path, dir)
if err != nil {
return err
}
if !extracted {
remoteToolName = fileName
}
ok, err := copyTool(dir, remoteToolName, targetDir, trueToolName)
if err != nil {
return err
Expand All @@ -172,7 +206,7 @@ func copyTool(dir string, fileName string, targetDir string, targetName string)
if file.IsDir() {
dirs = append(dirs, file)
}
if file.Name() == binaryName(fileName) {
if file.Name() == binaryName(fileName) || file.Name() == binaryName(fmt.Sprintf("%s_%s_%s", fileName, runtime.GOOS, runtime.GOARCH)) {

if err := copyFile(dir, file, targetDir, targetName); err != nil {
return false, err
Expand Down Expand Up @@ -238,10 +272,6 @@ func matches(info string, name string) bool {
return false
}

var (
aliases = map[string][]string{"amd64": {"x86_64"}}
)

func downloadFile(path string, url string) (err error) {
resp, err := grab.Get(path, url)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ import (
"github.com/xi2/xz"
)

func File(file, target string) error {
func File(file, target string) (bool, error) {
if strings.HasSuffix(file, ".tar.gz") {
log.Printf("Extracting %s", file)
return tarGz(file, target)
return true, tarGz(file, target)
}
if strings.HasSuffix(file, ".zip") {
log.Printf("Extracting %s", file)
return unzip(file, target)
return true, unzip(file, target)
}
if strings.HasSuffix(file, ".tar.xz") {
log.Printf("Extracting %s", file)
return tarXz(file, target)
return true, tarXz(file, target)
}
return nil
return false, nil
}

func unzip(file string, target string) error {
Expand Down
25 changes: 10 additions & 15 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ const (
)

type Toolbox struct {
Tools map[string]*Tool `yaml:"tools"`
Target string `yaml:"target"`
CreateTarget *bool `yaml:"createTarget"`
Tools map[string]*Tool `yaml:"tools"`
Target string `yaml:"target"`
CreateTarget *bool `yaml:"createTarget"`
Aliases *map[string][]string `yaml:"aliases"`
}

type Tool struct {
Name string `yaml:"name"`
Github string `yaml:"github"`
Google string `yaml:"google"`
DownloadURL string `yaml:"downloadURL"`
Version string `yaml:"version"`
Additional []string `yaml:"additional"`
FileNames *FileNames `yaml:"fileNames"`
}

type FileNames struct {
Linux string `yaml:"linux"`
Windows string `yaml:"windows"`
Name string `yaml:"name"`
Github string `yaml:"github"`
Google string `yaml:"google"`
DownloadURL string `yaml:"downloadURL"`
Version string `yaml:"version"`
Additional []string `yaml:"additional"`
}

func (t *Tool) LatestURL() string {
Expand Down
76 changes: 39 additions & 37 deletions pkg/types/types_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,43 @@ import (
)

type GithubRelease struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
Assets []struct {
Url string `json:"url"`
Id int `json:"id"`
NodeId string `json:"node_id"`
Name string `json:"name"`
Label interface{} `json:"label"`
Uploader struct {
Login string `json:"login"`
Id int `json:"id"`
NodeId string `json:"node_id"`
AvatarUrl string `json:"avatar_url"`
GravatarId string `json:"gravatar_id"`
Url string `json:"url"`
HtmlUrl string `json:"html_url"`
FollowersUrl string `json:"followers_url"`
FollowingUrl string `json:"following_url"`
GistsUrl string `json:"gists_url"`
StarredUrl string `json:"starred_url"`
SubscriptionsUrl string `json:"subscriptions_url"`
OrganizationsUrl string `json:"organizations_url"`
ReposUrl string `json:"repos_url"`
EventsUrl string `json:"events_url"`
ReceivedEventsUrl string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
} `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int `json:"size"`
DownloadCount int `json:"download_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BrowserDownloadUrl string `json:"browser_download_url"`
} `json:"assets"`
Body string `json:"body"`
TagName string `json:"tag_name"`
Name string `json:"name"`
Assets []Asset `json:"assets"`
Body string `json:"body"`
}

type Asset struct {
Url string `json:"url"`
Id int `json:"id"`
NodeId string `json:"node_id"`
Name string `json:"name"`
Label interface{} `json:"label"`
Uploader struct {
Login string `json:"login"`
Id int `json:"id"`
NodeId string `json:"node_id"`
AvatarUrl string `json:"avatar_url"`
GravatarId string `json:"gravatar_id"`
Url string `json:"url"`
HtmlUrl string `json:"html_url"`
FollowersUrl string `json:"followers_url"`
FollowingUrl string `json:"following_url"`
GistsUrl string `json:"gists_url"`
StarredUrl string `json:"starred_url"`
SubscriptionsUrl string `json:"subscriptions_url"`
OrganizationsUrl string `json:"organizations_url"`
ReposUrl string `json:"repos_url"`
EventsUrl string `json:"events_url"`
ReceivedEventsUrl string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
} `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int `json:"size"`
DownloadCount int `json:"download_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BrowserDownloadUrl string `json:"browser_download_url"`
}
10 changes: 0 additions & 10 deletions pkg/types/typse_unix.go

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/types/typse_windows.go

This file was deleted.

0 comments on commit 8969107

Please sign in to comment.