From 1eee12533d5efb931878756ab6ac0f0ab7f32ef8 Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Sat, 13 Jan 2024 16:03:32 +0100 Subject: [PATCH] Add a version command to print the version of toxstatus and sqlite --- .github/workflows/build.yaml | 7 ++++++ cmd/toxstatus/cmd/version.go | 45 ++++++++++++++++++++++++++++++++++++ flake.nix | 12 +++++++++- internal/version/version.go | 30 ++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 cmd/toxstatus/cmd/version.go create mode 100644 internal/version/version.go diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 769730b..05a1835 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -21,3 +21,10 @@ jobs: - name: Build run: | nix build --print-build-logs + - name: Check version number + if: startsWith(github.ref_name, 'v') + run: | + if ! ./result/bin/toxstatus version | grep -q '${{ github.ref_name }}'; then + echo "Version information doesn't match" + exit 1 + fi diff --git a/cmd/toxstatus/cmd/version.go b/cmd/toxstatus/cmd/version.go new file mode 100644 index 0000000..441290a --- /dev/null +++ b/cmd/toxstatus/cmd/version.go @@ -0,0 +1,45 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/Tox/ToxStatus/internal/version" + sqlite3 "github.com/mattn/go-sqlite3" + "github.com/spf13/cobra" +) + +var ( + versionCmd = &cobra.Command{ + Use: "version", + Short: "Version information", + Run: startVersion, + } +) + +func init() { + Root.AddCommand(versionCmd) +} + +func startVersion(cmd *cobra.Command, args []string) { + vs, err := version.String() + if err != nil { + exitWithError(err.Error()) + return + } + + sqliteVersion, _, _ := sqlite3.Version() + + fmt.Print(vs) + if ts := version.HumanRevisionTime(); ts != "" { + fmt.Printf(" (%s)", ts) + } + fmt.Println() + fmt.Printf("sqlite: %s \n", sqliteVersion) + fmt.Println("https://github.com/Tox/ToxStatus (GPLv3)") +} + +func exitWithError(s string) { + fmt.Fprintf(os.Stderr, "error: %s\n", s) + os.Exit(1) +} diff --git a/flake.nix b/flake.nix index 3bf5f91..3187607 100644 --- a/flake.nix +++ b/flake.nix @@ -6,11 +6,13 @@ outputs = { self, nixpkgs, flake-utils }: flake-utils.lib.eachDefaultSystem (system: let pkgs = nixpkgs.legacyPackages.${system}; + toxStatusVersion = "2.0.0-alpha1"; in { packages = flake-utils.lib.flattenTree rec { default = toxstatus; toxstatus = with pkgs; buildGoModule rec { - name = "toxstatus"; + pname = "toxstatus"; + version = toxStatusVersion; src = ./.; subPackages = [ "cmd/toxstatus" ]; @@ -18,6 +20,14 @@ tags = ["sqlite_foreign_keys"]; + ldflags = let + pkgPath = "github.com/Tox/ToxStatus/internal/version"; + in [ + "-X ${pkgPath}.Number=${version}" + "-X ${pkgPath}.Revision=${self.shortRev or "dirty"}" + "-X ${pkgPath}.RevisionTime=${toString self.lastModified}" + ]; + doCheck = false; }; }; diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..88b119b --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,30 @@ +package version + +import ( + "fmt" + "strconv" + "time" +) + +var ( + Number string + Revision string + RevisionTime string +) + +func String() (string, error) { + if Number == "" { + return "toxstatus: development build", nil + } + + return fmt.Sprintf("toxstatus: v%s-%s", Number, Revision), nil +} + +func HumanRevisionTime() string { + secs, err := strconv.ParseInt(RevisionTime, 10, 64) + if err != nil { + return "" + } + + return time.Unix(secs, 0).UTC().String() +}