Skip to content

Commit b9f4821

Browse files
authored
Merge pull request #191 from nix-community/version-flag
add version flag
2 parents baeaf37 + 89d722e commit b9f4821

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

cmd/root.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import (
1111

1212
"github.com/numtide/nixos-facter/pkg/facter"
1313
"github.com/numtide/nixos-facter/pkg/hwinfo"
14+
"github.com/numtide/nixos-facter/pkg/build"
1415
)
1516

1617
var (
1718
outputPath string
1819
logLevel string
1920
hardwareFeatures []string
21+
version bool
2022

2123
scanner = facter.Scanner{}
2224
)
@@ -30,6 +32,10 @@ func init() {
3032
&scanner.Ephemeral, "ephemeral", false,
3133
"capture all ephemeral properties e.g. swap, filesystems and so on",
3234
)
35+
flag.BoolVar(
36+
&version, "version", false,
37+
"print version and exit",
38+
)
3339
flag.StringVar(&logLevel, "log-level", "info", "log level")
3440

3541
defaultFeatures := []string{
@@ -56,7 +62,7 @@ func init() {
5662
defaultValues := strings.Join(defaultFeatures, ",")
5763

5864
const usage = `nixos-facter [flags]
59-
Hardware report generator
65+
Hardware report generator %s (%s)
6066
6167
Usage:
6268
nixos-facter [flags]
@@ -66,6 +72,7 @@ Flags:
6672
-h, --help help for nixos-facter
6773
-o, --output string path to write the report
6874
--swap capture swap entries
75+
--version version for nixos-facter
6976
--log-level string log level, one of <error|warn|info|debug> (default "info")
7077
--hardware strings Hardware items to probe.
7178
Default: %s
@@ -74,12 +81,17 @@ Flags:
7481
`
7582

7683
// Custom usage function
77-
flag.Usage = func() { fmt.Fprintf(os.Stderr, usage, defaultValues, possibleValues) }
84+
flag.Usage = func() { fmt.Fprintf(os.Stderr, usage, build.Version, build.System, defaultValues, possibleValues) }
7885
}
7986

8087
func Execute() {
8188
flag.Parse()
8289

90+
if version {
91+
fmt.Printf("%s\n", build.Version)
92+
return
93+
}
94+
8395
// Check if the effective user id is 0 e.g. root
8496
if os.Geteuid() != 0 {
8597
log.Fatalf("you must run this program as root")

nix/packages/nixos-facter/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ in
1919
# there's no good way of tying in the version to a git tag or branch
2020
# so for simplicity's sake we set the version as the commit revision hash
2121
# we remove the `-dirty` suffix to avoid a lot of unnecessary rebuilds in local dev
22-
version = lib.removeSuffix "-dirty" (flake.shortRev or flake.dirtyShortRev);
22+
versionSuffix = "-${lib.removeSuffix "-dirty" (flake.shortRev or flake.dirtyShortRev)}";
2323
}
2424
// {
2525
passthru.tests = import ./tests args;

nix/packages/nixos-facter/package.nix

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
{
22
lib,
3-
version ? "0.3.1",
3+
versionSuffix ? null,
44
systemdMinimal,
55
hwinfo,
66
gcc,
77
makeWrapper,
88
pkg-config,
99
stdenv,
1010
buildGo124Module,
11+
versionCheckHook,
1112
}: let
1213
fs = lib.fileset;
1314
in
1415
buildGo124Module (final: {
1516
pname = "nixos-facter";
16-
inherit version;
17+
version = "0.3.2";
1718

1819
src = fs.toSource {
1920
root = ../../..;
@@ -29,24 +30,26 @@ in
2930
vendorHash = "sha256-A7ZuY8Gc/a0Y8O6UG2WHWxptHstJOxi4n9F8TY6zqiw=";
3031

3132
buildInputs = [
32-
systemdMinimal.dev
33+
systemdMinimal
3334
hwinfo
3435
];
3536

3637
nativeBuildInputs = [
3738
gcc
3839
makeWrapper
3940
pkg-config
41+
versionCheckHook
4042
];
4143

4244
ldflags = [
4345
"-s"
4446
"-w"
4547
"-X github.com/numtide/nixos-facter/pkg/build.Name=${final.pname}"
46-
"-X github.com/numtide/nixos-facter/pkg/build.Version=v${final.version}"
48+
"-X github.com/numtide/nixos-facter/pkg/build.Version=v${final.version}${toString versionSuffix}"
4749
"-X github.com/numtide/nixos-facter/pkg/build.System=${stdenv.hostPlatform.system}"
4850
];
4951

52+
doInstallCheck = true;
5053
postInstall = let
5154
binPath = lib.makeBinPath [
5255
systemdMinimal

nix/packages/nixos-facter/tests/default.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ in
2424
installPhase = ''
2525
touch $out
2626
'';
27+
doInstallCheck = false;
28+
doCheck = false;
2729
});
2830

2931
basic = diskoLib.testLib.makeDiskoTest {

scripts/create-release.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ if [[ $unpushed_commits != "" ]]; then
2828
echo -e "\nThere are unpushed changes, exiting:\n$unpushed_commits" >&2
2929
exit 1
3030
fi
31+
sed -i "s/version = \".*\"/version = \"$version\"/" ./nix/packages/nixos-facter/default.nix
32+
git add ./nix/packages/nixos-facter/default.nix
33+
git commit -m "release: v${version}"
3134
nix flake check -vL
3235
git tag "v${version}"
3336

0 commit comments

Comments
 (0)