github.com/blend/go-sdk@v1.20220411.3/template/vars.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package template 9 10 import ( 11 "encoding/json" 12 "os" 13 "strings" 14 15 "gopkg.in/yaml.v3" 16 ) 17 18 // Vars is a soft alias to map[string]interface{}. 19 type Vars = map[string]interface{} 20 21 // NewVarsFromPath returns a new vars file from a given path. 22 func NewVarsFromPath(path string) (map[string]interface{}, error) { 23 contents, err := os.ReadFile(path) 24 if err != nil { 25 return nil, err 26 } 27 output := map[string]interface{}{} 28 if strings.HasSuffix(path, ".json") { 29 err = json.Unmarshal(contents, &output) 30 if err != nil { 31 return nil, err 32 } 33 } else { 34 err = yaml.Unmarshal(contents, &output) 35 if err != nil { 36 return nil, err 37 } 38 } 39 return output, nil 40 } 41 42 // MergeVars merges a variadic array of variable sets. 43 func MergeVars(vars ...Vars) Vars { 44 output := Vars{} 45 for _, set := range vars { 46 for key, value := range set { 47 output[key] = value 48 } 49 } 50 return output 51 }