mirror of https://github.com/docker/docs.git
Merge pull request #542 from dotcloud/docker-search
+ Registry: Add docker search top level command in order to search a repository
This commit is contained in:
commit
01575e1f67
29
commands.go
29
commands.go
|
@ -56,6 +56,7 @@ func (srv *Server) Help() string {
|
||||||
{"rm", "Remove a container"},
|
{"rm", "Remove a container"},
|
||||||
{"rmi", "Remove an image"},
|
{"rmi", "Remove an image"},
|
||||||
{"run", "Run a command in a new container"},
|
{"run", "Run a command in a new container"},
|
||||||
|
{"search", "Search for an image in the docker index"},
|
||||||
{"start", "Start a stopped container"},
|
{"start", "Start a stopped container"},
|
||||||
{"stop", "Stop a running container"},
|
{"stop", "Stop a running container"},
|
||||||
{"tag", "Tag an image into a repository"},
|
{"tag", "Tag an image into a repository"},
|
||||||
|
@ -1001,6 +1002,34 @@ func (srv *Server) CmdAttach(stdin io.ReadCloser, stdout rcli.DockerConn, args .
|
||||||
return <-container.Attach(stdin, nil, stdout, stdout)
|
return <-container.Attach(stdin, nil, stdout, stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv *Server) CmdSearch(stdin io.ReadCloser, stdout rcli.DockerConn, args ...string) error {
|
||||||
|
cmd := rcli.Subcmd(stdout, "search", "NAME", "Search the docker index for images")
|
||||||
|
if err := cmd.Parse(args); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if cmd.NArg() != 1 {
|
||||||
|
cmd.Usage()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
term := cmd.Arg(0)
|
||||||
|
results, err := srv.runtime.graph.SearchRepositories(stdout, term)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(stdout, "Found %d results matching your query (\"%s\")\n", results.NumResults, results.Query)
|
||||||
|
w := tabwriter.NewWriter(stdout, 20, 1, 3, ' ', 0)
|
||||||
|
fmt.Fprintf(w, "NAME\tDESCRIPTION\n")
|
||||||
|
for _, repo := range results.Results {
|
||||||
|
description := repo["description"]
|
||||||
|
if len(description) > 45 {
|
||||||
|
description = Trunc(description, 42) + "..."
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%s\t%s\n", repo["name"], description)
|
||||||
|
}
|
||||||
|
w.Flush()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Ports type - Used to parse multiple -p flags
|
// Ports type - Used to parse multiple -p flags
|
||||||
type ports []int
|
type ports []int
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@ Available Commands
|
||||||
command/rm
|
command/rm
|
||||||
command/rmi
|
command/rmi
|
||||||
command/run
|
command/run
|
||||||
|
command/search
|
||||||
command/start
|
command/start
|
||||||
command/stop
|
command/stop
|
||||||
command/tag
|
command/tag
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
===================================================================
|
||||||
|
``search`` -- Search for an image in the docker index
|
||||||
|
===================================================================
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
Usage: docker search TERM
|
||||||
|
|
||||||
|
Searches for the TERM parameter on the Docker index and prints out a list of repositories
|
||||||
|
that match.
|
31
registry.go
31
registry.go
|
@ -9,6 +9,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -638,3 +639,33 @@ func (graph *Graph) Checksums(output io.Writer, repo Repository) ([]map[string]s
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SearchResults struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
NumResults int `json:"num_results"`
|
||||||
|
Results []map[string]string `json:"results"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (graph *Graph) SearchRepositories(stdout io.Writer, term string) (*SearchResults, error) {
|
||||||
|
client := graph.getHttpClient()
|
||||||
|
u := INDEX_ENDPOINT + "/search?q=" + url.QueryEscape(term)
|
||||||
|
req, err := http.NewRequest("GET", u, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
return nil, fmt.Errorf("Unexepected status code %d", res.StatusCode)
|
||||||
|
}
|
||||||
|
rawData, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result := new(SearchResults)
|
||||||
|
err = json.Unmarshal(rawData, result)
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
1
utils.go
1
utils.go
|
@ -404,7 +404,6 @@ func CopyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error)
|
||||||
return written, err
|
return written, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func HashData(src io.Reader) (string, error) {
|
func HashData(src io.Reader) (string, error) {
|
||||||
h := sha256.New()
|
h := sha256.New()
|
||||||
if _, err := io.Copy(h, src); err != nil {
|
if _, err := io.Copy(h, src); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue