2016-08-29 09:59:41 -04:00
package registry
2016-09-08 13:11:39 -04:00
import (
2018-05-03 21:02:44 -04:00
"context"
2023-07-10 14:18:28 -04:00
"fmt"
2016-09-08 13:11:39 -04:00
2017-04-17 18:07:56 -04:00
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
2017-08-15 06:58:49 -04:00
"github.com/docker/cli/cli/command/formatter"
2017-05-15 08:45:19 -04:00
"github.com/docker/cli/opts"
2016-09-08 13:11:39 -04:00
"github.com/docker/docker/api/types"
2023-04-11 12:16:30 -04:00
registrytypes "github.com/docker/docker/api/types/registry"
2016-09-08 13:11:39 -04:00
"github.com/docker/docker/registry"
"github.com/spf13/cobra"
)
type searchOptions struct {
2017-08-15 06:58:49 -04:00
format string
2016-09-08 13:11:39 -04:00
term string
noTrunc bool
limit int
2016-09-13 14:53:11 -04:00
filter opts . FilterOpt
2016-09-08 13:11:39 -04:00
}
// NewSearchCommand creates a new `docker search` command
2017-05-03 17:58:52 -04:00
func NewSearchCommand ( dockerCli command . Cli ) * cobra . Command {
2017-05-15 08:45:19 -04:00
options := searchOptions { filter : opts . NewFilterOpt ( ) }
2016-09-08 13:11:39 -04:00
cmd := & cobra . Command {
Use : "search [OPTIONS] TERM" ,
2022-03-29 05:24:51 -04:00
Short : "Search Docker Hub for images" ,
2016-09-08 13:11:39 -04:00
Args : cli . ExactArgs ( 1 ) ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2017-05-15 08:45:19 -04:00
options . term = args [ 0 ]
return runSearch ( dockerCli , options )
2016-09-08 13:11:39 -04:00
} ,
2022-03-30 03:37:08 -04:00
Annotations : map [ string ] string {
"category-top" : "10" ,
} ,
2016-09-08 13:11:39 -04:00
}
flags := cmd . Flags ( )
2017-05-15 08:45:19 -04:00
flags . BoolVar ( & options . noTrunc , "no-trunc" , false , "Don't truncate output" )
flags . VarP ( & options . filter , "filter" , "f" , "Filter output based on conditions provided" )
2022-03-15 10:06:35 -04:00
flags . IntVar ( & options . limit , "limit" , 0 , "Max number of search results" )
2017-08-15 06:58:49 -04:00
flags . StringVar ( & options . format , "format" , "" , "Pretty-print search using a Go template" )
2016-09-08 13:11:39 -04:00
return cmd
}
2017-05-15 08:45:19 -04:00
func runSearch ( dockerCli command . Cli , options searchOptions ) error {
2023-07-10 14:18:28 -04:00
if options . filter . Value ( ) . Contains ( "is-automated" ) {
_ , _ = fmt . Fprintln ( dockerCli . Err ( ) , ` WARNING: the "is-automated" filter is deprecated, and searching for "is-automated=true" will not yield any results in future. ` )
}
2017-05-15 08:45:19 -04:00
indexInfo , err := registry . ParseSearchIndexInfo ( options . term )
2016-09-08 13:11:39 -04:00
if err != nil {
return err
}
2023-07-10 11:24:07 -04:00
authConfig := command . ResolveAuthConfig ( dockerCli . ConfigFile ( ) , indexInfo )
2023-04-11 12:16:30 -04:00
encodedAuth , err := registrytypes . EncodeAuthConfig ( authConfig )
2016-09-08 13:11:39 -04:00
if err != nil {
return err
}
2023-07-10 11:24:07 -04:00
ctx := context . Background ( )
2023-04-11 12:16:30 -04:00
requestPrivilege := command . RegistryAuthenticationPrivilegedFunc ( dockerCli , indexInfo , "search" )
2023-04-11 11:55:00 -04:00
results , err := dockerCli . Client ( ) . ImageSearch ( ctx , options . term , types . ImageSearchOptions {
2016-09-08 13:11:39 -04:00
RegistryAuth : encodedAuth ,
PrivilegeFunc : requestPrivilege ,
2017-05-15 08:45:19 -04:00
Filters : options . filter . Value ( ) ,
Limit : options . limit ,
2023-04-11 11:55:00 -04:00
} )
2016-09-08 13:11:39 -04:00
if err != nil {
return err
}
2017-08-15 06:58:49 -04:00
searchCtx := formatter . Context {
Output : dockerCli . Out ( ) ,
2018-10-23 11:05:44 -04:00
Format : NewSearchFormat ( options . format ) ,
2017-08-15 06:58:49 -04:00
Trunc : ! options . noTrunc ,
2016-09-08 13:11:39 -04:00
}
2020-02-13 11:18:10 -05:00
return SearchWrite ( searchCtx , results )
2016-09-08 13:11:39 -04:00
}