github.com/helmwave/helmwave@v0.36.4-0.20240509190856-b35563eba4c6/pkg/hooks/yaml.go (about) 1 package hooks 2 3 import ( 4 "strings" 5 6 "github.com/helmwave/helmwave/pkg/helper" 7 "gopkg.in/yaml.v3" 8 ) 9 10 // UnmarshalYAML is an unmarshaller for gopkg.in/yaml.v3 to parse YAML into `Hook` interface. 11 func (h *Hooks) UnmarshalYAML(node *yaml.Node) error { 12 rr := make([]*hook, 0) 13 err := node.Decode(&rr) 14 if err != nil { 15 return NewYAMLDecodeError(err) 16 } 17 18 *h = helper.SlicesMap(rr, func(h *hook) Hook { return h }) 19 20 return nil 21 } 22 23 func (h *hook) UnmarshalYAML(node *yaml.Node) error { 24 type raw hook 25 var err error 26 27 // show by default 28 h.Show = true 29 30 switch node.Kind { 31 // single value or reference to another value 32 case yaml.ScalarNode, yaml.AliasNode: 33 var script string 34 err = node.Decode(&script) 35 if err != nil { 36 break 37 } 38 39 // Short name 40 words := strings.Fields(script) 41 if len(words) > 1 { 42 h.Cmd = words[0] 43 h.Args = words[1:] 44 } else { 45 h.Cmd = script 46 h.Args = []string{} 47 } 48 49 case yaml.MappingNode: 50 err = node.Decode((*raw)(h)) 51 default: 52 err = ErrUnknownFormat 53 } 54 55 if err != nil { 56 return NewYAMLDecodeError(err) 57 } 58 59 return nil 60 }