Example #1
0
func prepareLicenseFile(filepath string, overrideFile bool) (err error) {
	if filepath == "" {
		return
	}
	var dir bool
	dir, err = ioutils.IsDir(filepath)
	if err != nil {
		return
	}
	if dir {
		err = cliutils.CheckError(errors.New(filepath + " is a directory."))
		if err != nil {
			return
		}
	}
	var exists bool
	exists, err = ioutils.IsFileExists(filepath)
	if err != nil {
		return
	}
	if !overrideFile && exists {
		err = cliutils.CheckError(errors.New("File already exist, in case you wish to override the file use --override flag"))
		if err != nil {
			return
		}
	}
	_, directory := ioutils.GetFileAndDirFromPath(filepath)
	isPathExists := ioutils.IsPathExists(directory)
	if !isPathExists {
		os.MkdirAll(directory, 0700)
	}
	err = ioutil.WriteFile(filepath, nil, 0777)
	err = cliutils.CheckError(err)
	return
}
Example #2
0
func getSingleFileToUpload(rootPath, targetPath string, flat bool) cliutils.Artifact {
	var uploadPath string
	if !strings.HasSuffix(targetPath, "/") {
		uploadPath = targetPath
	} else {
		if flat {
			uploadPath, _ = ioutils.GetFileAndDirFromPath(rootPath)
			uploadPath = targetPath + uploadPath
		} else {
			uploadPath = targetPath + rootPath
			uploadPath = cliutils.TrimPath(uploadPath)
		}
	}
	return cliutils.Artifact{LocalPath: rootPath, TargetPath: uploadPath}
}
Example #3
0
func getSingleFileToUpload(rootPath, targetPath, debianDefaultPath string, flat bool) cliutils.Artifact {
	var uploadPath string
	rootPathOrig := rootPath
	if targetPath != "" && !strings.HasSuffix(targetPath, "/") {
		rootPath = targetPath
		targetPath = ""
	}
	if flat {
		uploadPath, _ = ioutils.GetFileAndDirFromPath(rootPath)
		uploadPath = targetPath + uploadPath
	} else {
		uploadPath = targetPath + rootPath
		uploadPath = cliutils.TrimPath(uploadPath)
	}
	return cliutils.Artifact{LocalPath: rootPathOrig, TargetPath: uploadPath}
}
Example #4
0
func moveSimple(fileSpec *Files, flags *MoveFlags, moveType MoveType) (successCount, failedCount int, err error) {

	cleanPattern := cliutils.StripChars(fileSpec.Pattern, "()")
	patternFileName, _ := ioutils.GetFileAndDirFromPath(fileSpec.Pattern)

	regexpPattern := cliutils.PathToRegExp(fileSpec.Pattern)
	placeHolderTarget, err := cliutils.ReformatRegexp(regexpPattern, cleanPattern, fileSpec.Target)
	if err != nil {
		return
	}

	if strings.HasSuffix(placeHolderTarget, "/") {
		placeHolderTarget += patternFileName
	}
	success, err := moveFile(cleanPattern, placeHolderTarget, flags, moveType)
	successCount = cliutils.Bool2Int(success)
	failedCount = cliutils.Bool2Int(!success)
	return
}
Example #5
0
func moveFiles(regexpPath string, resultItems []AqlSearchResultItem, fileSpec *Files, flags *MoveFlags, moveType MoveType) (successCount, failedCount int, err error) {
	successCount = 0
	failedCount = 0

	for _, v := range resultItems {
		destPathLocal := fileSpec.Target
		isFlat, e := cliutils.StringToBool(fileSpec.Flat, false)
		if e != nil {
			err = e
			return
		}
		if !isFlat {
			if strings.Contains(destPathLocal, "/") {
				file, dir := ioutils.GetFileAndDirFromPath(destPathLocal)
				destPathLocal = cliutils.TrimPath(dir + "/" + v.Path + "/" + file)
			} else {
				destPathLocal = cliutils.TrimPath(destPathLocal + "/" + v.Path + "/")
			}
		}
		destFile, e := cliutils.ReformatRegexp(regexpPath, v.GetFullUrl(), destPathLocal)
		if e != nil {
			err = e
			return
		}
		if strings.HasSuffix(destFile, "/") {
			destFile += v.Name
		}
		success, e := moveFile(v.GetFullUrl(), destFile, flags, moveType)
		if e != nil {
			err = e
			return
		}

		successCount += cliutils.Bool2Int(success)
		failedCount += cliutils.Bool2Int(!success)
	}
	return
}
Example #6
0
func DownloadBintrayFile(bintrayDetails *config.BintrayDetails, pathDetails *PathDetails, targetPath string,
	flags *DownloadFlags, logMsgPrefix string) (err error) {

	cleanPath := strings.Replace(pathDetails.Path, "(", "", -1)
	cleanPath = strings.Replace(cleanPath, ")", "", -1)
	downloadPath := path.Join(pathDetails.Subject, pathDetails.Repo, cleanPath)

	fileName, filePath := ioutils.GetFileAndDirFromPath(cleanPath)

	url := bintrayDetails.DownloadServerUrl + downloadPath
	if flags.IncludeUnpublished {
		url += "?include_unpublished=1"
	}
	log.Info(logMsgPrefix+"Downloading", downloadPath)

	httpClientsDetails := GetBintrayHttpClientDetails(bintrayDetails)
	var details *ioutils.FileDetails
	details, err = ioutils.GetRemoteFileDetails(url, httpClientsDetails)
	if err != nil {
		err = cliutils.CheckError(errors.New("Bintray " + err.Error()))
		if err != nil {
			return
		}
	}

	regexpPattern := cliutils.PathToRegExp(pathDetails.Path)
	placeHolderTarget, e := cliutils.ReformatRegexp(regexpPattern, cleanPath, targetPath)
	if e != nil {
		err = e
		return
	}

	localPath, localFileName := ioutils.GetLocalPathAndFile(fileName, filePath, placeHolderTarget, flags.Flat)
	var shouldDownload bool
	shouldDownload, err = shouldDownloadFile(path.Join(localPath, localFileName), details)
	if err != nil {
		return
	}
	if !shouldDownload {
		log.Info(logMsgPrefix, "File already exists locally.")
		return
	}

	// Check if the file should be downloaded concurrently.
	if flags.SplitCount == 0 || flags.MinSplitSize < 0 || flags.MinSplitSize*1000 > details.Size {
		// File should not be downloaded concurrently. Download it as one block.
		resp, err := ioutils.DownloadFile(url, localPath, localFileName, httpClientsDetails)
		if err != nil {
			return err
		}
		log.Debug(logMsgPrefix, "Bintray response:", resp.Status)
	} else {
		// We should attempt to download the file concurrently, but only if it is provided through the DSN.
		// To check if the file is provided through the DSN, we first attempt to download the file
		// with 'follow redirect' disabled.

		var resp *http.Response
		var redirectUrl string
		resp, redirectUrl, err =
			ioutils.DownloadFileNoRedirect(url, localPath, localFileName, httpClientsDetails)
		// There are two options now. Either the file has just been downloaded as one block, or
		// we got a redirect to DSN download URL. In case of the later, we should download the file
		// concurrently from the DSN URL.
		// 'err' is not nil in case 'redirectUrl' was returned.
		if redirectUrl != "" {
			concurrentDownloadFlags := ioutils.ConcurrentDownloadFlags{
				DownloadPath: redirectUrl,
				FileName:     localFileName,
				LocalPath:    localPath,
				FileSize:     details.Size,
				SplitCount:   flags.SplitCount,
				Flat:         flags.Flat}

			ioutils.DownloadFileConcurrently(concurrentDownloadFlags, "", httpClientsDetails)
		} else {
			err = cliutils.CheckError(err)
			if err != nil {
				return
			}
			log.Info(logMsgPrefix, "Bintray response:", resp.Status)
		}
	}
	return
}
Example #7
0
func getFilesToUpload(localPath, targetPath, packageName string, flags *UploadFlags) ([]cliutils.Artifact, error) {
	var debianDefaultPath string
	if targetPath == "" && flags.Deb != "" {
		debianDefaultPath = getDebianDefaultPath(flags.Deb, packageName)
	}

	rootPath := cliutils.GetRootPathForUpload(localPath, flags.UseRegExp)
	if !ioutils.IsPathExists(rootPath) {
		err := cliutils.CheckError(errors.New("Path does not exist: " + rootPath))
		if err != nil {
			return nil, err
		}
	}
	localPath = cliutils.PrepareLocalPathForUpload(localPath, flags.UseRegExp)

	artifacts := []cliutils.Artifact{}
	// If the path is a single file then return it
	dir, err := ioutils.IsDir(rootPath)
	if err != nil {
		return nil, err
	}

	if !dir {
		artifact := getSingleFileToUpload(rootPath, targetPath, debianDefaultPath, flags.Flat)
		return append(artifacts, artifact), nil
	}

	r, err := regexp.Compile(localPath)
	err = cliutils.CheckError(err)
	if err != nil {
		return nil, err
	}

	spinner := cliutils.NewSpinner("[Info] Collecting files for upload:", time.Second)
	spinner.Start()
	var paths []string
	if flags.Recursive {
		paths, err = ioutils.ListFilesRecursive(rootPath)
	} else {
		paths, err = ioutils.ListFiles(rootPath)
	}
	if err != nil {
		return nil, err
	}

	for _, path := range paths {
		dir, err := ioutils.IsDir(path)
		if err != nil {
			return nil, err
		}
		if dir {
			continue
		}

		groups := r.FindStringSubmatch(path)
		size := len(groups)
		target := targetPath

		if size > 0 {
			for i := 1; i < size; i++ {
				group := strings.Replace(groups[i], "\\", "/", -1)
				target = strings.Replace(target, "{"+strconv.Itoa(i)+"}", group, -1)
			}

			if target == "" || strings.HasSuffix(target, "/") {
				if target == "" {
					target = debianDefaultPath
				}
				if flags.Flat {
					fileName, _ := ioutils.GetFileAndDirFromPath(path)
					target += fileName
				} else {
					uploadPath := cliutils.TrimPath(path)
					target += uploadPath
				}
			}

			artifacts = append(artifacts, cliutils.Artifact{path, target})
		}
	}
	spinner.Stop()
	return artifacts, nil
}