zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/cli/client/search_sub_cmd.go (about) 1 //go:build search 2 // +build search 3 4 package client 5 6 import ( 7 "fmt" 8 9 "github.com/spf13/cobra" 10 11 zerr "zotregistry.dev/zot/errors" 12 zcommon "zotregistry.dev/zot/pkg/common" 13 ) 14 15 func NewSearchSubjectCommand(searchService SearchService) *cobra.Command { 16 imageListSortFlag := ImageListSortFlag(SortByAlphabeticAsc) 17 18 cmd := &cobra.Command{ 19 Use: "subject [repo:tag]|[repo@digest]", 20 Short: "List all referrers for this subject.", 21 Long: `List all referrers for this subject. The subject can be specified by tag(repo:tag) or by digest" + 22 "(repo@digest)`, 23 Example: `# For referrers search specify the referred subject using it's full digest or tag: 24 zli search subject "repo@sha256:f9a0981..." 25 zli search subject "repo:tag"`, 26 Args: OneImageWithRefArg, 27 RunE: func(cmd *cobra.Command, args []string) error { 28 searchConfig, err := GetSearchConfigFromFlags(cmd, searchService) 29 if err != nil { 30 return err 31 } 32 33 if err := CheckExtEndPointQuery(searchConfig, ReferrersQuery()); err == nil { 34 return SearchReferrersGQL(searchConfig, args[0]) 35 } else { 36 return SearchReferrers(searchConfig, args[0]) 37 } 38 }, 39 } 40 41 cmd.Flags().Var(&imageListSortFlag, SortByFlag, 42 fmt.Sprintf("Options for sorting the output: [%s]", ImageListSortOptionsStr())) 43 44 return cmd 45 } 46 47 func NewSearchQueryCommand(searchService SearchService) *cobra.Command { 48 imageSearchSortFlag := ImageSearchSortFlag(SortByRelevance) 49 50 cmd := &cobra.Command{ 51 Use: "query [repo]|[repo:tag]", 52 Short: "Fuzzy search for repos and their tags.", 53 Long: "Fuzzy search for repos and their tags.", 54 Example: `# For repo search specify a substring of the repo name without the tag 55 zli search query "test/repo" 56 57 # For image search specify the full repo name followed by the tag or a prefix of the tag. 58 zli search query "test/repo:2.1."`, 59 Args: cobra.ExactArgs(1), 60 RunE: func(cmd *cobra.Command, args []string) error { 61 searchConfig, err := GetSearchConfigFromFlags(cmd, searchService) 62 if err != nil { 63 return err 64 } 65 66 // special format for searching all images and tags 67 if args[0] == ":" { 68 err := CheckExtEndPointQuery(searchConfig, GlobalSearchQuery()) 69 if err != nil { 70 return fmt.Errorf("%w: '%s'", err, ImageListQuery().Name) 71 } 72 73 return SearchAllImagesGQL(searchConfig) 74 } 75 76 if err := CheckExtEndPointQuery(searchConfig, GlobalSearchQuery()); err != nil { 77 return fmt.Errorf("%w: '%s'", err, GlobalSearchQuery().Name) 78 } 79 80 return GlobalSearchGQL(searchConfig, args[0]) 81 }, 82 } 83 84 cmd.Flags().Var(&imageSearchSortFlag, SortByFlag, 85 fmt.Sprintf("Options for sorting the output: [%s]", ImageSearchSortOptionsStr())) 86 87 return cmd 88 } 89 90 func OneImageWithRefArg(cmd *cobra.Command, args []string) error { 91 if err := cobra.ExactArgs(1)(cmd, args); err != nil { 92 return err 93 } 94 95 image := args[0] 96 97 if dir, ref, _ := zcommon.GetImageDirAndReference(image); dir == "" || ref == "" { 98 return zerr.ErrInvalidRepoRefFormat 99 } 100 101 return nil 102 }