github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/generic/search.go (about) 1 package generic 2 3 import ( 4 "github.com/jfrog/jfrog-cli-go/artifactory/spec" 5 "github.com/jfrog/jfrog-cli-go/artifactory/utils" 6 "github.com/jfrog/jfrog-client-go/artifactory/services" 7 clientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils" 8 "github.com/jfrog/jfrog-client-go/utils/errorutils" 9 "github.com/jfrog/jfrog-client-go/utils/log" 10 ) 11 12 type SearchResult struct { 13 Path string `json:"path,omitempty"` 14 Type string `json:"type,omitempty"` 15 Size int64 `json:"size,omitempty"` 16 Created string `json:"created,omitempty"` 17 Modified string `json:"modified,omitempty"` 18 Props map[string][]string `json:"props,omitempty"` 19 } 20 21 type SearchCommand struct { 22 GenericCommand 23 searchResult []SearchResult 24 } 25 26 func NewSearchCommand() *SearchCommand { 27 return &SearchCommand{GenericCommand: *NewGenericCommand()} 28 } 29 30 func (sc *SearchCommand) SearchResult() []SearchResult { 31 return sc.searchResult 32 } 33 34 func (sc *SearchCommand) SearchResultNoDate() []SearchResult { 35 for i, _ := range sc.SearchResult() { 36 sc.searchResult[i].Created = "" 37 sc.searchResult[i].Modified = "" 38 delete(sc.searchResult[i].Props, "vcs.url") 39 delete(sc.searchResult[i].Props, "vcs.revision") 40 } 41 return sc.searchResult 42 } 43 44 func (sc *SearchCommand) CommandName() string { 45 return "rt_search" 46 } 47 48 func (sc *SearchCommand) Run() error { 49 return sc.Search() 50 } 51 52 func (sc *SearchCommand) Search() error { 53 // Service Manager 54 rtDetails, err := sc.RtDetails() 55 if errorutils.CheckError(err) != nil { 56 return err 57 } 58 servicesManager, err := utils.CreateServiceManager(rtDetails, false) 59 if err != nil { 60 return err 61 } 62 63 // Search Loop 64 log.Info("Searching artifacts...") 65 var resultItems []clientutils.ResultItem 66 for i := 0; i < len(sc.Spec().Files); i++ { 67 68 searchParams, err := GetSearchParams(sc.Spec().Get(i)) 69 if err != nil { 70 log.Error(err) 71 return err 72 } 73 74 currentResultItems, err := servicesManager.SearchFiles(searchParams) 75 if err != nil { 76 return err 77 } 78 resultItems = append(resultItems, currentResultItems...) 79 } 80 81 sc.searchResult = aqlResultToSearchResult(resultItems) 82 clientutils.LogSearchResults(len(resultItems)) 83 return err 84 } 85 86 func aqlResultToSearchResult(aqlResult []clientutils.ResultItem) (result []SearchResult) { 87 result = make([]SearchResult, len(aqlResult)) 88 for i, v := range aqlResult { 89 tempResult := new(SearchResult) 90 tempResult.Path = v.Repo + "/" 91 if v.Path != "." { 92 tempResult.Path += v.Path + "/" 93 } 94 if v.Name != "." { 95 tempResult.Path += v.Name 96 } 97 tempResult.Type = v.Type 98 tempResult.Size = v.Size 99 tempResult.Created = v.Created 100 tempResult.Modified = v.Modified 101 tempResult.Props = make(map[string][]string, len(v.Properties)) 102 for _, prop := range v.Properties { 103 tempResult.Props[prop.Key] = append(tempResult.Props[prop.Key], prop.Value) 104 } 105 result[i] = *tempResult 106 } 107 return 108 } 109 110 func GetSearchParams(f *spec.File) (searchParams services.SearchParams, err error) { 111 searchParams = services.NewSearchParams() 112 searchParams.ArtifactoryCommonParams = f.ToArtifactoryCommonParams() 113 searchParams.Recursive, err = f.IsRecursive(true) 114 if err != nil { 115 return 116 } 117 118 searchParams.IncludeDirs, err = f.IsIncludeDirs(false) 119 return 120 }