2022-12-30 20:10:56 +03:00
|
|
|
package tablew
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/cli/buildinfo"
|
|
|
|
"github.com/neilotoole/sq/cli/output"
|
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ output.VersionWriter = (*versionWriter)(nil)
|
|
|
|
|
2023-04-19 08:28:09 +03:00
|
|
|
// versionWriter implements output.VersionWriter for text.
|
2022-12-30 20:10:56 +03:00
|
|
|
type versionWriter struct {
|
|
|
|
out io.Writer
|
2023-04-22 06:36:32 +03:00
|
|
|
pr *output.Printing
|
2022-12-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewVersionWriter returns a new output.VersionWriter instance
|
2023-04-19 08:28:09 +03:00
|
|
|
// that outputs version info in text.
|
2023-04-22 06:36:32 +03:00
|
|
|
func NewVersionWriter(out io.Writer, pr *output.Printing) output.VersionWriter {
|
|
|
|
return &versionWriter{out: out, pr: pr}
|
2022-12-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *versionWriter) Version(bi buildinfo.BuildInfo, latestVersion string) error {
|
|
|
|
fmt.Fprintf(w.out, "sq %s", bi.Version)
|
|
|
|
|
2023-04-22 06:36:32 +03:00
|
|
|
if w.pr.Verbose {
|
2022-12-30 20:59:27 +03:00
|
|
|
if len(bi.Commit) > 0 {
|
|
|
|
fmt.Fprint(w.out, " ")
|
2023-04-22 06:36:32 +03:00
|
|
|
w.pr.Faint.Fprint(w.out, "#"+bi.Commit)
|
2022-12-30 20:59:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(bi.Timestamp) > 0 {
|
|
|
|
fmt.Fprint(w.out, " ")
|
2023-04-22 06:36:32 +03:00
|
|
|
w.pr.Faint.Fprint(w.out, bi.Timestamp)
|
2022-12-30 20:59:27 +03:00
|
|
|
}
|
2022-12-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
showUpdate := semver.Compare(latestVersion, bi.Version) > 0
|
|
|
|
if showUpdate {
|
|
|
|
fmt.Fprint(w.out, " ")
|
2023-04-22 06:36:32 +03:00
|
|
|
w.pr.Faint.Fprint(w.out, "Update available: "+latestVersion)
|
2022-12-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(w.out)
|
2023-05-07 05:36:34 +03:00
|
|
|
|
|
|
|
if w.pr.Verbose {
|
|
|
|
// Follow GNU standards (mostly)
|
|
|
|
// https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion
|
|
|
|
const notice = `
|
|
|
|
Copyright (c) 2023 Neil O'Toole
|
|
|
|
MIT License: https://opensource.org/license/mit
|
|
|
|
Website: https://sq.io
|
|
|
|
Source code: https://github.com/neilotoole/sq`
|
|
|
|
w.pr.Faint.Fprintln(w.out, notice)
|
|
|
|
}
|
2022-12-30 20:10:56 +03:00
|
|
|
return nil
|
|
|
|
}
|