diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cd7d251..9d14025d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Breaking changes are annotated with ☢️, and alpha/beta features with 🐥. +## Upcoming + +### Fixed + +- [`sq version`](https://sq.io/docs/cmd/version) now honors option + [`format.datetime`](https://sq.io/docs/config#formatdatetime) when outputting build timestamp. + ## [v0.43.1] - 2023-11-19 ### Added diff --git a/cli/buildinfo/buildinfo.go b/cli/buildinfo/buildinfo.go index bd4042f0..29a4730c 100644 --- a/cli/buildinfo/buildinfo.go +++ b/cli/buildinfo/buildinfo.go @@ -9,6 +9,7 @@ import ( "log/slog" "os" "strings" + "time" "golang.org/x/mod/semver" @@ -34,9 +35,9 @@ var ( // BuildInfo encapsulates Version, Commit and Timestamp. type BuildInfo struct { - Version string `json:"version" yaml:"version"` - Commit string `json:"commit,omitempty" yaml:"commit,omitempty"` - Timestamp string `json:"timestamp,omitempty" yaml:"timestamp,omitempty"` + Version string `json:"version" yaml:"version"` + Commit string `json:"commit,omitempty" yaml:"commit,omitempty"` + Timestamp time.Time `json:"timestamp,omitempty" yaml:"timestamp,omitempty"` } // String returns a string representation of BuildInfo. @@ -45,8 +46,8 @@ func (bi BuildInfo) String() string { if bi.Commit != "" { s += " " + bi.Commit } - if bi.Timestamp != "" { - s += " " + bi.Timestamp + if !bi.Timestamp.IsZero() { + s += " " + bi.Timestamp.Format(timez.RFC3339Z) } return s } @@ -56,17 +57,26 @@ func (bi BuildInfo) LogValue() slog.Value { gv := slog.GroupValue( slog.String(lga.Version, bi.Version), slog.String(lga.Commit, bi.Commit), - slog.String(lga.Timestamp, bi.Timestamp)) + slog.Time(lga.Timestamp, bi.Timestamp)) return gv } -// Get returns BuildInfo. +// Get returns BuildInfo. If buildinfo.Timestamp cannot be parsed, +// the returned BuildInfo.Timestamp will be the zero value. func Get() BuildInfo { + var t time.Time + if Timestamp != "" { + got, err := timez.ParseTimestampUTC(Timestamp) + if err == nil { + t = got + } + } + return BuildInfo{ Version: Version, Commit: Commit, - Timestamp: Timestamp, + Timestamp: t, } } diff --git a/cli/output/jsonw/versionwriter.go b/cli/output/jsonw/versionwriter.go index bc1a985f..ac92a732 100644 --- a/cli/output/jsonw/versionwriter.go +++ b/cli/output/jsonw/versionwriter.go @@ -24,17 +24,26 @@ func NewVersionWriter(out io.Writer, pr *output.Printing) output.VersionWriter { // Version implements output.VersionWriter. func (w *versionWriter) Version(bi buildinfo.BuildInfo, latestVersion string, hi hostinfo.Info) error { + // We use a custom struct so that we can + // control the timestamp format. type cliBuildInfo struct { - buildinfo.BuildInfo + Version string `json:"version" yaml:"version"` + Commit string `json:"commit,omitempty" yaml:"commit,omitempty"` + Timestamp string `json:"timestamp,omitempty" yaml:"timestamp,omitempty"` LatestVersion string `json:"latest_version"` Host hostinfo.Info `json:"host"` } cbi := cliBuildInfo{ - BuildInfo: bi, + Version: bi.Version, + Commit: bi.Commit, LatestVersion: latestVersion, Host: hi, } + if !bi.Timestamp.IsZero() { + cbi.Timestamp = w.pr.FormatDatetime(bi.Timestamp) + } + return writeJSON(w.out, w.pr, cbi) } diff --git a/cli/output/tablew/versionwriter.go b/cli/output/tablew/versionwriter.go index 8926aa7a..98a75323 100644 --- a/cli/output/tablew/versionwriter.go +++ b/cli/output/tablew/versionwriter.go @@ -53,8 +53,8 @@ func (w *versionWriter) Version(bi buildinfo.BuildInfo, latestVersion string, hi w.pr.Faint.Fprintf(w.out, "Commit: #%s\n", bi.Commit) } - if bi.Timestamp != "" { - w.pr.Faint.Fprintf(w.out, "Timestamp: %s\n", bi.Timestamp) + if !bi.Timestamp.IsZero() { + w.pr.Faint.Fprintf(w.out, "Timestamp: %s\n", w.pr.FormatDatetime(bi.Timestamp)) } // latestVersion = "" diff --git a/cli/output/yamlw/versionwriter.go b/cli/output/yamlw/versionwriter.go index 349b69e6..26fe2fff 100644 --- a/cli/output/yamlw/versionwriter.go +++ b/cli/output/yamlw/versionwriter.go @@ -38,7 +38,7 @@ func (w *versionWriter) Version(bi buildinfo.BuildInfo, latestVersion string, hi cbi := cliBuildInfo{ Version: bi.Version, Commit: bi.Commit, - Timestamp: bi.Timestamp, + Timestamp: w.pr.FormatDatetime(bi.Timestamp), LatestVersion: latestVersion, Host: hi, }