From d56c5406acb3f48ee338a8e4007be7c135074779 Mon Sep 17 00:00:00 2001 From: shin- Date: Tue, 7 May 2013 03:49:08 -0700 Subject: [PATCH 1/4] Implemented command --- commands.go | 29 +++++++++++++++++++++++++++++ registry.go | 31 +++++++++++++++++++++++++++++++ utils.go | 1 - 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 6f3240448c..1bbbc983fc 100644 --- a/commands.go +++ b/commands.go @@ -56,6 +56,7 @@ func (srv *Server) Help() string { {"rm", "Remove a container"}, {"rmi", "Remove an image"}, {"run", "Run a command in a new container"}, + {"search", "Search for an image in the docker index"} {"start", "Start a stopped container"}, {"stop", "Stop a running container"}, {"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) } +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 type ports []int diff --git a/registry.go b/registry.go index f9bc756643..cc76485c09 100644 --- a/registry.go +++ b/registry.go @@ -9,6 +9,7 @@ import ( "io" "io/ioutil" "net/http" + "net/url" "path" "strings" ) @@ -638,3 +639,33 @@ func (graph *Graph) Checksums(output io.Writer, repo Repository) ([]map[string]s } 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 +} diff --git a/utils.go b/utils.go index 095be2f4bf..2d54ef4c6a 100644 --- a/utils.go +++ b/utils.go @@ -404,7 +404,6 @@ func CopyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) return written, err } - func HashData(src io.Reader) (string, error) { h := sha256.New() if _, err := io.Copy(h, src); err != nil { From 3d25e09c3b572a7f060e4e7ad0f3b4a2f191e9de Mon Sep 17 00:00:00 2001 From: shin- Date: Tue, 7 May 2013 03:54:31 -0700 Subject: [PATCH 2/4] missing comma --- commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 1bbbc983fc..dba8cd8572 100644 --- a/commands.go +++ b/commands.go @@ -56,7 +56,7 @@ func (srv *Server) Help() string { {"rm", "Remove a container"}, {"rmi", "Remove an image"}, {"run", "Run a command in a new container"}, - {"search", "Search for an image in the docker index"} + {"search", "Search for an image in the docker index"}, {"start", "Start a stopped container"}, {"stop", "Stop a running container"}, {"tag", "Tag an image into a repository"}, From 4df26b9ee74c9d381447a10f9eae5db1ab1872c8 Mon Sep 17 00:00:00 2001 From: shin- Date: Tue, 7 May 2013 10:29:49 -0700 Subject: [PATCH 3/4] Added doc page for new search command --- docs/sources/commandline/cli.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/sources/commandline/cli.rst b/docs/sources/commandline/cli.rst index 46ea3e4a7f..47ecb79e67 100644 --- a/docs/sources/commandline/cli.rst +++ b/docs/sources/commandline/cli.rst @@ -47,6 +47,7 @@ Available Commands command/rm command/rmi command/run + command/search command/start command/stop command/tag From 82513815f1d2bee77ae729b4293e29d6377ab03d Mon Sep 17 00:00:00 2001 From: shin- Date: Tue, 7 May 2013 10:31:55 -0700 Subject: [PATCH 4/4] Added actual doc file --- docs/sources/commandline/command/search.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 docs/sources/commandline/command/search.rst diff --git a/docs/sources/commandline/command/search.rst b/docs/sources/commandline/command/search.rst new file mode 100644 index 0000000000..0af24dfaf5 --- /dev/null +++ b/docs/sources/commandline/command/search.rst @@ -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.