Skip to content

Commit

Permalink
Add a version command to print the version of toxstatus and sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbakker committed Jan 13, 2024
1 parent 472d3e6 commit 1eee125
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 45 additions & 0 deletions cmd/toxstatus/cmd/version.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 11 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,28 @@
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" ];
vendorHash = "sha256-cE8L7vuf3msMplL6BfQ4gt1rELMIFswDu9mfSXJ9VAs=";

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;
};
};
Expand Down
30 changes: 30 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 1eee125

Please sign in to comment.