github.com/kruglovmax/helm@v3.0.0-beta.3+incompatible/pkg/cli/values/options.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 values 18 19 import ( 20 "io/ioutil" 21 "net/url" 22 "os" 23 "strings" 24 25 "github.com/pkg/errors" 26 "sigs.k8s.io/yaml" 27 28 "helm.sh/helm/pkg/getter" 29 "helm.sh/helm/pkg/strvals" 30 ) 31 32 type Options struct { 33 ValueFiles []string 34 StringValues []string 35 Values []string 36 } 37 38 // MergeValues merges values from files specified via -f/--values and 39 // directly via --set or --set-string, marshaling them to YAML 40 func (opts *Options) MergeValues(p getter.Providers) (map[string]interface{}, error) { 41 base := map[string]interface{}{} 42 43 // User specified a values files via -f/--values 44 for _, filePath := range opts.ValueFiles { 45 currentMap := map[string]interface{}{} 46 47 bytes, err := readFile(filePath, p) 48 if err != nil { 49 return nil, err 50 } 51 52 if err := yaml.Unmarshal(bytes, ¤tMap); err != nil { 53 return nil, errors.Wrapf(err, "failed to parse %s", filePath) 54 } 55 // Merge with the previous map 56 base = mergeMaps(base, currentMap) 57 } 58 59 // User specified a value via --set 60 for _, value := range opts.Values { 61 if err := strvals.ParseInto(value, base); err != nil { 62 return nil, errors.Wrap(err, "failed parsing --set data") 63 } 64 } 65 66 // User specified a value via --set-string 67 for _, value := range opts.StringValues { 68 if err := strvals.ParseIntoString(value, base); err != nil { 69 return nil, errors.Wrap(err, "failed parsing --set-string data") 70 } 71 } 72 73 return base, nil 74 } 75 76 func mergeMaps(a, b map[string]interface{}) map[string]interface{} { 77 out := make(map[string]interface{}, len(a)) 78 for k, v := range a { 79 out[k] = v 80 } 81 for k, v := range b { 82 if v, ok := v.(map[string]interface{}); ok { 83 if bv, ok := out[k]; ok { 84 if bv, ok := bv.(map[string]interface{}); ok { 85 out[k] = mergeMaps(bv, v) 86 continue 87 } 88 } 89 } 90 out[k] = v 91 } 92 return out 93 } 94 95 // readFile load a file from stdin, the local directory, or a remote file with a url. 96 func readFile(filePath string, p getter.Providers) ([]byte, error) { 97 if strings.TrimSpace(filePath) == "-" { 98 return ioutil.ReadAll(os.Stdin) 99 } 100 u, _ := url.Parse(filePath) 101 102 // FIXME: maybe someone handle other protocols like ftp. 103 g, err := p.ByScheme(u.Scheme) 104 if err != nil { 105 return ioutil.ReadFile(filePath) 106 } 107 data, err := g.Get(filePath, getter.WithURL(filePath)) 108 return data.Bytes(), err 109 }