Exemplo n.º 1
0
// We have simple dir delete when:
//    1) The deleteFile is a dir path, ends with "/"
//    2) The deleteFile doest contains wildcards
//    3) The user hasn't sent any props
//    4) The delete is recursive
func isSimpleDirectoryDelete(deleteFile *utils.Files) (bool, error) {
	isRecursive, err := cliutils.StringToBool(deleteFile.Recursive, true)
	if err != nil {
		return false, err
	}
	return utils.IsSimpleDirectoryPath(deleteFile.Pattern) && isRecursive && deleteFile.Props == "", nil
}
Exemplo n.º 2
0
func GetPathsToDelete(deleteSpec *utils.SpecFiles, flags *DeleteFlags) (resultItems []utils.AqlSearchResultItem, err error) {
	log.Info("Searching artifacts...")
	for i := 0; i < len(deleteSpec.Files); i++ {
		var isDirectoryDeleteBool bool
		isSimpleDirectoryDeleteBool, e := isSimpleDirectoryDelete(deleteSpec.Get(i))
		if e != nil {
			err = e
			return
		}
		if !isSimpleDirectoryDeleteBool {
			isDirectoryDeleteBool, e = isDirectoryDelete(deleteSpec.Get(i))
			if e != nil {
				err = e
				return
			}
		}
		switch {
		case deleteSpec.Get(i).GetSpecType() == utils.AQL:
			resultItemsTemp, e := utils.AqlSearchBySpec(deleteSpec.Get(i).Aql, flags)
			if e != nil {
				err = e
				return
			}
			resultItems = append(resultItems, resultItemsTemp...)

		case isSimpleDirectoryDeleteBool:
			simplePathItem := utils.AqlSearchResultItem{Path: deleteSpec.Get(i).Pattern}
			resultItems = append(resultItems, []utils.AqlSearchResultItem{simplePathItem}...)

		case isDirectoryDeleteBool:
			tempResultItems, e := utils.AqlSearchDefaultReturnFields(deleteSpec.Get(i).Pattern, true, "", flags)
			if e != nil {
				err = e
				return
			}
			paths, e := getDirsForDeleteFromFilesPaths(deleteSpec.Get(i).Pattern, tempResultItems)
			if e != nil {
				err = e
				return
			}
			resultItems = append(resultItems, paths...)
		default:
			isRecursive, e := cliutils.StringToBool(deleteSpec.Get(i).Recursive, true)
			if e != nil {
				err = e
				return
			}
			tempResultItems, e := utils.AqlSearchDefaultReturnFields(deleteSpec.Get(i).Pattern,
				isRecursive, deleteSpec.Get(i).Props, flags)
			if e != nil {
				err = e
				return
			}
			resultItems = append(resultItems, tempResultItems...)
		}
	}
	utils.LogSearchResults(len(resultItems))
	return
}
Exemplo n.º 3
0
func collectWildcardDependecies(fileSpec *utils.Files, flags *DownloadFlags) ([]DownloadData, error) {
	isRecursive, err := cliutils.StringToBool(fileSpec.Recursive, true)
	if err != nil {
		return nil, err
	}

	resultItems, err := utils.AqlSearchDefaultReturnFields(fileSpec.Pattern, isRecursive, fileSpec.Props, flags)
	if err != nil {
		return nil, err
	}

	return createDownloadDataList(resultItems, fileSpec)
}
Exemplo n.º 4
0
func moveWildcard(fileSpec *Files, flags *MoveFlags, moveType MoveType) (successCount, failedCount int, err error) {
	isRecursive, err := cliutils.StringToBool(fileSpec.Recursive, true)
	if err != nil {
		return
	}
	log.Info("Searching artifacts...")
	resultItems, err := AqlSearchDefaultReturnFields(fileSpec.Pattern, isRecursive, fileSpec.Props, flags)
	if err != nil {
		return
	}
	LogSearchResults(len(resultItems))
	regexpPath := cliutils.PathToRegExp(fileSpec.Pattern)
	successCount, failedCount, err = moveFiles(regexpPath, resultItems, fileSpec, flags, moveType)
	return
}
Exemplo n.º 5
0
func createDownloadDataList(items []utils.AqlSearchResultItem, fileSpec *utils.Files) ([]DownloadData, error) {
	var dependencies []DownloadData
	for _, v := range items {
		flat, err := cliutils.StringToBool(fileSpec.Flat, false)
		if err != nil {
			return dependencies, err
		}
		dependencies = append(dependencies, DownloadData{
			Dependency:   v,
			DownloadPath: fileSpec.Pattern,
			Target:       fileSpec.Target,
			Flat:         flat,
		})
	}
	return dependencies, nil
}
Exemplo n.º 6
0
func Search(searchSpec *utils.SpecFiles, flags *SearchFlags) (result []SearchResult, err error) {
	err = utils.PreCommandSetup(flags)
	if err != nil {
		return
	}

	var resultItems []utils.AqlSearchResultItem
	var itemsFound []utils.AqlSearchResultItem

	log.Info("Searching artifacts...")
	for i := 0; i < len(searchSpec.Files); i++ {
		switch searchSpec.Get(i).GetSpecType() {
		case utils.WILDCARD, utils.SIMPLE:
			isRecursive, e := cliutils.StringToBool(searchSpec.Get(i).Recursive, true)
			if e != nil {
				err = e
				return
			}
			itemsFound, e = utils.AqlSearchDefaultReturnFields(searchSpec.Get(i).Pattern,
				isRecursive, searchSpec.Get(i).Props, flags)
			if e != nil {
				err = e
				return
			}
			resultItems = append(resultItems, itemsFound...)
		case utils.AQL:
			itemsFound, err = utils.AqlSearchBySpec(searchSpec.Get(i).Aql, flags)
			if err != nil {
				return
			}
			resultItems = append(resultItems, itemsFound...)
		}
	}

	result = aqlResultToSearchResult(resultItems)
	utils.LogSearchResults(len(resultItems))
	return
}
Exemplo n.º 7
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
}