mirror of https://github.com/helm/helm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.7 KiB
84 lines
1.7 KiB
package chartutil
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
"strings"
|
|
|
|
"github.com/ghodss/yaml"
|
|
)
|
|
|
|
// ErrNoTable indicates that a chart does not have a matching table.
|
|
var ErrNoTable = errors.New("no table")
|
|
|
|
// Values represents a collection of chart values.
|
|
type Values map[string]interface{}
|
|
|
|
// Table gets a table (YAML subsection) from a Values object.
|
|
//
|
|
// The table is returned as a Values.
|
|
//
|
|
// Compound table names may be specified with dots:
|
|
//
|
|
// foo.bar
|
|
//
|
|
// The above will be evaluated as "The table bar inside the table
|
|
// foo".
|
|
//
|
|
// An ErrNoTable is returned if the table does not exist.
|
|
func (v Values) Table(name string) (Values, error) {
|
|
names := strings.Split(name, ".")
|
|
table := v
|
|
var err error
|
|
|
|
for _, n := range names {
|
|
table, err = tableLookup(table, n)
|
|
if err != nil {
|
|
return table, err
|
|
}
|
|
}
|
|
return table, err
|
|
}
|
|
|
|
// Encode writes serialized Values information to the given io.Writer.
|
|
func (v Values) Encode(w io.Writer) error {
|
|
//return yaml.NewEncoder(w).Encode(v)
|
|
out, err := yaml.Marshal(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = w.Write(out)
|
|
return err
|
|
}
|
|
|
|
func tableLookup(v Values, simple string) (Values, error) {
|
|
v2, ok := v[simple]
|
|
if !ok {
|
|
return v, ErrNoTable
|
|
}
|
|
vv, ok := v2.(map[string]interface{})
|
|
if !ok {
|
|
return vv, ErrNoTable
|
|
}
|
|
return vv, nil
|
|
}
|
|
|
|
// ReadValues will parse YAML byte data into a Values.
|
|
func ReadValues(data []byte) (vals Values, err error) {
|
|
vals = make(map[string]interface{})
|
|
if len(data) > 0 {
|
|
err = yaml.Unmarshal(data, &vals)
|
|
}
|
|
return
|
|
}
|
|
|
|
// ReadValuesFile will parse a YAML file into a Values.
|
|
func ReadValuesFile(filename string) (Values, error) {
|
|
data, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return map[string]interface{}{}, err
|
|
}
|
|
return ReadValues(data)
|
|
}
|