github.com/cloudposse/helm@v2.2.3+incompatible/cmd/helm/search.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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/spf13/cobra" 26 27 "k8s.io/helm/cmd/helm/helmpath" 28 "k8s.io/helm/cmd/helm/search" 29 "k8s.io/helm/pkg/repo" 30 ) 31 32 const searchDesc = ` 33 Search reads through all of the repositories configured on the system, and 34 looks for matches. 35 36 Repositories are managed with 'helm repo' commands. 37 ` 38 39 // searchMaxScore suggests that any score higher than this is not considered a match. 40 const searchMaxScore = 25 41 42 type searchCmd struct { 43 out io.Writer 44 helmhome helmpath.Home 45 46 versions bool 47 regexp bool 48 } 49 50 func newSearchCmd(out io.Writer) *cobra.Command { 51 sc := &searchCmd{out: out, helmhome: helmpath.Home(homePath())} 52 53 cmd := &cobra.Command{ 54 Use: "search [keyword]", 55 Short: "search for a keyword in charts", 56 Long: searchDesc, 57 RunE: func(cmd *cobra.Command, args []string) error { 58 return sc.run(args) 59 }, 60 } 61 62 f := cmd.Flags() 63 f.BoolVarP(&sc.regexp, "regexp", "r", false, "use regular expressions for searching") 64 f.BoolVarP(&sc.versions, "versions", "l", false, "show the long listing, with each version of each chart on its own line") 65 66 return cmd 67 } 68 69 func (s *searchCmd) run(args []string) error { 70 index, err := s.buildIndex() 71 if err != nil { 72 return err 73 } 74 75 if len(args) == 0 { 76 s.showAllCharts(index) 77 return nil 78 } 79 80 q := strings.Join(args, " ") 81 res, err := index.Search(q, searchMaxScore, s.regexp) 82 if err != nil { 83 return nil 84 } 85 search.SortScore(res) 86 87 fmt.Fprintln(s.out, s.formatSearchResults(res)) 88 89 return nil 90 } 91 92 func (s *searchCmd) showAllCharts(i *search.Index) { 93 res := i.All() 94 search.SortScore(res) 95 fmt.Fprintln(s.out, s.formatSearchResults(res)) 96 } 97 98 func (s *searchCmd) formatSearchResults(res []*search.Result) string { 99 if len(res) == 0 { 100 return "No results found" 101 } 102 table := uitable.New() 103 table.MaxColWidth = 50 104 table.AddRow("NAME", "VERSION", "DESCRIPTION") 105 for _, r := range res { 106 table.AddRow(r.Name, r.Chart.Version, r.Chart.Description) 107 } 108 return table.String() 109 } 110 111 func (s *searchCmd) buildIndex() (*search.Index, error) { 112 // Load the repositories.yaml 113 rf, err := repo.LoadRepositoriesFile(s.helmhome.RepositoryFile()) 114 if err != nil { 115 return nil, err 116 } 117 118 i := search.NewIndex() 119 for _, re := range rf.Repositories { 120 n := re.Name 121 f := s.helmhome.CacheIndex(n) 122 ind, err := repo.LoadIndexFile(f) 123 if err != nil { 124 fmt.Fprintf(s.out, "WARNING: Repo %q is corrupt or missing. Try 'helm repo update'.", n) 125 continue 126 } 127 128 i.AddRepo(n, ind, s.versions) 129 } 130 return i, nil 131 }