Skip to content

Commit a756332

Browse files
committed
fix: refactor static version to be collected from the tag during build
1 parent 8158177 commit a756332

2 files changed

Lines changed: 46 additions & 35 deletions

File tree

.goreleaser.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ builds:
1313
- CGO_ENABLED=0
1414
ldflags:
1515
- -s -w
16-
- -X main.version={{.Version}}
17-
- -X main.commit={{.Commit}}
18-
- -X main.date={{.Date}}
16+
- -X github.com/abdo-farag/otc-cli/cli.version={{.Version}}
17+
- -X github.com/abdo-farag/otc-cli/cli.commit={{.Commit}}
18+
- -X github.com/abdo-farag/otc-cli/cli.buildDate={{.Date}}
1919
ignore:
2020
- goos: windows
2121
goarch: arm64

cli/root.go

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
11
package cli
22

33
import (
4-
"fmt"
4+
"fmt"
55

6-
"github.com/spf13/cobra"
6+
"github.com/spf13/cobra"
77
)
88

99
var (
10-
version = "1.0.0"
10+
// Version information - set via ldflags during build
11+
version = "dev"
12+
commit = "none"
13+
buildDate = "unknown"
1114
)
1215

1316
// Global flags
1417
var (
15-
projectFlag string
16-
rawFlag bool
17-
csvFlag bool
18+
projectFlag string
19+
rawFlag bool
20+
csvFlag bool
1821
)
1922

2023
// rootCmd represents the base command
2124
var rootCmd = &cobra.Command{
22-
Use: "otc-cli",
23-
Short: "OTC CLI - Manage your Open Telekom Cloud resources",
24-
Long: `A command-line tool for managing Open Telekom Cloud (OTC) resources.
25+
Use: "otc-cli",
26+
Short: "OTC CLI - Manage your Open Telekom Cloud resources",
27+
Long: `A command-line tool for managing Open Telekom Cloud (OTC) resources.
2528
Supports authentication, resource management, and automation.`,
26-
Version: version,
29+
Version: version,
2730
}
2831

2932
// Execute runs the root command
3033
func Execute() error {
31-
return rootCmd.Execute()
34+
return rootCmd.Execute()
3235
}
3336

3437
func init() {
35-
// Persistent flags available to all subcommands
36-
rootCmd.PersistentFlags().StringVarP(&projectFlag, "project", "p", "", "Project ID or name")
37-
rootCmd.PersistentFlags().BoolVar(&rawFlag, "raw", false, "Output raw JSON response")
38-
rootCmd.PersistentFlags().BoolVar(&rawFlag, "json", false, "Output raw JSON response (alias)")
39-
rootCmd.PersistentFlags().BoolVar(&csvFlag, "csv", false, "Output in CSV format")
40-
41-
rootCmd.MarkFlagsMutuallyExclusive("raw", "csv")
42-
rootCmd.MarkFlagsMutuallyExclusive("json", "csv")
43-
// Add subcommands
44-
rootCmd.AddCommand(loginCmd)
45-
rootCmd.AddCommand(logoutCmd)
46-
rootCmd.AddCommand(docsCmd)
47-
rootCmd.AddCommand(listCmd)
48-
rootCmd.AddCommand(getCmd)
49-
rootCmd.AddCommand(consoleCmd)
50-
rootCmd.AddCommand(versionCmd)
38+
// Persistent flags available to all subcommands
39+
rootCmd.PersistentFlags().StringVarP(&projectFlag, "project", "p", "", "Project ID or name")
40+
rootCmd.PersistentFlags().BoolVar(&rawFlag, "raw", false, "Output raw JSON response")
41+
rootCmd.PersistentFlags().BoolVar(&rawFlag, "json", false, "Output raw JSON response (alias)")
42+
rootCmd.PersistentFlags().BoolVar(&csvFlag, "csv", false, "Output in CSV format")
43+
44+
rootCmd.MarkFlagsMutuallyExclusive("raw", "csv")
45+
rootCmd.MarkFlagsMutuallyExclusive("json", "csv")
46+
47+
// Add subcommands
48+
rootCmd.AddCommand(loginCmd)
49+
rootCmd.AddCommand(logoutCmd)
50+
rootCmd.AddCommand(docsCmd)
51+
rootCmd.AddCommand(listCmd)
52+
rootCmd.AddCommand(getCmd)
53+
rootCmd.AddCommand(consoleCmd)
54+
rootCmd.AddCommand(versionCmd)
5155
}
5256

5357
var versionCmd = &cobra.Command{
54-
Use: "version",
55-
Short: "Print the version number",
56-
Run: func(cmd *cobra.Command, args []string) {
57-
fmt.Printf("otc-cli v%s\n", version)
58-
},
58+
Use: "version",
59+
Short: "Print the version number",
60+
Long: "Display version information including build details",
61+
Run: func(cmd *cobra.Command, args []string) {
62+
fmt.Printf("otc-cli version %s\n", version)
63+
if commit != "none" {
64+
fmt.Printf(" commit: %s\n", commit)
65+
}
66+
if buildDate != "unknown" {
67+
fmt.Printf(" built: %s\n", buildDate)
68+
}
69+
},
5970
}

0 commit comments

Comments
 (0)