1
16
17 package util
18
19 import (
20 "crypto/md5"
21 "errors"
22 "fmt"
23 "path"
24 "path/filepath"
25 "strings"
26 "time"
27
28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29 "k8s.io/apimachinery/pkg/runtime"
30 )
31
32
33 func ParseRFC3339(s string, nowFn func() metav1.Time) (metav1.Time, error) {
34 if t, timeErr := time.Parse(time.RFC3339Nano, s); timeErr == nil {
35 return metav1.Time{Time: t}, nil
36 }
37 t, err := time.Parse(time.RFC3339, s)
38 if err != nil {
39 return metav1.Time{}, err
40 }
41 return metav1.Time{Time: t}, nil
42 }
43
44
45 func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) {
46 data, err := runtime.Encode(codec, obj)
47 if err != nil {
48 return "", err
49 }
50 return fmt.Sprintf("%x", md5.Sum(data)), nil
51 }
52
53
54
55
56
57
58
59
60
61 func ParseFileSource(source string) (keyName, filePath string, err error) {
62 numSeparators := strings.Count(source, "=")
63 switch {
64 case numSeparators == 0:
65 return path.Base(filepath.ToSlash(source)), source, nil
66 case numSeparators == 1 && strings.HasPrefix(source, "="):
67 return "", "", fmt.Errorf("key name for file path %v missing", strings.TrimPrefix(source, "="))
68 case numSeparators == 1 && strings.HasSuffix(source, "="):
69 return "", "", fmt.Errorf("file path for key name %v missing", strings.TrimSuffix(source, "="))
70 case numSeparators > 1:
71 return "", "", errors.New("key names or file paths cannot contain '='")
72 default:
73 components := strings.Split(source, "=")
74 return components[0], components[1], nil
75 }
76 }
77
78
79
80
81 func ParseLiteralSource(source string) (keyName, value string, err error) {
82
83 if strings.Index(source, "=") == 0 {
84 return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source)
85 }
86
87 items := strings.SplitN(source, "=", 2)
88 if len(items) != 2 {
89 return "", "", fmt.Errorf("invalid literal source %v, expected key=value", source)
90 }
91
92 return items[0], items[1], nil
93 }
94
View as plain text