-
Notifications
You must be signed in to change notification settings - Fork 56
/
publish.go
337 lines (293 loc) · 9.51 KB
/
publish.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package npm
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"github.com/jfrog/jfrog-client-go/utils/io/content"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/jfrog/jfrog-cli-core/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/artifactory/utils/npm"
"github.com/jfrog/jfrog-cli-core/utils/config"
"github.com/jfrog/jfrog-client-go/artifactory/buildinfo"
"github.com/jfrog/jfrog-client-go/artifactory/services"
specutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
)
type NpmPublishCommandArgs struct {
NpmCommand
executablePath string
workingDirectory string
collectBuildInfo bool
packedFilePath string
packageInfo *npm.PackageInfo
publishPath string
tarballProvided bool
artifactsDetailsReader *content.ContentReader
}
type NpmPublishCommand struct {
configFilePath string
commandName string
*NpmPublishCommandArgs
}
func NewNpmPublishCommand() *NpmPublishCommand {
return &NpmPublishCommand{NpmPublishCommandArgs: NewNpmPublishCommandArgs(), commandName: "rt_npm_publish"}
}
func NewNpmPublishCommandArgs() *NpmPublishCommandArgs {
return &NpmPublishCommandArgs{}
}
func (npc *NpmPublishCommand) ServerDetails() (*config.ServerDetails, error) {
return npc.serverDetails, nil
}
func (npc *NpmPublishCommand) SetConfigFilePath(configFilePath string) *NpmPublishCommand {
npc.configFilePath = configFilePath
return npc
}
func (nic *NpmPublishCommand) SetArgs(args []string) *NpmPublishCommand {
nic.NpmPublishCommandArgs.npmArgs = args
return nic
}
func (npc *NpmPublishCommand) Run() error {
if npc.configFilePath != "" {
// Read config file.
log.Debug("Preparing to read the config file", npc.configFilePath)
vConfig, err := utils.ReadConfigFile(npc.configFilePath, utils.YAML)
if err != nil {
return err
}
deployerParams, err := utils.GetRepoConfigByPrefix(npc.configFilePath, utils.ProjectConfigDeployerPrefix, vConfig)
if err != nil {
return err
}
_, _, filteredNpmArgs, buildConfiguration, err := npm.ExtractNpmOptionsFromArgs(npc.NpmPublishCommandArgs.npmArgs)
if err != nil {
return err
}
rtDetails, err := deployerParams.ServerDetails()
if err != nil {
return errorutils.CheckError(err)
}
npc.SetBuildConfiguration(buildConfiguration).SetRepo(deployerParams.TargetRepo()).SetNpmArgs(filteredNpmArgs).SetServerDetails(rtDetails)
}
return npc.run()
}
func (npc *NpmPublishCommand) run() error {
log.Info("Running npm Publish")
if err := npc.preparePrerequisites(); err != nil {
return err
}
if !npc.tarballProvided {
if err := npc.pack(); err != nil {
return err
}
}
if err := npc.deploy(); err != nil {
if npc.tarballProvided {
return err
}
// We should delete the tarball we created
return deleteCreatedTarballAndError(npc.packedFilePath, err)
}
if !npc.tarballProvided {
if err := deleteCreatedTarball(npc.packedFilePath); err != nil {
return err
}
}
if !npc.collectBuildInfo {
log.Info("npm publish finished successfully.")
return nil
}
if err := npc.saveArtifactData(); err != nil {
return err
}
log.Info("npm publish finished successfully.")
return nil
}
func (npc *NpmPublishCommand) CommandName() string {
return npc.commandName
}
func (npc *NpmPublishCommand) preparePrerequisites() error {
log.Debug("Preparing prerequisites.")
npmExecPath, err := exec.LookPath("npm")
if err != nil {
return errorutils.CheckError(err)
}
if npmExecPath == "" {
return errorutils.CheckError(errors.New("Could not find 'npm' executable"))
}
npc.executablePath = npmExecPath
log.Debug("Using npm executable:", npc.executablePath)
currentDir, err := os.Getwd()
if err != nil {
return errorutils.CheckError(err)
}
currentDir, err = filepath.Abs(currentDir)
if err != nil {
return errorutils.CheckError(err)
}
npc.workingDirectory = currentDir
log.Debug("Working directory set to:", npc.workingDirectory)
npc.collectBuildInfo = len(npc.buildConfiguration.BuildName) > 0 && len(npc.buildConfiguration.BuildNumber) > 0
if err = npc.setPublishPath(); err != nil {
return err
}
artDetails, err := npc.serverDetails.CreateArtAuthConfig()
if err != nil {
return err
}
if err = utils.CheckIfRepoExists(npc.repo, artDetails); err != nil {
return err
}
return npc.setPackageInfo()
}
func (npc *NpmPublishCommand) pack() error {
log.Debug("Creating npm package.")
packageFileName, err := npm.Pack(npc.npmArgs, npc.executablePath)
if err != nil {
return err
}
npc.packedFilePath = filepath.Join(npc.workingDirectory, packageFileName)
log.Debug("Created npm package at", npc.packedFilePath)
return nil
}
func (npc *NpmPublishCommand) deploy() error {
log.Debug("Deploying npm package.")
if err := npc.readPackageInfoFromTarball(); err != nil {
return err
}
target := fmt.Sprintf("%s/%s", npc.repo, npc.packageInfo.GetDeployPath())
return npc.doDeploy(target, npc.serverDetails)
}
func (npc *NpmPublishCommand) doDeploy(target string, artDetails *config.ServerDetails) error {
servicesManager, err := utils.CreateServiceManager(artDetails, false)
if err != nil {
return err
}
up := services.UploadParams{}
up.ArtifactoryCommonParams = &specutils.ArtifactoryCommonParams{Pattern: npc.packedFilePath, Target: target}
if npc.collectBuildInfo {
utils.SaveBuildGeneralDetails(npc.buildConfiguration.BuildName, npc.buildConfiguration.BuildNumber, npc.buildConfiguration.Project)
up.BuildProps, err = utils.CreateBuildProperties(npc.buildConfiguration.BuildName, npc.buildConfiguration.BuildNumber, npc.buildConfiguration.Project)
if err != nil {
return err
}
}
var totalFailed int
if npc.collectBuildInfo {
summary, err := servicesManager.UploadFilesWithSummary(up)
if err != nil {
return err
}
summary.TransferDetailsReader.Close()
npc.artifactsDetailsReader = summary.ArtifactsDetailsReader
totalFailed = summary.TotalFailed
} else {
_, totalFailed, err = servicesManager.UploadFiles(up)
if err != nil {
return err
}
}
// We deploying only one Artifact which have to be deployed, in case of failure we should fail
if totalFailed > 0 {
return errorutils.CheckError(errors.New("Failed to upload the npm package to Artifactory. See Artifactory logs for more details."))
}
return nil
}
func (npc *NpmPublishCommand) saveArtifactData() error {
log.Debug("Saving npm package artifact build info data.")
buildArtifacts, err := specutils.ConvertArtifactsDetailsToBuildInfoArtifacts(npc.artifactsDetailsReader)
if err != nil {
return err
}
npc.artifactsDetailsReader.Close()
populateFunc := func(partial *buildinfo.Partial) {
partial.Artifacts = buildArtifacts
if npc.buildConfiguration.Module == "" {
npc.buildConfiguration.Module = npc.packageInfo.BuildInfoModuleId()
}
partial.ModuleId = npc.buildConfiguration.Module
partial.ModuleType = buildinfo.Npm
}
return utils.SavePartialBuildInfo(npc.buildConfiguration.BuildName, npc.buildConfiguration.BuildNumber, npc.buildConfiguration.Project, populateFunc)
}
func (npc *NpmPublishCommand) setPublishPath() error {
log.Debug("Reading Package Json.")
npc.publishPath = npc.workingDirectory
if len(npc.npmArgs) > 0 && !strings.HasPrefix(strings.TrimSpace(npc.npmArgs[0]), "-") {
path := strings.TrimSpace(npc.npmArgs[0])
path = clientutils.ReplaceTildeWithUserHome(path)
if filepath.IsAbs(path) {
npc.publishPath = path
} else {
npc.publishPath = filepath.Join(npc.workingDirectory, path)
}
}
return nil
}
func (npc *NpmPublishCommand) setPackageInfo() error {
log.Debug("Setting Package Info.")
fileInfo, err := os.Stat(npc.publishPath)
if err != nil {
return errorutils.CheckError(err)
}
if fileInfo.IsDir() {
npc.packageInfo, err = npm.ReadPackageInfoFromPackageJson(npc.publishPath)
return err
}
log.Debug("The provided path is not a directory, we assume this is a compressed npm package")
npc.tarballProvided = true
npc.packedFilePath = npc.publishPath
return npc.readPackageInfoFromTarball()
}
func (npc *NpmPublishCommand) readPackageInfoFromTarball() error {
log.Debug("Extracting info from npm package:", npc.packedFilePath)
tarball, err := os.Open(npc.packedFilePath)
if err != nil {
return errorutils.CheckError(err)
}
defer tarball.Close()
gZipReader, err := gzip.NewReader(tarball)
if err != nil {
return errorutils.CheckError(err)
}
tarReader := tar.NewReader(gZipReader)
for {
hdr, err := tarReader.Next()
if err != nil {
if err == io.EOF {
return errorutils.CheckError(errors.New("Could not find 'package.json' in the compressed npm package: " + npc.packedFilePath))
}
return errorutils.CheckError(err)
}
parent := filepath.Dir(hdr.Name)
if filepath.Base(parent) == "package" && strings.HasSuffix(hdr.Name, "package.json") {
packageJson, err := ioutil.ReadAll(tarReader)
if err != nil {
return errorutils.CheckError(err)
}
npc.packageInfo, err = npm.ReadPackageInfo(packageJson)
return err
}
}
}
func deleteCreatedTarballAndError(packedFilePath string, currentError error) error {
if err := deleteCreatedTarball(packedFilePath); err != nil {
errorText := fmt.Sprintf("Two errors occurred: \n%s \n%s", currentError, err)
return errorutils.CheckError(errors.New(errorText))
}
return currentError
}
func deleteCreatedTarball(packedFilePath string) error {
if err := os.Remove(packedFilePath); err != nil {
return errorutils.CheckError(err)
}
log.Debug("Successfully deleted the created npm package:", packedFilePath)
return nil
}