func getPackageFiles(packageName string) []tests.PackageSearchResultItem { apiUrl := bintrayConfig.ApiUrl + path.Join("packages", bintrayConfig.User, tests.BintrayRepo1, packageName, "files?include_unpublished=1") clientDetails := ioutils.HttpClientDetails{ User: bintrayConfig.User, Password: bintrayConfig.Key, Headers: map[string]string{"Content-Type": "application/json"}} resp, body, _, err := ioutils.SendGet(apiUrl, true, clientDetails) if cliutils.CheckError(err) != nil { os.Exit(1) } if resp.StatusCode != 200 { log.Error(resp.Status) log.Error(string(body)) os.Exit(1) } var result []tests.PackageSearchResultItem err = json.Unmarshal(body, &result) if err != nil { log.Error(err) os.Exit(1) } return result }
func deleteBuild(buildName string) { artHttpDetails := utils.GetArtifactoryHttpClientDetails(artifactoryDetails) resp, body, err := ioutils.SendDelete(*tests.RtUrl+"api/build/"+buildName+"?deleteAll=1", nil, artHttpDetails) if err != nil { log.Error(err) } if resp.StatusCode != 200 { log.Error(resp.Status) log.Error(string(body)) } }
func CheckErrorWithMessage(err error, message string) error { if err != nil { log.Error(message) err = CheckError(err) } return err }
func moveFile(sourcePath, destPath string, flags *MoveFlags, moveType MoveType) (bool, error) { message := moveMsgs[moveType].MovingMsg + " artifact: " + sourcePath + " to: " + destPath if flags.DryRun == true { log.Info("[Dry run] ", message) return true, nil } log.Info(message) moveUrl := flags.ArtDetails.Url restApi := "api/" + string(moveType) + "/" + sourcePath requestFullUrl, err := BuildArtifactoryUrl(moveUrl, restApi, map[string]string{"to": destPath}) if err != nil { return false, err } httpClientsDetails := GetArtifactoryHttpClientDetails(flags.ArtDetails) resp, body, err := ioutils.SendPost(requestFullUrl, nil, httpClientsDetails) if err != nil { return false, err } if resp.StatusCode != 200 { log.Error("Artifactory response: " + resp.Status + "\n" + cliutils.IndentJson(body)) } log.Debug("Artifactory response:", resp.Status) return resp.StatusCode == 200, nil }
func deleteBintrayRepo() { apiUrl := bintrayConfig.ApiUrl + path.Join("repos", bintrayConfig.User, tests.BintrayRepo1) clientDetails := ioutils.HttpClientDetails{ User: bintrayConfig.User, Password: bintrayConfig.Key, Headers: map[string]string{"Content-Type": "application/json"}} resp, body, err := ioutils.SendDelete(apiUrl, nil, clientDetails) if cliutils.CheckError(err) != nil { os.Exit(1) } if resp.StatusCode != 200 && resp.StatusCode != 404 { log.Error(resp.Status) log.Error(string(body)) os.Exit(1) } }
func CleanFileSystem() { isExist, err := ioutils.IsDirExists(Out) if err != nil { log.Error(err) } if isExist { os.RemoveAll(Out) } }
func uploadWildcard(artifacts []UploadData, flags *UploadFlags) (buildInfoArtifacts [][]utils.ArtifactBuildInfo, totalUploaded, totalFailed int, err error) { minChecksumDeploySize, e := getMinChecksumDeploySize() if e != nil { err = e return } size := len(artifacts) var wg sync.WaitGroup // Create an array of integers, to store the total file that were uploaded successfully. // Each array item is used by a single thread. uploadCount := make([]int, flags.Threads, flags.Threads) buildInfoArtifacts = make([][]utils.ArtifactBuildInfo, flags.Threads) for i := 0; i < flags.Threads; i++ { wg.Add(1) go func(threadId int) { logMsgPrefix := cliutils.GetLogMsgPrefix(threadId, flags.DryRun) for j := threadId; j < size && err == nil; j += flags.Threads { var e error var uploaded bool var target string var buildInfoArtifact utils.ArtifactBuildInfo target, e = utils.BuildArtifactoryUrl(flags.ArtDetails.Url, artifacts[j].Artifact.TargetPath, make(map[string]string)) if e != nil { err = e break } buildInfoArtifact, uploaded, e = uploadFile(artifacts[j].Artifact.LocalPath, target, artifacts[j].Props, flags, minChecksumDeploySize, logMsgPrefix) if e != nil { err = e break } if uploaded { uploadCount[threadId]++ buildInfoArtifacts[threadId] = append(buildInfoArtifacts[threadId], buildInfoArtifact) } } wg.Done() }(i) } wg.Wait() if err != nil { return } totalUploaded = 0 for _, i := range uploadCount { totalUploaded += i } log.Info("Uploaded", strconv.Itoa(totalUploaded), "artifacts.") totalFailed = size - totalUploaded if totalFailed > 0 { log.Error("Failed uploading", strconv.Itoa(totalFailed), "artifacts.") } return }
func handleError(err error, exitCode ExitCode) error { if err != nil { if onError == OnErrorPanic { panic(err) } log.Error(err.Error()) } return err }
func createBintrayRepo() { content, err := ioutil.ReadFile(tests.GetTestResourcesPath() + tests.BintrayTestRepositoryConfig) if cliutils.CheckError(err) != nil { os.Exit(1) } apiUrl := bintrayConfig.ApiUrl + path.Join("repos", bintrayConfig.User, tests.BintrayRepo1) clientDetails := ioutils.HttpClientDetails{ User: bintrayConfig.User, Password: bintrayConfig.Key, Headers: map[string]string{"Content-Type": "application/json"}} resp, body, err := ioutils.SendPost(apiUrl, content, clientDetails) if cliutils.CheckError(err) != nil { os.Exit(1) } if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 409 { log.Error(resp.Status) log.Error(string(body)) os.Exit(1) } }
func uploadFiles(artifacts []cliutils.Artifact, baseUrl string, flags *UploadFlags) (totalUploaded, totalFailed int, err error) { size := len(artifacts) var wg sync.WaitGroup // Create an array of integers, to store the total file that were uploaded successfully. // Each array item is used by a single thread. uploadCount := make([]int, flags.Threads, flags.Threads) matrixParams := getMatrixParams(flags) for i := 0; i < flags.Threads; i++ { wg.Add(1) go func(threadId int) { logMsgPrefix := cliutils.GetLogMsgPrefix(threadId, flags.DryRun) for j := threadId; j < size; j += flags.Threads { if err != nil { break } url := baseUrl + artifacts[j].TargetPath + matrixParams if !flags.DryRun { uploaded, e := uploadFile(artifacts[j], url, logMsgPrefix, flags.BintrayDetails) if e != nil { err = e break } if uploaded { uploadCount[threadId]++ } } else { log.Info("[Dry Run] Uploading artifact:", artifacts[j].LocalPath) uploadCount[threadId]++ } } wg.Done() }(i) } wg.Wait() totalUploaded = 0 for _, i := range uploadCount { totalUploaded += i } log.Info("Uploaded", strconv.Itoa(totalUploaded), "artifacts.") totalFailed = size - totalUploaded if totalFailed > 0 { log.Error("Failed uploading", strconv.Itoa(totalFailed), "artifacts.") } return }
func uploadFile(artifact cliutils.Artifact, url, logMsgPrefix string, bintrayDetails *config.BintrayDetails) (bool, error) { log.Info(logMsgPrefix+"Uploading artifact:", artifact.LocalPath) f, err := os.Open(artifact.LocalPath) err = cliutils.CheckError(err) if err != nil { return false, err } defer f.Close() httpClientsDetails := utils.GetBintrayHttpClientDetails(bintrayDetails) resp, body, err := ioutils.UploadFile(f, url, httpClientsDetails) if err != nil { return false, err } log.Debug(logMsgPrefix+"Bintray response:", resp.Status) if resp.StatusCode != 201 && resp.StatusCode != 200 { log.Error(logMsgPrefix + "Bintray response: " + resp.Status + "\n" + cliutils.IndentJson(body)) } return resp.StatusCode == 201 || resp.StatusCode == 200, nil }
func initBintrayCredentials() { if bintrayConfig != nil { return } var err error bintrayConfig, err = config.ReadBintrayConf() if cliutils.CheckError(err) != nil { os.Exit(1) } if *tests.BtUser != "" { bintrayConfig.User = *tests.BtUser } if *tests.BtKey != "" { bintrayConfig.Key = *tests.BtKey } if bintrayConfig.User == "" || bintrayConfig.Key == "" { log.Error("To test Bintray credentials must be configured.") os.Exit(1) } apiUrl := os.Getenv("JFROG_CLI_BINTRAY_API_URL") if apiUrl == "" { apiUrl = "https://bintray.com/api/v1/" } downloadServerUrl := os.Getenv("JFROG_CLI_BINTRAY_DOWNLOAD_URL") if downloadServerUrl == "" { downloadServerUrl = "https://dl.bintray.com/" } apiUrl = cliutils.AddTrailingSlashIfNeeded(apiUrl) downloadServerUrl = cliutils.AddTrailingSlashIfNeeded(downloadServerUrl) bintrayConfig.ApiUrl = apiUrl bintrayConfig.DownloadServerUrl = downloadServerUrl }
func Exit(exitCode ExitCode, msg string) { if msg != "" { log.Error(msg) } os.Exit(exitCode.Code) }