Skip to content

Commit

Permalink
feat: binary url can be remote url or local path
Browse files Browse the repository at this point in the history
  • Loading branch information
snobbee committed Dec 11, 2023
1 parent 78d08fe commit 26c73f4
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions scripts/chain-initiator/download-and-run-version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,37 @@ import (
"strings"
)

func downloadAndRunVersion(binaryURL string, skipDownload bool) (path string, version string, err error) {
func isURL(str string) bool {
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
}

func downloadAndRunVersion(binaryPathOrURL string, skipDownload bool) (path string, version string, err error) {
if !isURL(binaryPathOrURL) {
// If the input is a local path
path = binaryPathOrURL

// Check if the path exists
if _, err = os.Stat(path); os.IsNotExist(err) {
err = errors.New(fmt.Sprintf("binary file does not exist at the specified path: %v", path))
return
}

// Run the command 'binary version'
cmd := exec.Command(path, "version")
var versionOutput []byte
versionOutput, err = cmd.CombinedOutput()
if err != nil {
return
}
version = strings.TrimSpace(string(versionOutput))

return
}

if skipDownload {
// Extract version from the URL
re := regexp.MustCompile(`v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?`)
versionMatches := re.FindStringSubmatch(binaryURL)
versionMatches := re.FindStringSubmatch(binaryPathOrURL)
if len(versionMatches) == 0 {
err = errors.New("no version found in URL")
return
Expand All @@ -41,7 +67,7 @@ func downloadAndRunVersion(binaryURL string, skipDownload bool) (path string, ve
}

// Download the binary
resp, err := http.Get(binaryURL) // nolint: gosec
resp, err := http.Get(binaryPathOrURL) // nolint: gosec
if err != nil {
return
}
Expand Down

0 comments on commit 26c73f4

Please sign in to comment.