github.com/y-taka-23/helm@v2.8.0+incompatible/pkg/repo/repo.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 repo // import "k8s.io/helm/pkg/repo" 18 19 import ( 20 "errors" 21 "fmt" 22 "io/ioutil" 23 "os" 24 "time" 25 26 "github.com/ghodss/yaml" 27 ) 28 29 // ErrRepoOutOfDate indicates that the repository file is out of date, but 30 // is fixable. 31 var ErrRepoOutOfDate = errors.New("repository file is out of date") 32 33 // RepoFile represents the repositories.yaml file in $HELM_HOME 34 type RepoFile struct { 35 APIVersion string `json:"apiVersion"` 36 Generated time.Time `json:"generated"` 37 Repositories []*Entry `json:"repositories"` 38 } 39 40 // NewRepoFile generates an empty repositories file. 41 // 42 // Generated and APIVersion are automatically set. 43 func NewRepoFile() *RepoFile { 44 return &RepoFile{ 45 APIVersion: APIVersionV1, 46 Generated: time.Now(), 47 Repositories: []*Entry{}, 48 } 49 } 50 51 // LoadRepositoriesFile takes a file at the given path and returns a RepoFile object 52 // 53 // If this returns ErrRepoOutOfDate, it also returns a recovered RepoFile that 54 // can be saved as a replacement to the out of date file. 55 func LoadRepositoriesFile(path string) (*RepoFile, error) { 56 b, err := ioutil.ReadFile(path) 57 if err != nil { 58 if os.IsNotExist(err) { 59 return nil, fmt.Errorf( 60 "Couldn't load repositories file (%s).\n"+ 61 "You might need to run `helm init` (or "+ 62 "`helm init --client-only` if tiller is "+ 63 "already installed)", path) 64 } 65 return nil, err 66 } 67 68 r := &RepoFile{} 69 err = yaml.Unmarshal(b, r) 70 if err != nil { 71 return nil, err 72 } 73 74 // File is either corrupt, or is from before v2.0.0-Alpha.5 75 if r.APIVersion == "" { 76 m := map[string]string{} 77 if err = yaml.Unmarshal(b, &m); err != nil { 78 return nil, err 79 } 80 r := NewRepoFile() 81 for k, v := range m { 82 r.Add(&Entry{ 83 Name: k, 84 URL: v, 85 Cache: fmt.Sprintf("%s-index.yaml", k), 86 }) 87 } 88 return r, ErrRepoOutOfDate 89 } 90 91 return r, nil 92 } 93 94 // Add adds one or more repo entries to a repo file. 95 func (r *RepoFile) Add(re ...*Entry) { 96 r.Repositories = append(r.Repositories, re...) 97 } 98 99 // Update attempts to replace one or more repo entries in a repo file. If an 100 // entry with the same name doesn't exist in the repo file it will add it. 101 func (r *RepoFile) Update(re ...*Entry) { 102 for _, target := range re { 103 found := false 104 for j, repo := range r.Repositories { 105 if repo.Name == target.Name { 106 r.Repositories[j] = target 107 found = true 108 break 109 } 110 } 111 if !found { 112 r.Add(target) 113 } 114 } 115 } 116 117 // Has returns true if the given name is already a repository name. 118 func (r *RepoFile) Has(name string) bool { 119 for _, rf := range r.Repositories { 120 if rf.Name == name { 121 return true 122 } 123 } 124 return false 125 } 126 127 // Remove removes the entry from the list of repositories. 128 func (r *RepoFile) Remove(name string) bool { 129 cp := []*Entry{} 130 found := false 131 for _, rf := range r.Repositories { 132 if rf.Name == name { 133 found = true 134 continue 135 } 136 cp = append(cp, rf) 137 } 138 r.Repositories = cp 139 return found 140 } 141 142 // WriteFile writes a repositories file to the given path. 143 func (r *RepoFile) WriteFile(path string, perm os.FileMode) error { 144 data, err := yaml.Marshal(r) 145 if err != nil { 146 return err 147 } 148 return ioutil.WriteFile(path, data, perm) 149 }