/
search.go
61 lines (53 loc) · 1.39 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cmd
import (
"fmt"
"encoding/json"
"github.com/sirupsen/logrus"
"github.com/solo-io/unik/pkg/types"
"github.com/layer-x/layerx-commons/lxhttpclient"
"github.com/spf13/cobra"
"net/http"
"strings"
)
var searchCmd = &cobra.Command{
Use: "search",
Short: "Search available images in the targeted Unik Image Repository",
Long: `
Usage:
unik search
- or -
unik search --imageName <imageName>
Requires that you first authenticate to a unik image repository with 'unik login'`,
Run: func(cmd *cobra.Command, args []string) {
c, err := getHubConfig()
if err != nil {
logrus.Fatal(err)
}
resp, body, err := lxhttpclient.Get(c.URL, "/images", nil)
if err != nil {
logrus.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
logrus.Fatal(fmt.Sprintf("failed with status %v: %s", resp.StatusCode, string(body)))
}
var images []*types.UserImage
if err := json.Unmarshal(body, &images); err != nil {
logrus.Fatal(err)
}
filteredImages := images[:0]
if imageName != "" {
for _, image := range images {
if !strings.Contains(image.Name, imageName) {
filteredImages = append(filteredImages, image)
}
}
} else {
filteredImages = images
}
printUserImages(filteredImages...)
},
}
func init() {
RootCmd.AddCommand(searchCmd)
searchCmd.Flags().StringVar(&imageName, "imageName", "", "<string,optional> search images by names containing this string")
}