forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
122 lines (106 loc) · 2.88 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package describe
import (
"bytes"
"fmt"
"text/tabwriter"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/openshift/origin/pkg/api/latest"
buildapi "github.com/openshift/origin/pkg/build/api"
)
const emptyString = "<none>"
func tabbedString(f func(*tabwriter.Writer) error) (string, error) {
out := new(tabwriter.Writer)
b := make([]byte, 1024)
buf := bytes.NewBuffer(b)
out.Init(buf, 0, 8, 1, '\t', 0)
err := f(out)
if err != nil {
return "", err
}
out.Flush()
str := string(buf.String())
return str, nil
}
func toString(v interface{}) string {
value := fmt.Sprintf("%s", v)
if len(value) == 0 {
value = emptyString
}
return value
}
func bold(v interface{}) string {
return "\033[1m" + toString(v) + "\033[0m"
}
func convertEnv(env []api.EnvVar) map[string]string {
result := make(map[string]string, len(env))
for _, e := range env {
result[e.Name] = toString(e.Value)
}
return result
}
func formatString(out *tabwriter.Writer, label string, v interface{}) {
fmt.Fprintf(out, fmt.Sprintf("%s:\t%s\n", label, toString(v)))
}
func formatLabels(labelMap map[string]string) string {
return labels.Set(labelMap).String()
}
func extractAnnotations(annotations map[string]string, keys ...string) ([]string, map[string]string) {
extracted := make([]string, len(keys))
remaining := make(map[string]string)
for k, v := range annotations {
remaining[k] = v
}
for i, key := range keys {
extracted[i] = remaining[key]
delete(remaining, key)
}
return extracted, remaining
}
func formatAnnotations(out *tabwriter.Writer, m api.ObjectMeta, prefix string) {
values, annotations := extractAnnotations(m.Annotations, "description")
if len(values[0]) > 0 {
formatString(out, prefix+"Description", values[0])
}
if len(annotations) > 0 {
formatString(out, prefix+"Annotations", formatLabels(annotations))
}
}
func formatMeta(out *tabwriter.Writer, m api.ObjectMeta) {
formatString(out, "Name", m.Name)
if !m.CreationTimestamp.IsZero() {
formatString(out, "Created", m.CreationTimestamp)
}
formatString(out, "Labels", formatLabels(m.Labels))
formatAnnotations(out, m, "")
}
// webhookURL assembles map with of webhook type as key and webhook url and value
func webhookURL(c *buildapi.BuildConfig, configHost string) map[string]string {
result := map[string]string{}
for i, trigger := range c.Triggers {
whTrigger := ""
switch trigger.Type {
case "github":
whTrigger = trigger.GithubWebHook.Secret
case "generic":
whTrigger = trigger.GenericWebHook.Secret
}
if len(whTrigger) == 0 {
continue
}
apiVersion := latest.Version
host := "localhost"
if len(configHost) > 0 {
host = configHost
}
url := fmt.Sprintf("%s/osapi/%s/buildConfigHooks/%s/%s/%s",
host,
apiVersion,
c.Name,
whTrigger,
c.Triggers[i].Type,
)
result[string(trigger.Type)] = url
}
return result
}