-
Notifications
You must be signed in to change notification settings - Fork 56
/
dependenciesutils.go
146 lines (126 loc) · 4.82 KB
/
dependenciesutils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package utils
import (
"errors"
"fmt"
bintrayutils "github.com/jfrog/jfrog-cli-core/bintray/utils"
"github.com/jfrog/jfrog-cli-core/utils/config"
"github.com/jfrog/jfrog-cli-core/utils/coreutils"
rthttpclient "github.com/jfrog/jfrog-client-go/artifactory/httpclient"
"github.com/jfrog/jfrog-client-go/bintray"
"github.com/jfrog/jfrog-client-go/bintray/auth"
"github.com/jfrog/jfrog-client-go/bintray/services"
"github.com/jfrog/jfrog-client-go/bintray/services/utils"
"github.com/jfrog/jfrog-client-go/httpclient"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"net/http"
"os"
"path"
)
const (
// This env var should be used for downloading the extractor jars through an Artifactory remote
// repository, instead of downloading directly from jcenter. The remote repository should be
// configured to proxy jcenter.
// The env var should store a server ID configured by JFrog CLI.
JCenterRemoteServerEnv = "JFROG_CLI_JCENTER_REMOTE_SERVER"
// If the JCenterRemoteServerEnv env var is used, a maven remote repository named jcenter is assumed.
// This env var can be used to use a different remote repository name.
JCenterRemoteRepoEnv = "JFROG_CLI_JCENTER_REMOTE_REPO"
)
// Download the relevant build-info-extractor jar, if it does not already exist locally.
// By default, the jar is downloaded directly from jcenter.
// If the JCenterRemoteServerEnv environment variable is configured, the jar will be
// downloaded from a remote Artifactory repository which proxies jcenter.
//
// downloadPath: The Bintray or Artifactory download path.
// filename: The local file name.
// targetPath: The local download path (without the file name).
func DownloadExtractorIfNeeded(downloadPath, targetPath string) error {
// If the file exists locally, we're done.
exists, err := fileutils.IsFileExists(targetPath, false)
if exists || err != nil {
return err
}
artDetails, remotePath, err := GetJcenterRemoteDetails(downloadPath)
if err != nil {
return err
}
// Download through a remote repository in Artifactory, if configured to do so.
if artDetails != nil {
return downloadFileFromArtifactory(artDetails, remotePath, targetPath)
}
// If not configured to download through a remote repository in Artifactory,
// download from jcenter.
return downloadFileFromBintray(remotePath, targetPath)
}
func GetJcenterRemoteDetails(downloadPath string) (artDetails *config.ArtifactoryDetails, remotePath string, err error) {
// Download through a remote repository in Artifactory, if configured to do so.
serverId := os.Getenv(JCenterRemoteServerEnv)
if serverId != "" {
artDetails, err = config.GetArtifactorySpecificConfig(serverId, false, true)
if err != nil {
return
}
remotePath = path.Join(getJcenterRemoteRepoName(), downloadPath)
return
}
// If not configured to download through a remote repository in Artifactory,
// download from jcenter.
remotePath = path.Join("bintray/jcenter", downloadPath)
return
}
func getJcenterRemoteRepoName() string {
jcenterRemoteRepo := os.Getenv(JCenterRemoteRepoEnv)
if jcenterRemoteRepo == "" {
jcenterRemoteRepo = "jcenter"
}
return jcenterRemoteRepo
}
func downloadFileFromArtifactory(artDetails *config.ArtifactoryDetails, downloadPath, targetPath string) error {
downloadUrl := fmt.Sprintf("%s%s", artDetails.Url, downloadPath)
log.Info("Downloading build-info-extractor from", downloadUrl)
filename, localDir := fileutils.GetFileAndDirFromPath(targetPath)
downloadFileDetails := &httpclient.DownloadFileDetails{
FileName: filename,
DownloadPath: downloadUrl,
LocalPath: localDir,
LocalFileName: filename,
}
auth, err := artDetails.CreateArtAuthConfig()
if err != nil {
return err
}
certsPath, err := coreutils.GetJfrogCertsDir()
if err != nil {
return err
}
client, err := rthttpclient.ArtifactoryClientBuilder().
SetCertificatesPath(certsPath).
SetInsecureTls(artDetails.InsecureTls).
SetServiceDetails(&auth).
Build()
if err != nil {
return err
}
httpClientDetails := auth.CreateHttpClientDetails()
resp, err := client.DownloadFile(downloadFileDetails, "", &httpClientDetails, 3, false)
if err == nil && resp.StatusCode != http.StatusOK {
err = errorutils.CheckError(errors.New(resp.Status + " received when attempting to download " + downloadUrl))
}
return err
}
func downloadFileFromBintray(downloadPath, targetPath string) error {
bintrayConfig := auth.NewBintrayDetails()
config := bintray.NewConfigBuilder().SetBintrayDetails(bintrayConfig).Build()
pathDetails, err := utils.CreatePathDetails(downloadPath)
if err != nil {
return err
}
params := &services.DownloadFileParams{}
params.PathDetails = pathDetails
params.TargetPath = targetPath
params.Flat = true
_, _, err = bintrayutils.DownloadFileFromBintray(config, params)
return err
}