Browse Source

improvements to templating

sungo 2 months ago
parent
commit
e993a584ca
12 changed files with 42 additions and 44 deletions
  1. 1 2
      datacenter.go
  2. 4 5
      devices.go
  3. 0 1
      hardware.go
  4. 8 0
      main.go
  5. 1 2
      racks.go
  6. 1 2
      relays.go
  7. 1 2
      roles.go
  8. 1 2
      rooms.go
  9. 20 20
      templates.go
  10. 1 2
      user.go
  11. 1 2
      validations.go
  12. 3 4
      workspaces.go

+ 1 - 2
datacenter.go

@@ -14,7 +14,6 @@ import (
 	"fmt"
 	"net/url"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -45,7 +44,7 @@ func (d Datacenter) String() string {
 		return API.AsJSON(d)
 	}
 
-	t, err := template.New("d").Parse(datacenterTemplate)
+	t, err := NewTemplate().Parse(datacenterTemplate)
 	if err != nil {
 		panic(err)
 	}

+ 4 - 5
devices.go

@@ -14,7 +14,6 @@ import (
 	"regexp"
 	"sort"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -191,7 +190,7 @@ func (dl DeviceLocation) String() string {
 		return API.AsJSON(dl)
 	}
 
-	t, err := template.New("d").Parse(deviceLocationTemplate)
+	t, err := NewTemplate().Parse(deviceLocationTemplate)
 	if err != nil {
 		panic(err)
 	}
@@ -234,7 +233,7 @@ func (dn DeviceNic) String() string {
 		return API.AsJSON(dn)
 	}
 
-	t, err := template.New("d").Parse(deviceNicTemplate)
+	t, err := NewTemplate().Parse(deviceNicTemplate)
 	if err != nil {
 		panic(err)
 	}
@@ -359,7 +358,7 @@ func (d DetailedDevice) String() string {
 		Validations     ValidationStatesWithResults
 	}{d, rackRole, hp, enclosures, validations}
 
-	t, err := template.New("d").Parse(deviceTemplate)
+	t, err := NewTemplate().Parse(deviceTemplate)
 	if err != nil {
 		panic(err)
 	}
@@ -595,7 +594,7 @@ func init() {
 		idArg := cmd.StringArg(
 			"DEVICE",
 			"",
-			"UUID or serial number of the device",
+			"UUID or serial number of the device. Short UUIDs are *not* accepted",
 		)
 
 		cmd.Spec = "DEVICE"

+ 0 - 1
hardware.go

@@ -16,7 +16,6 @@ import (
 	// "sort"
 	// "strconv"
 	// "strings"
-	// "text/template"
 	"time"
 
 	"github.com/gofrs/uuid"

+ 8 - 0
main.go

@@ -14,6 +14,7 @@ import (
 	"os/user"
 	"regexp"
 	"runtime"
+	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -104,6 +105,13 @@ func RequireSysAdmin() {
 	}
 }
 
+func NewTemplate() *template.Template {
+	return template.New("wat").Funcs(template.FuncMap{
+		"CutUUID": func(id string) string { return CutUUID(id) },
+		"TimeStr": func(t time.Time) string { return TimeStr(t) },
+	})
+}
+
 /***/
 
 func init() {

+ 1 - 2
racks.go

@@ -19,7 +19,6 @@ import (
 	"sort"
 	"strconv"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -106,7 +105,7 @@ func (r Rack) String() string {
 		return API.AsJSON(r)
 	}
 
-	t, err := template.New("r").Parse(rackTemplate)
+	t, err := NewTemplate().Parse(rackTemplate)
 	if err != nil {
 		panic(err)
 	}

+ 1 - 2
relays.go

@@ -14,7 +14,6 @@ import (
 	"sort"
 	"strconv"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -95,7 +94,7 @@ func (r Relay) String() string {
 		return API.AsJSON(r)
 	}
 
-	t, err := template.New("r").Parse(relayTemplate)
+	t, err := NewTemplate().Parse(relayTemplate)
 	if err != nil {
 		panic(err)
 	}

+ 1 - 2
roles.go

@@ -16,7 +16,6 @@ import (
 	"sort"
 	"strconv"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -94,7 +93,7 @@ func (r RackRole) String() string {
 		return API.AsJSON(r)
 	}
 
-	t, err := template.New("r").Parse(rackRoleTemplate)
+	t, err := NewTemplate().Parse(rackRoleTemplate)
 	if err != nil {
 		panic(err)
 	}

+ 1 - 2
rooms.go

@@ -15,7 +15,6 @@ import (
 	"net/url"
 	"sort"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -99,7 +98,7 @@ func (r Room) String() string {
 		return API.AsJSON(r)
 	}
 
-	t, err := template.New("r").Parse(roomTemplate)
+	t, err := NewTemplate().Parse(roomTemplate)
 	if err != nil {
 		panic(err)
 	}

+ 20 - 20
templates.go

@@ -8,8 +8,8 @@ package main
 
 const validationStatesWithResultsTemplate = `{{ range . }}
 - ID: {{ .ID }}
-  Created: {{ .Created }}
-  Completed: {{ .Completed }}
+  Created: {{ TimeStr .Created }}
+  Completed: {{ TimeStr .Completed }}
   Status: {{ .Status }}
   Validation Plan: {{ .ValidationPlan.Name }}{{ if len .Results }}
 
@@ -29,9 +29,9 @@ Phase: {{ .Phase }}
 Health: {{ .Health }}
 Validated: {{ if not $.Validated.IsZero }}{{ .Validated.Local }}{{ end }}
 
-Created:   {{ .Created.Local }}
-Updated:   {{ .Updated.Local }}
-Last Seen: {{ .LastSeen.Local }}{{ if .Links }}
+Created:   {{ TimeStr .Created }}
+Updated:   {{ TimeStr .Updated }}
+Last Seen: {{ TimeStr .LastSeen }}{{ if .Links }}
 
 Links: {{ range .Links }}
   - {{ $ }}
@@ -93,10 +93,10 @@ const workspaceRelayTemplate = `
 ID: {{ .ID }}
 Name: {{ .Alias }}
 Version: {{ .Version }}
-Created: {{ .Created.Local }}
-Updated: {{ .Updated.Local }}
+Created: {{ TimeStr .Created }}
+Updated: {{ TimeStr .Updated }}
 
-Last Seen: {{ .LastSeen.Local }}
+Last Seen: {{ TimeStr .LastSeen }}
 
 IP Address: {{ .IpAddr }}
 SSH Port: {{ .SshPort }}
@@ -113,8 +113,8 @@ ID: {{ .ID }}
 Serial Number: {{ .SerialNumber }}
 Name: {{ .Name }}
 Version: {{ .Version }}
-Created: {{ .Created.Local }}
-Updated: {{ .Updated.Local }}
+Created: {{ TimeStr .Created }}
+Updated: {{ TimeStr .Updated }}
 
 IP Address: {{ .IpAddr }}
 SSH Port: {{ .SshPort }}
@@ -144,8 +144,8 @@ Name: {{ .Name }}
 Email: {{ .Email }}
 System Admin: {{ if $.IsAdmin }}Yes{{ else }}No{{ end }}
 
-Created: {{ .Created.Local }}
-Last Login: {{ if $.LastLogin.IsZero }}Never/Unknown{{ else }}{{ .LastLogin.Local }}{{ end }}
+Created: {{ TimeStr .Created }}
+Last Login: {{ if $.LastLogin.IsZero }}Never/Unknown{{ else }}{{ TimeStr .LastLogin }}{{ end }}
 
 
 Workspaces:
@@ -159,8 +159,8 @@ Vendor Name: {{ .VendorName }}
 Region: {{ .Region }}
 Location: {{ .Location }}
 
-Created: {{ .Created.Local }}
-Updated: {{ .Updated.Local }}
+Created: {{ TimeStr .Created }}
+Updated: {{ TimeStr .Updated }}
 `
 
 const roomTemplate = `
@@ -169,16 +169,16 @@ AZ: {{ .AZ }}
 Vendor Name: {{ .VendorName }}
 Datacenter ID: {{ .DatacenterID }}
 
-Created: {{ .Created.Local }}
-Updated: {{ .Updated.Local }}
+Created: {{ TimeStr .Created }}
+Updated: {{ TimeStr .Updated }}
 `
 
 const rackRoleTemplate = `
 Name: {{ .Name }}
 Rack Size: {{ .RackSize }}
 
-Created: {{ .Created.Local }}
-Updated: {{ .Updated.Local }}
+Created: {{ TimeStr .Created }}
+Updated: {{ TimeStr .Updated }}
 `
 
 const rackTemplate = `
@@ -190,8 +190,8 @@ Phase: {{ .Phase }}
 Role: {{ .Role.Name }}
 Room: {{ .Room.Alias }}
 
-Created: {{ .Created.Local }}
-Updated: {{ .Updated.Local }}
+Created: {{ TimeStr .Created }}
+Updated: {{ TimeStr .Updated }}
 `
 
 const deviceLocationTemplate = `

+ 1 - 2
user.go

@@ -14,7 +14,6 @@ import (
 	"net/url"
 	"sort"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -51,7 +50,7 @@ func (u DetailedUser) String() string {
 		return API.AsJSON(u)
 	}
 
-	t, err := template.New("u").Parse(detailedUserTemplate)
+	t, err := NewTemplate().Parse(detailedUserTemplate)
 	if err != nil {
 		panic(err)
 	}

+ 1 - 2
validations.go

@@ -12,7 +12,6 @@ import (
 	"net/url"
 	"sort"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -161,7 +160,7 @@ func (vs ValidationStatesWithResults) String() string {
 		})
 	}
 
-	t, err := template.New("v").Parse(validationStatesWithResultsTemplate)
+	t, err := NewTemplate().Parse(validationStatesWithResultsTemplate)
 	if err != nil {
 		panic(err)
 	}

+ 3 - 4
workspaces.go

@@ -14,7 +14,6 @@ import (
 	"sort"
 	"strconv"
 	"strings"
-	"text/template"
 	"time"
 
 	"github.com/gofrs/uuid"
@@ -67,7 +66,7 @@ func (w WorkspaceAndRole) String() string {
 		return API.AsJSON(w)
 	}
 
-	t, err := template.New("w").Parse(workspaceTemplate)
+	t, err := NewTemplate().Parse(workspaceTemplate)
 	if err != nil {
 		panic(err)
 	}
@@ -546,7 +545,7 @@ func (summaries WorkspaceRackSummaries) String() string {
 				Statuses []*status
 			}{summary, az, statusii}
 
-			t, err := template.New("r").Parse(rackSummaryTemplate)
+			t, err := NewTemplate().Parse(rackSummaryTemplate)
 			if err != nil {
 				panic(err)
 			}
@@ -608,7 +607,7 @@ func (w WorkspaceRelay) String() string {
 		return API.AsJSON(w)
 	}
 
-	t, err := template.New("r").Parse(workspaceRelayTemplate)
+	t, err := NewTemplate().Parse(workspaceRelayTemplate)
 	if err != nil {
 		panic(err)
 	}