github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/generic/props.go (about)

     1  package generic
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/jfrog/jfrog-cli-go/artifactory/spec"
     7  	"github.com/jfrog/jfrog-cli-go/utils/cliutils"
     8  	"github.com/jfrog/jfrog-cli-go/utils/config"
     9  	"github.com/jfrog/jfrog-client-go/artifactory"
    10  	"github.com/jfrog/jfrog-client-go/artifactory/services"
    11  	clientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
    12  	clientConfig "github.com/jfrog/jfrog-client-go/config"
    13  	"github.com/jfrog/jfrog-client-go/utils/log"
    14  )
    15  
    16  type PropsCommand struct {
    17  	props   string
    18  	threads int
    19  	GenericCommand
    20  }
    21  
    22  func NewPropsCommand() *PropsCommand {
    23  	return &PropsCommand{GenericCommand: *NewGenericCommand()}
    24  }
    25  
    26  func (pc *PropsCommand) Threads() int {
    27  	return pc.threads
    28  }
    29  
    30  func (pc *PropsCommand) SetThreads(threads int) *PropsCommand {
    31  	pc.threads = threads
    32  	return pc
    33  }
    34  
    35  func (pc *PropsCommand) Props() string {
    36  	return pc.props
    37  }
    38  
    39  func (pc *PropsCommand) SetProps(props string) *PropsCommand {
    40  	pc.props = props
    41  	return pc
    42  }
    43  
    44  func createPropsServiceManager(threads int, artDetails *config.ArtifactoryDetails) (*artifactory.ArtifactoryServicesManager, error) {
    45  	certPath, err := cliutils.GetJfrogSecurityDir()
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	artAuth, err := artDetails.CreateArtAuthConfig()
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	serviceConfig, err := clientConfig.NewConfigBuilder().
    54  		SetArtDetails(artAuth).
    55  		SetCertificatesPath(certPath).
    56  		SetInsecureTls(artDetails.InsecureTls).
    57  		SetThreads(threads).
    58  		Build()
    59  
    60  	return artifactory.New(&artAuth, serviceConfig)
    61  }
    62  
    63  func searchItems(spec *spec.SpecFiles, servicesManager *artifactory.ArtifactoryServicesManager) (resultItems []clientutils.ResultItem, err error) {
    64  	var errorOccurred = false
    65  	for i := 0; i < len(spec.Files); i++ {
    66  		searchParams, err := getSearchParamsForProps(spec.Get(i))
    67  		if err != nil {
    68  			errorOccurred = true
    69  			log.Error(err)
    70  			continue
    71  		}
    72  
    73  		currentResultItems, err := servicesManager.SearchFiles(searchParams)
    74  		if err != nil {
    75  			errorOccurred = true
    76  			log.Error(err)
    77  			continue
    78  		}
    79  		resultItems = append(resultItems, currentResultItems...)
    80  	}
    81  	if errorOccurred {
    82  		err = errors.New("Operation finished with errors, please review the logs.")
    83  	}
    84  	return
    85  }
    86  
    87  func GetPropsParams(resultItems []clientutils.ResultItem, properties string) (propsParams services.PropsParams) {
    88  	propsParams = services.NewPropsParams()
    89  	propsParams.Items = resultItems
    90  	propsParams.Props = properties
    91  	return
    92  }
    93  
    94  func getSearchParamsForProps(f *spec.File) (searchParams services.SearchParams, err error) {
    95  	searchParams = services.NewSearchParams()
    96  	searchParams.ArtifactoryCommonParams = f.ToArtifactoryCommonParams()
    97  	searchParams.Recursive, err = f.IsRecursive(true)
    98  	if err != nil {
    99  		return
   100  	}
   101  
   102  	searchParams.IncludeDirs, err = f.IsIncludeDirs(false)
   103  	if err != nil {
   104  		return
   105  	}
   106  	return
   107  }