forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaries.go
69 lines (56 loc) · 1.59 KB
/
binaries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package binaries
import (
"fmt"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"os/exec"
"runtime"
"github.com/pkg/errors"
)
const EksctlVersion = "0.1.3"
const HeptioAuthenticatorAwsVersion = "1.10.3"
func BinaryWithExtension(binary string) string {
if runtime.GOOS == "windows" {
return binary + ".exe"
}
return binary
}
func LookupForBinary(binary string) (string, error) {
path, err := exec.LookPath(BinaryWithExtension(binary))
if err != nil {
return "", err
}
return path, nil
}
type VersionExtractor interface {
arguments() []string
extractVersion(command string, arguments []string) (string, error)
}
func ShouldInstallBinary(binary string, expectedVersion string, versionExtractor VersionExtractor) (bool, error) {
if versionExtractor == nil {
return true, nil
}
binaryPath, err := LookupForBinary(binary)
if err != nil {
return true, errors.Wrap(err, "looking up the binary")
}
if binaryPath != "" {
currentVersion, err := versionExtractor.extractVersion(binaryPath, versionExtractor.arguments())
if err != nil {
return true, errors.Wrap(err, "extracting the version")
}
if currentVersion == expectedVersion {
return false, nil
}
}
return true, nil
}
func DownloadFile(clientURL string, fullPath string) error {
log.Infof("Downloading %s to %s...\n", util.ColorInfo(clientURL), util.ColorInfo(fullPath))
err := util.DownloadFile(fullPath, clientURL)
if err != nil {
return fmt.Errorf("Unable to download file %s from %s due to: %v", fullPath, clientURL, err)
}
log.Infof("Downloaded %s\n", util.ColorInfo(fullPath))
return nil
}