forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(appset): fromYaml, fromYamlArray toYaml functions (argoproj#15063)
* feat(appset): fromYaml, fromYamlArray toYaml functions Signed-off-by: Geoffrey Muselli <geoffrey.muselli@gmail.com> * fix(11993): Document and error messages Signed-off-by: gmuselli <geoffrey.muselli@gmail.com> --------- Signed-off-by: Geoffrey Muselli <geoffrey.muselli@gmail.com> Signed-off-by: gmuselli <geoffrey.muselli@gmail.com>
- Loading branch information
Showing
4 changed files
with
144 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package utils | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// SanitizeName sanitizes the name in accordance with the below rules | ||
// 1. contain no more than 253 characters | ||
// 2. contain only lowercase alphanumeric characters, '-' or '.' | ||
// 3. start and end with an alphanumeric character | ||
func SanitizeName(name string) string { | ||
invalidDNSNameChars := regexp.MustCompile("[^-a-z0-9.]") | ||
maxDNSNameLength := 253 | ||
|
||
name = strings.ToLower(name) | ||
name = invalidDNSNameChars.ReplaceAllString(name, "-") | ||
if len(name) > maxDNSNameLength { | ||
name = name[:maxDNSNameLength] | ||
} | ||
|
||
return strings.Trim(name, "-.") | ||
} | ||
|
||
// This has been copied from helm and may be removed as soon as it is retrofited in sprig | ||
// toYAML takes an interface, marshals it to yaml, and returns a string. It will | ||
// always return a string, even on marshal error (empty string). | ||
// | ||
// This is designed to be called from a template. | ||
func toYAML(v interface{}) (string, error) { | ||
data, err := yaml.Marshal(v) | ||
if err != nil { | ||
// Swallow errors inside of a template. | ||
return "", err | ||
} | ||
return strings.TrimSuffix(string(data), "\n"), nil | ||
} | ||
|
||
// This has been copied from helm and may be removed as soon as it is retrofited in sprig | ||
// fromYAML converts a YAML document into a map[string]interface{}. | ||
// | ||
// This is not a general-purpose YAML parser, and will not parse all valid | ||
// YAML documents. Additionally, because its intended use is within templates | ||
// it tolerates errors. It will insert the returned error message string into | ||
// m["Error"] in the returned map. | ||
func fromYAML(str string) (map[string]interface{}, error) { | ||
m := map[string]interface{}{} | ||
|
||
if err := yaml.Unmarshal([]byte(str), &m); err != nil { | ||
return nil, err | ||
} | ||
return m, nil | ||
} | ||
|
||
// This has been copied from helm and may be removed as soon as it is retrofited in sprig | ||
// fromYAMLArray converts a YAML array into a []interface{}. | ||
// | ||
// This is not a general-purpose YAML parser, and will not parse all valid | ||
// YAML documents. Additionally, because its intended use is within templates | ||
// it tolerates errors. It will insert the returned error message string as | ||
// the first and only item in the returned array. | ||
func fromYAMLArray(str string) ([]interface{}, error) { | ||
a := []interface{}{} | ||
|
||
if err := yaml.Unmarshal([]byte(str), &a); err != nil { | ||
return nil, err | ||
} | ||
return a, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters