github.com/hydgladiator/helm@v3.0.0-beta.3+incompatible/cmd/helm/search_hub.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "io" 22 "strings" 23 24 "github.com/gosuri/uitable" 25 "github.com/pkg/errors" 26 "github.com/spf13/cobra" 27 28 "helm.sh/helm/internal/monocular" 29 ) 30 31 const searchHubDesc = ` 32 Search the Helm Hub or an instance of Monocular for Helm charts. 33 34 The Helm Hub provides a centralized search for publicly available distributed 35 charts. It is maintained by the Helm project. It can be visited at 36 https://hub.helm.sh 37 38 Monocular is a web-based application that enables the search and discovery of 39 charts from multiple Helm Chart repositories. It is the codebase that powers the 40 Helm Hub. You can find it at https://github.com/helm/monocular 41 ` 42 43 type searchHubOptions struct { 44 searchEndpoint string 45 maxColWidth uint 46 } 47 48 func newSearchHubCmd(out io.Writer) *cobra.Command { 49 o := &searchHubOptions{} 50 51 cmd := &cobra.Command{ 52 Use: "hub [keyword]", 53 Short: "search for charts in the Helm Hub or an instance of Monocular", 54 Long: searchHubDesc, 55 RunE: func(cmd *cobra.Command, args []string) error { 56 return o.run(out, args) 57 }, 58 } 59 60 f := cmd.Flags() 61 f.StringVar(&o.searchEndpoint, "endpoint", "https://hub.helm.sh", "monocular instance to query for charts") 62 f.UintVar(&o.maxColWidth, "max-col-width", 50, "maximum column width for output table") 63 64 return cmd 65 } 66 67 func (o *searchHubOptions) run(out io.Writer, args []string) error { 68 69 c, err := monocular.New(o.searchEndpoint) 70 if err != nil { 71 return errors.Wrap(err, fmt.Sprintf("unable to create connection to %q", o.searchEndpoint)) 72 } 73 74 q := strings.Join(args, " ") 75 results, err := c.Search(q) 76 if err != nil { 77 debug("%s", err) 78 return fmt.Errorf("unable to perform search against %q", o.searchEndpoint) 79 } 80 81 fmt.Fprintln(out, o.formatSearchResults(o.searchEndpoint, results)) 82 83 return nil 84 } 85 86 func (o *searchHubOptions) formatSearchResults(endpoint string, res []monocular.SearchResult) string { 87 if len(res) == 0 { 88 return "No results found" 89 } 90 table := uitable.New() 91 92 // The max column width is configurable because a URL could be longer than the 93 // max value and we want the user to have the ability to display the whole url 94 table.MaxColWidth = o.maxColWidth 95 table.AddRow("URL", "CHART VERSION", "APP VERSION", "DESCRIPTION") 96 var url string 97 for _, r := range res { 98 url = endpoint + "/charts/" + r.ID 99 table.AddRow(url, r.Relationships.LatestChartVersion.Data.Version, r.Relationships.LatestChartVersion.Data.AppVersion, r.Attributes.Description) 100 } 101 return table.String() 102 }