Skip to content

Commit f8c791e

Browse files
committed
Add --version flag and version printing
Add support for printing the app version and exiting. Introduce ShowVersion in Config and parse --version / -v flags, add a Version variable (default "dev") in main, and print "hideTop <version>" when the flag is set. Update README to document the --version flag and show how to embed a git tag into the binary using -ldflags.
1 parent 9919ba0 commit f8c791e

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Built with [Bubble Tea](https://github.com/charmbracelet/bubbletea) and [Lip Glo
3232
go build -o hideTop ./src/
3333
./hideTop # default 1s refresh
3434
./hideTop --interval 500ms # faster refresh
35+
./hideTop --version # print version and exit
36+
# local build with git tag in --version:
37+
go build -ldflags "-X main.Version=$(git describe --tags --always --dirty)" -o hideTop ./src/
3538
```
3639

3740
## Project structure

internal/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@ import (
77

88
type Config struct {
99
RefreshInterval time.Duration
10+
ShowVersion bool
1011
}
1112

1213
func Parse() Config {
1314
interval := flag.Duration("interval", 1*time.Second,
1415
"metrics refresh interval (e.g. 500ms, 1s, 2s)")
16+
showVersion := flag.Bool("version", false, "print version and exit")
17+
showVersionShort := flag.Bool("v", false, "print version and exit")
1518
flag.Parse()
1619

1720
cfg := Config{
1821
RefreshInterval: *interval,
22+
ShowVersion: *showVersion || *showVersionShort,
1923
}
2024

2125
if cfg.RefreshInterval < 100*time.Millisecond {

src/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ import (
1010
"github.com/youhide/hideTop/internal/config"
1111
)
1212

13+
var Version = "dev"
14+
1315
func main() {
1416
cfg := config.Parse()
17+
if cfg.ShowVersion {
18+
fmt.Printf("hideTop %s\n", Version)
19+
return
20+
}
21+
1522
m := app.New(cfg)
1623

1724
p := tea.NewProgram(m,

0 commit comments

Comments
 (0)