github.com/argoproj/argo-cd/v2@v2.10.9/pkg/apis/application/v1alpha1/values.go (about) 1 package v1alpha1 2 3 import ( 4 "encoding/json" 5 "fmt" 6 reflect "reflect" 7 "strings" 8 9 runtime "k8s.io/apimachinery/pkg/runtime" 10 "sigs.k8s.io/yaml" 11 ) 12 13 // Set the ValuesObject property to the json representation of the yaml contained in value 14 // Remove Values property if present 15 func (h *ApplicationSourceHelm) SetValuesString(value string) error { 16 if value == "" { 17 h.ValuesObject = nil 18 h.Values = "" 19 } else { 20 data, err := yaml.YAMLToJSON([]byte(value)) 21 if err != nil { 22 return fmt.Errorf("failed converting yaml to json: %v", err) 23 } 24 var v interface{} 25 if err := json.Unmarshal(data, &v); err != nil { 26 return fmt.Errorf("failed to unmarshal json: %v", err) 27 } 28 switch v.(type) { 29 case string: 30 case map[string]interface{}: 31 default: 32 return fmt.Errorf("invalid type %q", reflect.TypeOf(v)) 33 } 34 h.ValuesObject = &runtime.RawExtension{Raw: data} 35 h.Values = "" 36 } 37 return nil 38 } 39 40 func (h *ApplicationSourceHelm) ValuesYAML() []byte { 41 if h.ValuesObject == nil || h.ValuesObject.Raw == nil { 42 return []byte(h.Values) 43 } 44 b, err := yaml.JSONToYAML(h.ValuesObject.Raw) 45 if err != nil { 46 // This should be impossible, because rawValue isn't set directly. 47 return []byte{} 48 } 49 return b 50 } 51 52 func (h *ApplicationSourceHelm) ValuesIsEmpty() bool { 53 return len(h.ValuesYAML()) == 0 54 } 55 56 func (h *ApplicationSourceHelm) ValuesString() string { 57 if h.ValuesObject == nil || h.ValuesObject.Raw == nil { 58 return h.Values 59 } 60 return strings.TrimSuffix(string(h.ValuesYAML()), "\n") 61 }