WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 5b0aabc

Browse files
authored
Add --version flag to print build info (#411)
## Summary Adds a `--version` flag to the genqlient CLI that displays version information including commit hash and build date. ## Changes - Added `--version` flag to `generate/cliArgs` struct - Implemented `printVersion()` function that uses `runtime/debug.BuildInfo` to extract: - Module version (or "dev" for development builds) - Git commit hash (shortened to 12 characters) - Build timestamp - Updated changelog with new feature entry ## Example Output For development builds: ``` genqlient dev (8ddeeee, built 2025-10-28T05:47:17Z) ``` For release builds: ``` genqlient v1.2.3 (abc123def456, built 2025-10-28T05:47:17Z) ``` ## Testing - ✅ All existing tests pass - ✅ Lint passes (`make lint`) - ✅ Manually tested with `go run . --version` and `go build` + `./genqlient --version` - ✅ Help output correctly shows the new flag ## Notes The version flag follows common CLI conventions and provides useful debugging information. Pseudo-versions (like `v0.0.0-...`) are normalized to "dev" for cleaner output, while real version tags display normally. Issue #296 I have: - [x] Written a clear PR title and description (above) - [x] Signed the [Khan Academy CLA](https://www.khanacademy.org/r/cla) - [x] Added tests covering my changes, if applicable - [x] Included a link to the issue fixed, if applicable - [x] Included documentation, for new features - [x] Added an entry to the changelog
1 parent 8ddeeee commit 5b0aabc

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

docs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Note that genqlient now requires Go 1.23 or higher, and is tested through Go 1.2
2828

2929
### New features:
3030

31+
- Added `--version` flag to print version information including commit hash and build date
32+
3133
### Bug fixes:
3234

3335
- fixed minor typos and grammatical issues across the project

generate/main.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"os"
1111
"path/filepath"
12+
"runtime/debug"
1213
"strings"
1314

1415
"github.com/alexflint/go-arg"
@@ -59,6 +60,7 @@ func readConfigGenerateAndWrite(configFilename string) error {
5960
type cliArgs struct {
6061
ConfigFilename string `arg:"positional" placeholder:"CONFIG" default:"" help:"path to genqlient configuration (default: genqlient.yaml in current or any parent directory)"`
6162
Init bool `arg:"--init" help:"write out and use a default config file"`
63+
Version bool `arg:"--version" help:"print version information"`
6264
}
6365

6466
func (cliArgs) Description() string {
@@ -68,6 +70,42 @@ See https://github.com/Khan/genqlient for full documentation.
6870
`)
6971
}
7072

73+
func printVersion() {
74+
info, ok := debug.ReadBuildInfo()
75+
if !ok {
76+
fmt.Println("genqlient (version information not available)")
77+
return
78+
}
79+
80+
version := info.Main.Version
81+
if version == "" || version == "(devel)" || strings.HasPrefix(version, "v0.0.0-") {
82+
version = "dev"
83+
}
84+
85+
var commit, buildDate string
86+
for _, setting := range info.Settings {
87+
switch setting.Key {
88+
case "vcs.revision":
89+
commit = setting.Value
90+
case "vcs.time":
91+
buildDate = setting.Value
92+
}
93+
}
94+
95+
fmt.Print("genqlient " + version)
96+
if commit != "" {
97+
if len(commit) > 12 {
98+
commit = commit[:12]
99+
}
100+
fmt.Printf(" (%s", commit)
101+
if buildDate != "" {
102+
fmt.Printf(", built %s", buildDate)
103+
}
104+
fmt.Print(")")
105+
}
106+
fmt.Println()
107+
}
108+
71109
// Main is the command-line entrypoint to genqlient; it's equivalent to calling
72110
//
73111
// go run github.com/Khan/genqlient
@@ -83,6 +121,12 @@ func Main() {
83121

84122
var args cliArgs
85123
arg.MustParse(&args)
124+
125+
if args.Version {
126+
printVersion()
127+
return
128+
}
129+
86130
if args.Init {
87131
filename := args.ConfigFilename
88132
if filename == "" {

0 commit comments

Comments
 (0)