fix(metadata): allow repeated values and improve layout

This commit is contained in:
Mya Pitzeruse 2022-05-21 12:21:56 -05:00
parent 5cb2bfb760
commit 50d86b26e4
No known key found for this signature in database
GPG Key ID: C3ECFA648DAD27FA
3 changed files with 80 additions and 52 deletions

View File

@ -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 @@
<body>
<div class="catalog">
<h1>Service Catalog</h1>
{{- range $service := .Services }}
<div class="service">
<div class="row">
<div class="col col-60" style="padding-right: 10px">
<div>
{{- if $service.LogoURL }}
<img class="logo" src="{{ $service.LogoURL }}" />
{{- range $i, $service := .Services }}
{{- if $i | mod 2 | eq 0 }}<div class="row">{{- end }}
<div class="col col-50">
<div class="service">
<div class="row">
<div class="col col-65" style="padding-right: 10px">
<div>
{{- if $service.LogoURL }}
<img class="logo" src="{{ $service.LogoURL }}" />
{{- end }}
<h2 class="name">{{ $service.Label }}</h2>
</div>
{{- if $service.URL }}
<p class="url"><a href="{{ $service.URL }}">{{ $service.URL }}</a></p>
{{- end }}
<h2 class="name">{{ $service.Label }}</h2>
{{- if $service.Description }}
<p class="description">{{ $service.Description }}</p>
{{- end }}
{{- range $kv := $service.Metadata }}
<div class="metadata row">
<div class="col col-35"><b>{{ $kv.Key }}</b></div>
<div class="col col-65">{{ $kv.Value }}</div>
</div>
{{- end }}
</div>
{{- if $service.URL }}
<p class="url"><a href="{{ $service.URL }}">{{ $service.URL }}</a></p>
{{- end }}
{{- if $service.Description }}
<p class="description">{{ $service.Description }}</p>
{{- end }}
{{- range $key, $value := $service.Metadata }}
<div class="metadata row">
<div class="col-50"><b>{{ $key }}</b></div>
<div class="col-50">{{ $value }}</div>
<div class="col col-35">
{{- range $group := $service.LinkGroups }}
<div class="link-group">
<h3 class="label">{{ $group.Label }}</h3>
<ul>
{{- range $link := $group.Links }}
<li class="link">
<a href="{{ $link.URL }}">{{ $link.Label }}</a>
</li>
{{- end }}
</ul>
</div>
{{- end }}
</div>
{{- end }}
</div>
<div class="col col-40">
{{- range $group := $service.LinkGroups }}
<div class="link-group">
<h3 class="label">{{ $group.Label }}</h3>
<ul>
{{- range $link := $group.Links }}
<li class="link">
<a href="{{ $link.URL }}">{{ $link.Label }}</a>
</li>
{{- end }}
</ul>
</div>
{{- end }}
</div>
</div>
</div>
{{- if $i | mod 2 | eq 1 }}</div>{{- end }}
{{- end }}
</div>
</body>
</html>
</html>

View File

@ -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 {

View File

@ -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],
})
}
}
}