Skip to content

Commit 5000ac0

Browse files
committed
feat: add version flag using build metadata
- Add a version command-line flag that prints the build version and exits - Use Go build metadata to report the application version, with a fallback when unavailable Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent 91c5ba2 commit 5000ac0

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"net/url"
1010
"os"
11+
"runtime/debug"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -37,6 +38,7 @@ var (
3738
flagScope *string
3839
flagTokenFile *string
3940
flagDevice *bool
41+
flagVersion *bool
4042
)
4143

4244
const (
@@ -82,6 +84,7 @@ func init() {
8284
false,
8385
"Force Device Code Flow (skip browser detection)",
8486
)
87+
flagVersion = flag.Bool("version", false, "Print version and exit")
8588
}
8689

8790
func initConfig() {
@@ -92,6 +95,16 @@ func initConfig() {
9295

9396
flag.Parse()
9497

98+
// --version prints build version and exits immediately.
99+
if *flagVersion {
100+
if build, ok := debug.ReadBuildInfo(); ok {
101+
fmt.Println(build.Main.Version)
102+
} else {
103+
fmt.Println("unknown version")
104+
}
105+
os.Exit(0)
106+
}
107+
95108
// --device forces Device Code Flow unconditionally.
96109
forceDevice = *flagDevice
97110

0 commit comments

Comments
 (0)