github.com/osievert/jfrog-cli-core@v1.2.7/artifactory/commands/generic/props.go (about)

     1  package generic
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/jfrog/jfrog-cli-core/artifactory/spec"
     7  	"github.com/jfrog/jfrog-cli-core/utils/config"
     8  	"github.com/jfrog/jfrog-cli-core/utils/coreutils"
     9  	"github.com/jfrog/jfrog-client-go/artifactory"
    10  	"github.com/jfrog/jfrog-client-go/artifactory/services"
    11  	clientConfig "github.com/jfrog/jfrog-client-go/config"
    12  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    13  	"github.com/jfrog/jfrog-client-go/utils/io/content"
    14  	"github.com/jfrog/jfrog-client-go/utils/log"
    15  )
    16  
    17  type PropsCommand struct {
    18  	props   string
    19  	threads int
    20  	GenericCommand
    21  }
    22  
    23  func NewPropsCommand() *PropsCommand {
    24  	return &PropsCommand{GenericCommand: *NewGenericCommand()}
    25  }
    26  
    27  func (pc *PropsCommand) Threads() int {
    28  	return pc.threads
    29  }
    30  
    31  func (pc *PropsCommand) SetThreads(threads int) *PropsCommand {
    32  	pc.threads = threads
    33  	return pc
    34  }
    35  
    36  func (pc *PropsCommand) Props() string {
    37  	return pc.props
    38  }
    39  
    40  func (pc *PropsCommand) SetProps(props string) *PropsCommand {
    41  	pc.props = props
    42  	return pc
    43  }
    44  
    45  func createPropsServiceManager(threads int, artDetails *config.ArtifactoryDetails) (artifactory.ArtifactoryServicesManager, error) {
    46  	certsPath, err := coreutils.GetJfrogCertsDir()
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	artAuth, err := artDetails.CreateArtAuthConfig()
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	serviceConfig, err := clientConfig.NewConfigBuilder().
    55  		SetServiceDetails(artAuth).
    56  		SetCertificatesPath(certsPath).
    57  		SetInsecureTls(artDetails.InsecureTls).
    58  		SetThreads(threads).
    59  		Build()
    60  
    61  	return artifactory.New(&artAuth, serviceConfig)
    62  }
    63  
    64  func searchItems(spec *spec.SpecFiles, servicesManager artifactory.ArtifactoryServicesManager) (resultReader *content.ContentReader, err error) {
    65  	var errorOccurred = false
    66  	temp := []*content.ContentReader{}
    67  	defer func() {
    68  		for _, reader := range temp {
    69  			reader.Close()
    70  		}
    71  	}()
    72  	writer, err := content.NewContentWriter("results", true, false)
    73  	if err != nil {
    74  		return
    75  	}
    76  	defer writer.Close()
    77  	for i := 0; i < len(spec.Files); i++ {
    78  		searchParams, err := getSearchParamsForProps(spec.Get(i))
    79  		if err != nil {
    80  			errorOccurred = true
    81  			log.Error(err)
    82  			continue
    83  		}
    84  		reader, err := servicesManager.SearchFiles(searchParams)
    85  		if err != nil {
    86  			errorOccurred = true
    87  			log.Error(err)
    88  			continue
    89  		}
    90  		temp = append(temp, reader)
    91  	}
    92  	resultReader, err = content.MergeReaders(temp, content.DefaultKey)
    93  	if err != nil {
    94  		return
    95  	}
    96  	if errorOccurred {
    97  		err = errorutils.CheckError(errors.New("Operation finished with errors, please review the logs."))
    98  	}
    99  	return
   100  }
   101  
   102  func GetPropsParams(reader *content.ContentReader, properties string) (propsParams services.PropsParams) {
   103  	propsParams = services.NewPropsParams()
   104  	propsParams.Reader = reader
   105  	propsParams.Props = properties
   106  	return
   107  }
   108  
   109  func getSearchParamsForProps(f *spec.File) (searchParams services.SearchParams, err error) {
   110  	searchParams = services.NewSearchParams()
   111  	searchParams.ArtifactoryCommonParams = f.ToArtifactoryCommonParams()
   112  	searchParams.Recursive, err = f.IsRecursive(true)
   113  	if err != nil {
   114  		return
   115  	}
   116  	searchParams.ExcludeArtifacts, err = f.IsExcludeArtifacts(false)
   117  	if err != nil {
   118  		return
   119  	}
   120  	searchParams.IncludeDeps, err = f.IsIncludeDeps(false)
   121  	if err != nil {
   122  		return
   123  	}
   124  	searchParams.IncludeDirs, err = f.IsIncludeDirs(false)
   125  	if err != nil {
   126  		return
   127  	}
   128  	return
   129  }