github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/utils/conditionaluploadutils.go (about)

     1  package utils
     2  
     3  import (
     4  	"github.com/jfrog/jfrog-cli-core/v2/common/format"
     5  	"strings"
     6  
     7  	"github.com/jfrog/jfrog-cli-core/v2/common/spec"
     8  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
     9  	clientutils "github.com/jfrog/jfrog-client-go/utils"
    10  )
    11  
    12  type ConditionalUploadScanFuncType func(serverDetails *config.ServerDetails, fileSpec *spec.SpecFiles, threads int, scanOutputFormat format.OutputFormat) error
    13  
    14  // Function to run as a condition to upload. If not overridden, the default scan function is used.
    15  var ConditionalUploadScanFunc ConditionalUploadScanFuncType = nil
    16  
    17  // ScanDeployableArtifacts scans all files founds in the given parsed deployableArtifacts results.
    18  // If the scan passes, the function returns two file-specs ready for upload. The first one contains all the binaries
    19  // and the second all the "pom.xml"s.
    20  // If one of the file's scan failed both of the return values will be nil.
    21  func ScanDeployableArtifacts(deployableArtifacts *Result, serverDetails *config.ServerDetails, threads int, format format.OutputFormat) (*spec.SpecFiles, *spec.SpecFiles, error) {
    22  	binariesSpecFile := &spec.SpecFiles{}
    23  	pomSpecFile := &spec.SpecFiles{}
    24  	deployableArtifacts.Reader().Reset()
    25  	for item := new(clientutils.FileTransferDetails); deployableArtifacts.Reader().NextRecord(item) == nil; item = new(clientutils.FileTransferDetails) {
    26  		file := spec.File{Pattern: item.SourcePath, Target: parseTargetPath(item.TargetPath, serverDetails.ArtifactoryUrl)}
    27  		if strings.HasSuffix(item.SourcePath, "pom.xml") {
    28  			pomSpecFile.Files = append(pomSpecFile.Files, file)
    29  		} else {
    30  			binariesSpecFile.Files = append(binariesSpecFile.Files, file)
    31  		}
    32  	}
    33  	if err := deployableArtifacts.Reader().GetError(); err != nil {
    34  		return nil, nil, err
    35  	}
    36  	// Only non pom.xml should be scanned. If a FailBuildError is returned, skip the deployment.
    37  	err := ConditionalUploadScanFunc(serverDetails, binariesSpecFile, threads, format)
    38  	if err != nil {
    39  		return nil, nil, err
    40  	}
    41  	return binariesSpecFile, pomSpecFile, nil
    42  }
    43  
    44  // Returns the target path inside a given server URL.
    45  func parseTargetPath(target, serverUrl string) string {
    46  	if strings.Contains(target, serverUrl) {
    47  		return target[len(serverUrl):]
    48  	}
    49  	return target
    50  }