From 8b92fbf0062e1b4d6b579f4918bc67929925676e Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sat, 18 May 2024 23:10:05 -0600 Subject: imworkingon: Add Gerrit last-updated detection --- cmd/generate/gerrit.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 cmd/generate/gerrit.go (limited to 'cmd/generate/gerrit.go') diff --git a/cmd/generate/gerrit.go b/cmd/generate/gerrit.go new file mode 100644 index 0000000..c8837fc --- /dev/null +++ b/cmd/generate/gerrit.go @@ -0,0 +1,54 @@ +package main + +import ( + "encoding" + "encoding/json" + "fmt" + "strings" + "time" +) + +// httpGetGerritJSON is like [httpGetJSON], but +// https://gerrit-review.googlesource.com/Documentation/rest-api.html#output +func httpGetGerritJSON(u string, out any) error { + str, err := httpGet(u) + if err != nil { + return err + } + if _, body, ok := strings.Cut(str, "\n"); ok { + str = body + } + return json.Unmarshal([]byte(str), out) +} + +const GerritTimeFormat = "2006-01-02 15:04:05.000000000" + +type GerritTime struct { + Val time.Time +} + +var ( + _ fmt.Stringer = GerritTime{} + _ encoding.TextMarshaler = GerritTime{} + _ encoding.TextUnmarshaler = (*GerritTime)(nil) +) + +// String implements [fmt.Stringer]. +func (t GerritTime) String() string { + return t.Val.Format(GerritTimeFormat) +} + +// MarshalText implements [encoding.TextMarshaler]. +func (t GerritTime) MarshalText() ([]byte, error) { + return []byte(t.String()), nil +} + +// UnmarshalText implements [encoding.TextUnmarshaler]. +func (t *GerritTime) UnmarshalText(data []byte) error { + val, err := time.Parse(GerritTimeFormat, string(data)) + if err != nil { + return err + } + t.Val = val + return nil +} -- cgit v1.2.3-2-g168b