fix(metadata): allow repeated values and improve layout

This commit is contained in:
Mya 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

@ -14,7 +14,8 @@
} }
div.catalog { div.catalog {
max-width: 800px; max-width: 1600px;
min-width: 1200px;
margin: 0 auto; margin: 0 auto;
} }
@ -31,14 +32,14 @@
min-width: 50%; min-width: 50%;
} }
div.col-60 { div.col-65 {
max-width: 60%; max-width: 65%;
min-width: 60%; min-width: 65%;
} }
div.col-40 { div.col-35 {
max-width: 40%; max-width: 35%;
min-width: 40%; min-width: 35%;
} }
div.catalog h1 { div.catalog h1 {
@ -52,7 +53,7 @@
border: 1px solid #aaa; border: 1px solid #aaa;
padding: 20px 26px; padding: 20px 26px;
border-radius: 5px; border-radius: 5px;
margin-top: 18px; margin: 9px 18px;
} }
div.catalog div.service img.logo { div.catalog div.service img.logo {
@ -81,51 +82,54 @@
<body> <body>
<div class="catalog"> <div class="catalog">
<h1>Service Catalog</h1> {{- range $i, $service := .Services }}
{{- range $service := .Services }} {{- if $i | mod 2 | eq 0 }}<div class="row">{{- end }}
<div class="service"> <div class="col col-50">
<div class="row"> <div class="service">
<div class="col col-60" style="padding-right: 10px"> <div class="row">
<div> <div class="col col-65" style="padding-right: 10px">
{{- if $service.LogoURL }} <div>
<img class="logo" src="{{ $service.LogoURL }}" /> {{- 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 }} {{- 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> </div>
{{- if $service.URL }} <div class="col col-35">
<p class="url"><a href="{{ $service.URL }}">{{ $service.URL }}</a></p> {{- range $group := $service.LinkGroups }}
{{- end }} <div class="link-group">
<h3 class="label">{{ $group.Label }}</h3>
{{- if $service.Description }} <ul>
<p class="description">{{ $service.Description }}</p> {{- range $link := $group.Links }}
{{- end }} <li class="link">
<a href="{{ $link.URL }}">{{ $link.Label }}</a>
{{- range $key, $value := $service.Metadata }} </li>
<div class="metadata row"> {{- end }}
<div class="col-50"><b>{{ $key }}</b></div> </ul>
<div class="col-50">{{ $value }}</div> </div>
{{- end }}
</div> </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> </div>
</div> </div>
{{- if $i | mod 2 | eq 1 }}</div>{{- end }}
{{- end }} {{- end }}
</div> </div>
</body> </body>

@ -40,7 +40,16 @@ func Serve(options ...Option) {
start := time.Now() 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{} spec := Spec{}
for _, opt := range options { for _, opt := range options {

@ -10,8 +10,7 @@ import (
// New constructs a spec given a label and set of options. // New constructs a spec given a label and set of options.
func New(label string, options ...Option) Spec { func New(label string, options ...Option) Spec {
spec := Spec{ spec := Spec{
Label: label, Label: label,
Metadata: make(map[string]string),
} }
for _, opt := range options { for _, opt := range options {
@ -24,13 +23,19 @@ func New(label string, options ...Option) Spec {
// Option defines an optional component of the spec. // Option defines an optional component of the spec.
type Option func(spec *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. // Spec defines the elements needed to render a service.
type Spec struct { type Spec struct {
Label string Label string
LogoURL string LogoURL string
Description string Description string
URL string URL string
Metadata map[string]string Metadata []KV
LinkGroups []linkgroup.Spec LinkGroups []linkgroup.Spec
} }
@ -56,9 +61,19 @@ func URL(url string) Option {
} }
// Metadata allows additional metadata to be attached to a service. // 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) { 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],
})
}
} }
} }