Browse code

Merge pull request #2382 from dotcloud/reflect_future_changes_search_api

Update docker search to reflect future changes of the api

Victor Vieux authored on 2013/11/05 09:14:14
Showing 6 changed files
... ...
@@ -78,11 +78,6 @@ type APIContainersOld struct {
78 78
 	SizeRootFs int64
79 79
 }
80 80
 
81
-type APISearch struct {
82
-	Name        string
83
-	Description string
84
-}
85
-
86 81
 type APIID struct {
87 82
 	ID string `json:"Id"`
88 83
 }
... ...
@@ -1420,8 +1420,10 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
1420 1420
 }
1421 1421
 
1422 1422
 func (cli *DockerCli) CmdSearch(args ...string) error {
1423
-	cmd := Subcmd("search", "NAME", "Search the docker index for images")
1423
+	cmd := Subcmd("search", "TERM", "Search the docker index for images")
1424 1424
 	noTrunc := cmd.Bool("notrunc", false, "Don't truncate output")
1425
+	trusted := cmd.Bool("trusted", false, "Only show trusted builds")
1426
+	stars := cmd.Int("stars", 0, "Only displays with at least xxx stars")
1425 1427
 	if err := cmd.Parse(args); err != nil {
1426 1428
 		return nil
1427 1429
 	}
... ...
@@ -1437,27 +1439,32 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
1437 1437
 		return err
1438 1438
 	}
1439 1439
 
1440
-	outs := []APISearch{}
1440
+	outs := []registry.SearchResult{}
1441 1441
 	err = json.Unmarshal(body, &outs)
1442 1442
 	if err != nil {
1443 1443
 		return err
1444 1444
 	}
1445
-	fmt.Fprintf(cli.out, "Found %d results matching your query (\"%s\")\n", len(outs), cmd.Arg(0))
1446
-	w := tabwriter.NewWriter(cli.out, 33, 1, 3, ' ', 0)
1447
-	fmt.Fprintf(w, "NAME\tDESCRIPTION\n")
1448
-	_, width := cli.getTtySize()
1449
-	if width == 0 {
1450
-		width = 45
1451
-	} else {
1452
-		width = width - 33 //remote the first column
1453
-	}
1445
+	w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
1446
+	fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tTRUSTED\n")
1454 1447
 	for _, out := range outs {
1448
+		if (*trusted && !out.IsTrusted) || (*stars > out.StarCount) {
1449
+			continue
1450
+		}
1455 1451
 		desc := strings.Replace(out.Description, "\n", " ", -1)
1456 1452
 		desc = strings.Replace(desc, "\r", " ", -1)
1457
-		if !*noTrunc && len(desc) > width {
1458
-			desc = utils.Trunc(desc, width-3) + "..."
1453
+		if !*noTrunc && len(desc) > 45 {
1454
+			desc = utils.Trunc(desc, 42) + "..."
1455
+		}
1456
+		fmt.Fprintf(w, "%s\t%s\t%d\t", out.Name, desc, out.StarCount)
1457
+		if out.IsOfficial {
1458
+			fmt.Fprint(w, "[OK]")
1459
+
1460
+		}
1461
+		fmt.Fprint(w, "\t")
1462
+		if out.IsTrusted {
1463
+			fmt.Fprint(w, "[OK]")
1459 1464
 		}
1460
-		fmt.Fprintf(w, "%s\t%s\n", out.Name, desc)
1465
+		fmt.Fprint(w, "\n")
1461 1466
 	}
1462 1467
 	w.Flush()
1463 1468
 	return nil
... ...
@@ -426,7 +426,7 @@ _docker_run()
426 426
 
427 427
 _docker_search()
428 428
 {
429
-	COMPREPLY=( $( compgen -W "-notrunc" -- "$cur" ) )
429
+	COMPREPLY=( $( compgen -W "-notrunc" "-stars" "-trusted" -- "$cur" ) )
430 430
 }
431 431
 
432 432
 _docker_start()
... ...
@@ -677,8 +677,11 @@ to the newly created container.
677 677
 
678 678
     Usage: docker search TERM
679 679
 
680
-    Searches for the TERM parameter on the Docker index and prints out
681
-    a list of repositories that match.
680
+    Search the docker index for images
681
+
682
+     -notrunc=false: Don't truncate output
683
+     -stars=0: Only displays with at least xxx stars
684
+     -trusted=false: Only show trusted builds
682 685
 
683 686
 .. _cli_start:
684 687
 
... ...
@@ -615,10 +615,18 @@ func (r *Registry) GetAuthConfig(withPasswd bool) *auth.AuthConfig {
615 615
 	}
616 616
 }
617 617
 
618
+type SearchResult struct {
619
+	StarCount   int    `json:"star_count"`
620
+	IsOfficial  bool   `json:"is_official"`
621
+	Name        string `json:"name"`
622
+	IsTrusted   bool   `json:"is_trusted"`
623
+	Description string `json:"description"`
624
+}
625
+
618 626
 type SearchResults struct {
619
-	Query      string              `json:"query"`
620
-	NumResults int                 `json:"num_results"`
621
-	Results    []map[string]string `json:"results"`
627
+	Query      string         `json:"query"`
628
+	NumResults int            `json:"num_results"`
629
+	Results    []SearchResult `json:"results"`
622 630
 }
623 631
 
624 632
 type RepositoryData struct {
... ...
@@ -183,7 +183,7 @@ func (srv *Server) ContainerExport(name string, out io.Writer) error {
183 183
 	return fmt.Errorf("No such container: %s", name)
184 184
 }
185 185
 
186
-func (srv *Server) ImagesSearch(term string) ([]APISearch, error) {
186
+func (srv *Server) ImagesSearch(term string) ([]registry.SearchResult, error) {
187 187
 	r, err := registry.NewRegistry(srv.runtime.config.Root, nil, srv.HTTPRequestFactory(nil))
188 188
 	if err != nil {
189 189
 		return nil, err
... ...
@@ -192,15 +192,7 @@ func (srv *Server) ImagesSearch(term string) ([]APISearch, error) {
192 192
 	if err != nil {
193 193
 		return nil, err
194 194
 	}
195
-
196
-	var outs []APISearch
197
-	for _, repo := range results.Results {
198
-		var out APISearch
199
-		out.Description = repo["description"]
200
-		out.Name = repo["name"]
201
-		outs = append(outs, out)
202
-	}
203
-	return outs, nil
195
+	return results.Results, nil
204 196
 }
205 197
 
206 198
 func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) (string, error) {