2022-12-30 20:10:56 +03:00
|
|
|
package tablew
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"golang.org/x/mod/semver"
|
2023-06-21 15:28:15 +03:00
|
|
|
|
2022-12-30 20:10:56 +03:00
|
|
|
"github.com/neilotoole/sq/cli/buildinfo"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/cli/hostinfo"
|
2022-12-30 20:10:56 +03:00
|
|
|
"github.com/neilotoole/sq/cli/output"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-21 15:28:15 +03:00
|
|
|
// Version implements output.VersionWriter.
|
|
|
|
func (w *versionWriter) Version(bi buildinfo.BuildInfo, latestVersion string, hi hostinfo.Info) error {
|
|
|
|
var newerAvailable bool
|
2022-12-30 20:10:56 +03:00
|
|
|
|
2023-06-21 15:28:15 +03:00
|
|
|
if latestVersion != "" {
|
|
|
|
newerAvailable = semver.Compare(latestVersion, bi.Version) > 0
|
|
|
|
}
|
2022-12-30 20:59:27 +03:00
|
|
|
|
2023-06-21 15:28:15 +03:00
|
|
|
if !w.pr.Verbose {
|
|
|
|
fmt.Fprintf(w.out, "sq %s", bi.Version)
|
|
|
|
|
|
|
|
if newerAvailable {
|
2022-12-30 20:59:27 +03:00
|
|
|
fmt.Fprint(w.out, " ")
|
2023-08-20 17:50:24 +03:00
|
|
|
w.pr.Faint.Fprint(w.out, "Update available: "+latestVersion)
|
2022-12-30 20:59:27 +03:00
|
|
|
}
|
2023-08-20 17:50:24 +03:00
|
|
|
|
|
|
|
fmt.Fprintln(w.out)
|
2023-06-21 15:28:15 +03:00
|
|
|
return nil
|
2022-12-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
|
2023-06-21 15:28:15 +03:00
|
|
|
fmt.Fprintf(w.out, "sq %s\n", bi.Version)
|
|
|
|
|
|
|
|
w.pr.Faint.Fprintf(w.out, "Version: %s\n", bi.Version)
|
|
|
|
|
|
|
|
if bi.Commit != "" {
|
|
|
|
w.pr.Faint.Fprintf(w.out, "Commit: #%s\n", bi.Commit)
|
2022-12-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
|
2023-11-20 06:28:09 +03:00
|
|
|
if !bi.Timestamp.IsZero() {
|
|
|
|
w.pr.Faint.Fprintf(w.out, "Timestamp: %s\n", w.pr.FormatDatetime(bi.Timestamp))
|
2023-05-07 05:36:34 +03:00
|
|
|
}
|
2023-06-21 15:28:15 +03:00
|
|
|
|
|
|
|
// latestVersion = ""
|
|
|
|
w.pr.Faint.Fprint(w.out, "Latest version: ")
|
|
|
|
if latestVersion == "" {
|
|
|
|
w.pr.Error.Fprintf(w.out, "unknown\n")
|
|
|
|
} else {
|
|
|
|
if newerAvailable {
|
|
|
|
w.pr.Hilite.Fprintln(w.out, latestVersion)
|
|
|
|
} else {
|
|
|
|
w.pr.Faint.Fprintln(w.out, latestVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.pr.Faint.Fprintf(w.out, "Host: %s %s | %s %s | %s %s\n",
|
|
|
|
hi.Platform, hi.Arch, hi.Kernel, hi.KernelVersion, hi.Variant, hi.VariantVersion)
|
|
|
|
|
|
|
|
// Follow GNU standards (mostly)
|
|
|
|
// https://www.gnu.org/prep/standards/html_node/_002d_002dversion.html#g_t_002d_002dversion
|
|
|
|
const notice = `MIT License: https://opensource.org/license/mit
|
|
|
|
Website: https://sq.io
|
|
|
|
Source code: https://github.com/neilotoole/sq
|
|
|
|
Notice: Copyright (c) 2023 Neil O'Toole`
|
|
|
|
w.pr.Faint.Fprintln(w.out, notice)
|
|
|
|
|
2022-12-30 20:10:56 +03:00
|
|
|
return nil
|
|
|
|
}
|