"sq version" now honors format.datetime option (#330)

* sq version now honors config option format.date

* CHANGELOG update

* sq version now honors config option format.date
This commit is contained in:
Neil O'Toole 2023-11-19 20:28:09 -07:00 committed by GitHub
parent 2de993acaa
commit 0f9b5e2a75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 13 deletions

View File

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

View File

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

View File

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

View File

@ -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 = ""

View File

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