|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | +) |
| 9 | + |
| 10 | +var searchDescription = "\nSearch the images from specific registry." |
| 11 | + |
| 12 | +// SearchCommand implements search images. |
| 13 | +type SearchCommand struct { |
| 14 | + baseCommand |
| 15 | + registry string |
| 16 | +} |
| 17 | + |
| 18 | +// Init initialize start command. |
| 19 | +func (s *SearchCommand) Init(c *Cli) { |
| 20 | + s.cli = c |
| 21 | + |
| 22 | + s.cmd = &cobra.Command{ |
| 23 | + Use: "search [OPTIONS] TERM", |
| 24 | + Short: "Search the images from specific registry", |
| 25 | + Long: searchDescription, |
| 26 | + Args: cobra.MinimumNArgs(1), |
| 27 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 28 | + return s.runSearch(args) |
| 29 | + }, |
| 30 | + Example: searchExample(), |
| 31 | + } |
| 32 | + s.addFlags() |
| 33 | +} |
| 34 | + |
| 35 | +// addFlags adds flags for specific command. |
| 36 | +func (s *SearchCommand) addFlags() { |
| 37 | + flagSet := s.cmd.Flags() |
| 38 | + |
| 39 | + flagSet.StringVarP(&s.registry, "registry", "r", "", "set registry name") |
| 40 | +} |
| 41 | + |
| 42 | +func (s *SearchCommand) runSearch(args []string) error { |
| 43 | + ctx := context.Background() |
| 44 | + apiClient := s.cli.Client() |
| 45 | + |
| 46 | + term := args[0] |
| 47 | + searchResults, err := apiClient.ImageSearch(ctx, term, s.registry) |
| 48 | + |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + display := s.cli.NewTableDisplay() |
| 54 | + display.AddRow([]string{"NAME", "DESCRIPTION", "STARS", "OFFICIAL", "AUTOMATED"}) |
| 55 | + |
| 56 | + for _, result := range searchResults { |
| 57 | + display.AddRow([]string{result.Name, result.Description, fmt.Sprint(result.StarCount), boolToOKOrNot(result.IsOfficial), boolToOKOrNot(result.IsAutomated)}) |
| 58 | + } |
| 59 | + |
| 60 | + display.Flush() |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// chang bool value to ok or "" bool => "[OK]" false => "" |
| 65 | +func boolToOKOrNot(isTrue bool) string { |
| 66 | + if isTrue { |
| 67 | + return "[OK]" |
| 68 | + } |
| 69 | + return "" |
| 70 | +} |
| 71 | + |
| 72 | +func searchExample() string { |
| 73 | + return `$ pouch search nginx |
| 74 | +NAME DESCRIPTION STARS OFFICIAL AUTOMATED |
| 75 | +nginx Official build of Nginx. 11403 [OK] |
| 76 | +jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1600 [OK] |
| 77 | +richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 712 [OK] |
| 78 | +jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p… 509 [OK] |
| 79 | +webdevops/php-nginx Nginx with PHP-FPM 127 [OK] |
| 80 | +zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 101 [OK] |
| 81 | +bitnami/nginx Bitnami nginx Docker Image 66 [OK] |
| 82 | +linuxserver/nginx An Nginx container, brought to you by LinuxS… 61 |
| 83 | +1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK] |
| 84 | +zabbix/zabbix-web-nginx-pgsql Zabbix frontend based on Nginx with PostgreS… 33 [OK] |
| 85 | +tobi312/rpi-nginx NGINX on Raspberry Pi / ARM 26 [OK] |
| 86 | +nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 20 |
| 87 | +schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 15 [OK] |
| 88 | +nginxdemos/hello NGINX webserver that serves a simple page co… 14 [OK] |
| 89 | +blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK] |
| 90 | +wodby/drupal-nginx Nginx for Drupal container image 12 [OK] |
| 91 | +centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 10 |
| 92 | +centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 9 |
| 93 | +nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 4 |
| 94 | +1science/nginx Nginx Docker images that include Consul Temp… 4 [OK] |
| 95 | +nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 4 |
| 96 | +mailu/nginx Mailu nginx frontend 3 [OK] |
| 97 | +toccoag/openshift-nginx Nginx reverse proxy for Nice running on same… 1 [OK] |
| 98 | +ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK] |
| 99 | +wodby/nginx Generic nginx 0 [OK] |
| 100 | +` |
| 101 | +} |
0 commit comments