From 50d86b26e49489cfaea82d35abb198a31cbcee64 Mon Sep 17 00:00:00 2001 From: Mya Pitzeruse Date: Sat, 21 May 2022 12:21:56 -0500 Subject: [PATCH] fix(metadata): allow repeated values and improve layout --- catalog/index.html.tpl | 96 ++++++++++++++++++++++-------------------- catalog/serve.go | 11 ++++- catalog/service/dsl.go | 25 ++++++++--- 3 files changed, 80 insertions(+), 52 deletions(-) diff --git a/catalog/index.html.tpl b/catalog/index.html.tpl index 054e4a6..a7df857 100644 --- a/catalog/index.html.tpl +++ b/catalog/index.html.tpl @@ -14,7 +14,8 @@ } div.catalog { - max-width: 800px; + max-width: 1600px; + min-width: 1200px; margin: 0 auto; } @@ -31,14 +32,14 @@ min-width: 50%; } - div.col-60 { - max-width: 60%; - min-width: 60%; + div.col-65 { + max-width: 65%; + min-width: 65%; } - div.col-40 { - max-width: 40%; - min-width: 40%; + div.col-35 { + max-width: 35%; + min-width: 35%; } div.catalog h1 { @@ -52,7 +53,7 @@ border: 1px solid #aaa; padding: 20px 26px; border-radius: 5px; - margin-top: 18px; + margin: 9px 18px; } div.catalog div.service img.logo { @@ -81,52 +82,55 @@
-

Service Catalog

- {{- range $service := .Services }} -
-
-
-
- {{- if $service.LogoURL }} - + {{- range $i, $service := .Services }} + {{- if $i | mod 2 | eq 0 }}
{{- end }} +
+
+
+
+
+ {{- if $service.LogoURL }} + + {{- end }} + +

{{ $service.Label }}

+
+ + {{- if $service.URL }} +

{{ $service.URL }}

{{- end }} -

{{ $service.Label }}

+ {{- if $service.Description }} +

{{ $service.Description }}

+ {{- end }} + + {{- range $kv := $service.Metadata }} + + {{- end }}
- {{- if $service.URL }} -

{{ $service.URL }}

- {{- end }} - - {{- if $service.Description }} -

{{ $service.Description }}

- {{- end }} - - {{- range $key, $value := $service.Metadata }} - - -
- {{- range $group := $service.LinkGroups }} - - {{- end }}
+ {{- if $i | mod 2 | eq 1 }}
{{- end }} {{- end }}
- \ No newline at end of file + diff --git a/catalog/serve.go b/catalog/serve.go index 4e5eb96..729aa35 100644 --- a/catalog/serve.go +++ b/catalog/serve.go @@ -40,7 +40,16 @@ func Serve(options ...Option) { start := time.Now() - t := template.Must(template.New("catalog").Parse(catalog)) + t := template.Must(template.New("catalog"). + Funcs(map[string]any{ + "mod": func(mod, v int) int { + return v % mod + }, + "eq": func(exp, act int) bool { + return exp == act + }, + }). + Parse(catalog)) spec := Spec{} for _, opt := range options { diff --git a/catalog/service/dsl.go b/catalog/service/dsl.go index e414d30..a5f6879 100644 --- a/catalog/service/dsl.go +++ b/catalog/service/dsl.go @@ -10,8 +10,7 @@ import ( // New constructs a spec given a label and set of options. func New(label string, options ...Option) Spec { spec := Spec{ - Label: label, - Metadata: make(map[string]string), + Label: label, } for _, opt := range options { @@ -24,13 +23,19 @@ func New(label string, options ...Option) Spec { // Option defines an optional component of the spec. type Option func(spec *Spec) +// KV defines a metadata entry. +type KV struct { + Key string + Value string +} + // Spec defines the elements needed to render a service. type Spec struct { Label string LogoURL string Description string URL string - Metadata map[string]string + Metadata []KV LinkGroups []linkgroup.Spec } @@ -56,9 +61,19 @@ func URL(url string) Option { } // Metadata allows additional metadata to be attached to a service. -func Metadata(key, value string) Option { +func Metadata(kvs ...string) Option { + // ensure even number of parameters + if len(kvs)%2 > 0 { + kvs = append(kvs, "") + } + return func(spec *Spec) { - spec.Metadata[key] = value + for i := 0; i < len(kvs); i += 2 { + spec.Metadata = append(spec.Metadata, KV{ + Key: kvs[i], + Value: kvs[i+1], + }) + } } }