github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/chartutil/files.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package chartutil 17 18 import ( 19 "github.com/golang/protobuf/ptypes/any" 20 ) 21 22 // Files is a map of files in a chart that can be accessed from a template. 23 type Files map[string][]byte 24 25 // NewFiles creates a new Files from chart files. 26 // Given an []*any.Any (the format for files in a chart.Chart), extract a map of files. 27 func NewFiles(from []*any.Any) Files { 28 files := map[string][]byte{} 29 for _, f := range from { 30 files[f.TypeUrl] = f.Value 31 } 32 return files 33 } 34 35 // GetBytes gets a file by path. 36 // 37 // The returned data is raw. In a template context, this is identical to calling 38 // {{index .Files $path}}. 39 // 40 // This is intended to be accessed from within a template, so a missed key returns 41 // an empty []byte. 42 func (f Files) GetBytes(name string) []byte { 43 v, ok := f[name] 44 if !ok { 45 return []byte{} 46 } 47 return v 48 } 49 50 // Get returns a string representation of the given file. 51 // 52 // Fetch the contents of a file as a string. It is designed to be called in a 53 // template. 54 // 55 // {{.Files.Get "foo"}} 56 func (f Files) Get(name string) string { 57 return string(f.GetBytes(name)) 58 }