Skip to content

Commit

Permalink
Add binary renaming for easy downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnStarich committed Feb 18, 2019
1 parent 81f7739 commit 3b5359d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dist: out
--image="johnstarich/xgo:1.11-nano" \
--targets="${TARGETS}" \
${DIST_PACKAGE}
go run ./cmd/rename_binaries.go ./out

.PHONY: plugins
plugins: out
Expand Down
53 changes: 53 additions & 0 deletions cmd/rename_binaries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)

var (
versionRegex = regexp.MustCompile(`\d+(\.\d+)+`)
kernelRegex = regexp.MustCompile(`goenable-([^-]+)`)
// make suffixes consistent between download URLs, even if it is technically incorrect for that OS
suffixRegex = regexp.MustCompile(`\.dylib`)
)

func main() {
cliName, args := os.Args[0], os.Args[1:]
if len(args) != 1 {
fmt.Fprintf(os.Stderr, "Usage: %s OUT_DIR\n", filepath.Base(cliName))
os.Exit(2)
}

dir := args[0]
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}

for _, f := range files {
fileName := f.Name()
newFileName := kernelRegex.ReplaceAllStringFunc(fileName, func(s string) string {
s = strings.TrimPrefix(s, "goenable-")
s = strings.Title(s)
return "goenable-" + s
})
newFileName = versionRegex.ReplaceAllLiteralString(newFileName, "")
newFileName = strings.Replace(newFileName, "--", "-", -1)
newFileName = strings.Replace(newFileName, "amd64", "x86_64", 1)
newFileName = suffixRegex.ReplaceAllLiteralString(newFileName, ".so")

if fileName == newFileName {
continue
}
fmt.Printf("Moving %s to %s...\n", fileName, newFileName)
err := os.Rename(filepath.Join(dir, fileName), filepath.Join(dir, newFileName))
if err != nil {
panic(err)
}
}
}

0 comments on commit 3b5359d

Please sign in to comment.