Skip to content

Commit b466389

Browse files
committed
chore: move version printing to separate flag
Now version is printable through `--version/-v` flag at the root.
1 parent e40634c commit b466389

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

cmd/zigbee/firmware/root.go

-37
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package firmware
22

33
import (
4-
"errors"
5-
"log"
6-
"runtime/debug"
7-
"slices"
8-
94
"github.com/urfave/cli/v2"
105
)
116

@@ -23,37 +18,5 @@ func RootCmd() *cli.Command {
2318
Usage: "change the working directory for the build process (currently does not do anything)",
2419
},
2520
},
26-
Before: func(ctx *cli.Context) error {
27-
buildInfo, ok := debug.ReadBuildInfo()
28-
if !ok {
29-
return errors.New("could not read build information")
30-
}
31-
32-
vcsProps := make([]debug.BuildSetting, 0, 2)
33-
34-
for _, setting := range buildInfo.Settings {
35-
if !slices.Contains([]string{"vcs.revision", "vcs.modified"}, setting.Key) {
36-
continue
37-
}
38-
39-
vcsProps = append(vcsProps, setting)
40-
if len(vcsProps) == 3 {
41-
break
42-
}
43-
}
44-
45-
slices.SortFunc(vcsProps, func(a, b debug.BuildSetting) int {
46-
if a.Key > b.Key {
47-
return 1
48-
}
49-
50-
return -1
51-
})
52-
53-
// Information that will help with investigation of issues
54-
log.Printf("build info: %s/%s, %s", buildInfo.GoVersion, buildInfo.Main.Version, vcsProps)
55-
56-
return nil
57-
},
5821
}
5922
}

cmd/zigbee/main.go

+49
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"log"
67
"os"
8+
"runtime/debug"
9+
"slices"
710

811
"github.com/ffenix113/zigbee_home/cmd/zigbee/firmware"
912
"github.com/urfave/cli/v2"
@@ -23,6 +26,19 @@ func main() {
2326
Name: "config",
2427
Value: "zigbee.yaml",
2528
},
29+
&cli.BoolFlag{
30+
Name: "version",
31+
Aliases: []string{"v"},
32+
},
33+
},
34+
Action: func(ctx *cli.Context) error {
35+
if ctx.Bool("version") {
36+
return printVersion()
37+
}
38+
39+
cli.ShowAppHelpAndExit(ctx, 0)
40+
41+
return nil
2642
},
2743
}
2844

@@ -31,3 +47,36 @@ func main() {
3147
os.Exit(1)
3248
}
3349
}
50+
51+
func printVersion() error {
52+
buildInfo, ok := debug.ReadBuildInfo()
53+
if !ok {
54+
return errors.New("could not read build information")
55+
}
56+
57+
vcsProps := make([]debug.BuildSetting, 0, 2)
58+
59+
for _, setting := range buildInfo.Settings {
60+
if !slices.Contains([]string{"vcs.revision", "vcs.modified"}, setting.Key) {
61+
continue
62+
}
63+
64+
vcsProps = append(vcsProps, setting)
65+
if len(vcsProps) == cap(vcsProps) {
66+
break
67+
}
68+
}
69+
70+
slices.SortFunc(vcsProps, func(a, b debug.BuildSetting) int {
71+
if a.Key > b.Key {
72+
return 1
73+
}
74+
75+
return -1
76+
})
77+
78+
// Information that will help with investigation of
79+
log.Printf("%s, tag:%s, version:%s", buildInfo.GoVersion, buildInfo.Main.Version, vcsProps)
80+
81+
return nil
82+
}

0 commit comments

Comments
 (0)