Skip to content

Commit 3f8be9b

Browse files
author
Stanislav (Stas) Katkov
committed
fix --version flag
1 parent 3e67828 commit 3f8be9b

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

cmd/root.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76

87
tea "github.com/charmbracelet/bubbletea"
@@ -17,10 +16,6 @@ var rootCmd = &cobra.Command{
1716
Long: `devtui is a collection of small developer apps that help with day to day work.
1817
It includes tools like hash generator, unix timestamp converter, and number base converter and multiple others.`,
1918
RunE: func(cmd *cobra.Command, args []string) error {
20-
if flagVersion {
21-
fmt.Println(GetVersionString())
22-
return nil
23-
}
2419
p := tea.NewProgram(root.RootScreen(), tea.WithAltScreen())
2520
if _, err := p.Run(); err != nil {
2621
return err
@@ -29,13 +24,14 @@ It includes tools like hash generator, unix timestamp converter, and number base
2924
},
3025
}
3126

32-
var (
33-
flagTUI bool
34-
flagVersion bool
35-
)
27+
var flagTUI bool
3628

3729
func Execute() {
38-
err := fang.Execute(context.Background(), rootCmd)
30+
err := fang.Execute(
31+
context.Background(),
32+
rootCmd,
33+
fang.WithVersion(GetVersionShort()),
34+
)
3935
if err != nil {
4036
os.Exit(1)
4137
}
@@ -46,5 +42,6 @@ func GetRootCmd() *cobra.Command {
4642
}
4743

4844
func init() {
49-
rootCmd.Flags().BoolVarP(&flagVersion, "version", "v", false, "Print version information")
45+
// fang.Execute automatically adds --version flag
46+
// We configure it via fang.WithVersion() in Execute()
5047
}

cmd/version.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ var (
1313
date = "unknown"
1414
)
1515

16-
// GetVersionString returns a formatted version string with all version information.
17-
func GetVersionString() string {
18-
return fmt.Sprintf("Version: %s\nCommit: %s\nDate: %s", version, commit, date)
16+
// GetVersionShort returns a short version string suitable for single-line output.
17+
func GetVersionShort() string {
18+
return fmt.Sprintf("%s (commit: %s, built: %s)", version, commit, date)
1919
}
2020

2121
var versionCmd = &cobra.Command{
2222
Use: "version",
2323
Short: "Print version information",
2424
Long: "Print version, commit and date of release for this software",
2525
RunE: func(_ *cobra.Command, _ []string) error {
26-
fmt.Print(GetVersionString())
26+
fmt.Print("devtui version " + GetVersionShort())
2727
return nil
2828
},
2929
}

0 commit comments

Comments
 (0)