feat(build): Add version command and fix goreleaser configuration for Linux/macOS builds
- Add gordon version command with full and short output options - Integrate version information from goreleaser via ldflags - Remove Windows builds to focus on Linux and macOS platforms - Fix deprecated goreleaser archive format configuration - Make config file optional for version command
- commit
- 5446a7e3043bf1dcb0e0da7bc438b3ddf180da9b
- author
- bamen · · 2025-06-09 16:49 UTC
- parent
- 81621ee
- signature
- unsigned
4 files · +60 -6
M .goreleaser.yaml +1 -3
| @@ -12,7 +12,6 @@ builds: | |||
| 12 | 12 | binary: gordon | |
| 13 | 13 | goos: | |
| 14 | 14 | - linux | |
| 15 | - | - windows | |
| 16 | 15 | - darwin | |
| 17 | 16 | goarch: | |
| 18 | 17 | - amd64 | |
| @@ -63,8 +62,7 @@ docker_manifests: | |||
| 63 | 62 | - ghcr.io/bnema/gordon:{{ .Tag }}-arm64 | |
| 64 | 63 | ||
| 65 | 64 | archives: | |
| 66 | - | - format: zip | |
| 67 | - | name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" | |
| 65 | + | - name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" | |
| 68 | 66 | ||
| 69 | 67 | changelog: | |
| 70 | 68 | sort: asc | |
| @@ -12,7 +12,6 @@ builds: | |||||
| 12 | binary: gordon | 12 | binary: gordon | ||
| 13 | goos: | 13 | goos: | ||
| 14 | - linux | 14 | - linux | ||
| 15 | - | - windows | |||
| 16 | - darwin | 15 | - darwin | ||
| 17 | goarch: | 16 | goarch: | ||
| 18 | - amd64 | 17 | - amd64 | ||
| @@ -63,8 +62,7 @@ docker_manifests: | |||||
| 63 | - ghcr.io/bnema/gordon:{{ .Tag }}-arm64 | 62 | - ghcr.io/bnema/gordon:{{ .Tag }}-arm64 | ||
| 64 | 63 | ||||
| 65 | archives: | 64 | archives: | ||
| 66 | - | - format: zip | 65 | + | - name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" |
| 67 | - | name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}" | |||
| 68 | 66 | ||||
| 69 | changelog: | 67 | changelog: | ||
| 70 | sort: asc | 68 | sort: asc | ||
M cmd/root.go +16 -3
| @@ -4,13 +4,19 @@ import ( | |||
| 4 | 4 | "fmt" | |
| 5 | 5 | "os" | |
| 6 | 6 | ||
| 7 | - | "github.com/rs/zerolog/log" | |
| 8 | 7 | "github.com/spf13/cobra" | |
| 9 | 8 | "github.com/spf13/viper" | |
| 10 | 9 | ) | |
| 11 | 10 | ||
| 12 | 11 | var cfgFile string | |
| 13 | 12 | ||
| 13 | + | // Version information set by main.go from goreleaser | |
| 14 | + | var ( | |
| 15 | + | BuildVersion = "dev" | |
| 16 | + | BuildCommit = "unknown" | |
| 17 | + | BuildDate = "unknown" | |
| 18 | + | ) | |
| 19 | + | ||
| 14 | 20 | var rootCmd = &cobra.Command{ | |
| 15 | 21 | Use: "gordon", | |
| 16 | 22 | Short: "Gordon - Container Registry & Deployment System", | |
| @@ -22,6 +28,13 @@ func Execute() error { | |||
| 22 | 28 | return rootCmd.Execute() | |
| 23 | 29 | } | |
| 24 | 30 | ||
| 31 | + | // SetVersionInfo sets the build information from main.go | |
| 32 | + | func SetVersionInfo(version, commit, date string) { | |
| 33 | + | BuildVersion = version | |
| 34 | + | BuildCommit = commit | |
| 35 | + | BuildDate = date | |
| 36 | + | } | |
| 37 | + | ||
| 25 | 38 | func init() { | |
| 26 | 39 | cobra.OnInitialize(initConfig) | |
| 27 | 40 | rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./gordon.toml)") | |
| @@ -61,8 +74,8 @@ func initConfig() { | |||
| 61 | 74 | } else { | |
| 62 | 75 | if cfgFile != "" { | |
| 63 | 76 | fmt.Fprintf(os.Stderr, "Error reading config file: %v\n", err) | |
| 64 | - | } else { | |
| 65 | - | log.Fatal().Msg("config file not found - please specify with --config flag or ensure gordon.toml exists in current directory") | |
| 66 | 77 | } | |
| 78 | + | // Don't fatal for version command - config not needed | |
| 79 | + | // Other commands will check for config when needed | |
| 67 | 80 | } | |
| 68 | 81 | } | |
| @@ -4,13 +4,19 @@ import ( | |||||
| 4 | "fmt" | 4 | "fmt" | ||
| 5 | "os" | 5 | "os" | ||
| 6 | 6 | ||||
| 7 | - | "github.com/rs/zerolog/log" | |||
| 8 | "github.com/spf13/cobra" | 7 | "github.com/spf13/cobra" | ||
| 9 | "github.com/spf13/viper" | 8 | "github.com/spf13/viper" | ||
| 10 | ) | 9 | ) | ||
| 11 | 10 | ||||
| 12 | var cfgFile string | 11 | var cfgFile string | ||
| 13 | 12 | ||||
| 13 | + | // Version information set by main.go from goreleaser | |||
| 14 | + | var ( | |||
| 15 | + | BuildVersion = "dev" | |||
| 16 | + | BuildCommit = "unknown" | |||
| 17 | + | BuildDate = "unknown" | |||
| 18 | + | ) | |||
| 19 | + | ||||
| 14 | var rootCmd = &cobra.Command{ | 20 | var rootCmd = &cobra.Command{ | ||
| 15 | Use: "gordon", | 21 | Use: "gordon", | ||
| 16 | Short: "Gordon - Container Registry & Deployment System", | 22 | Short: "Gordon - Container Registry & Deployment System", | ||
| @@ -22,6 +28,13 @@ func Execute() error { | |||||
| 22 | return rootCmd.Execute() | 28 | return rootCmd.Execute() | ||
| 23 | } | 29 | } | ||
| 24 | 30 | ||||
| 31 | + | // SetVersionInfo sets the build information from main.go | |||
| 32 | + | func SetVersionInfo(version, commit, date string) { | |||
| 33 | + | BuildVersion = version | |||
| 34 | + | BuildCommit = commit | |||
| 35 | + | BuildDate = date | |||
| 36 | + | } | |||
| 37 | + | ||||
| 25 | func init() { | 38 | func init() { | ||
| 26 | cobra.OnInitialize(initConfig) | 39 | cobra.OnInitialize(initConfig) | ||
| 27 | rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./gordon.toml)") | 40 | rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ./gordon.toml)") | ||
| @@ -61,8 +74,8 @@ func initConfig() { | |||||
| 61 | } else { | 74 | } else { | ||
| 62 | if cfgFile != "" { | 75 | if cfgFile != "" { | ||
| 63 | fmt.Fprintf(os.Stderr, "Error reading config file: %v\n", err) | 76 | fmt.Fprintf(os.Stderr, "Error reading config file: %v\n", err) | ||
| 64 | - | } else { | |||
| 65 | - | log.Fatal().Msg("config file not found - please specify with --config flag or ensure gordon.toml exists in current directory") | |||
| 66 | } | 77 | } | ||
| 78 | + | // Don't fatal for version command - config not needed | |||
| 79 | + | // Other commands will check for config when needed | |||
| 67 | } | 80 | } | ||
| 68 | } | 81 | } | ||
A cmd/version.go +33 -0
| @@ -0,0 +1,33 @@ | |||
| 1 | + | package cmd | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "fmt" | |
| 5 | + | ||
| 6 | + | "github.com/spf13/cobra" | |
| 7 | + | ) | |
| 8 | + | ||
| 9 | + | var versionCmd = &cobra.Command{ | |
| 10 | + | Use: "version", | |
| 11 | + | Short: "Show version information", | |
| 12 | + | Long: `Display the Gordon version, commit hash, and build date.`, | |
| 13 | + | Run: func(cmd *cobra.Command, args []string) { | |
| 14 | + | fmt.Printf("Gordon %s\n", BuildVersion) | |
| 15 | + | fmt.Printf("Commit: %s\n", BuildCommit) | |
| 16 | + | fmt.Printf("Built: %s\n", BuildDate) | |
| 17 | + | }, | |
| 18 | + | } | |
| 19 | + | ||
| 20 | + | func init() { | |
| 21 | + | rootCmd.AddCommand(versionCmd) | |
| 22 | + | versionCmd.Flags().BoolP("short", "s", false, "Show only version number") | |
| 23 | + | ||
| 24 | + | // Override run function to handle short flag | |
| 25 | + | originalRun := versionCmd.Run | |
| 26 | + | versionCmd.Run = func(cmd *cobra.Command, args []string) { | |
| 27 | + | if short, _ := cmd.Flags().GetBool("short"); short { | |
| 28 | + | fmt.Println(BuildVersion) | |
| 29 | + | return | |
| 30 | + | } | |
| 31 | + | originalRun(cmd, args) | |
| 32 | + | } | |
| 33 | + | }⏎̸ | |
| @@ -0,0 +1,33 @@ | |||||
| 1 | + | package cmd | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "fmt" | |||
| 5 | + | ||||
| 6 | + | "github.com/spf13/cobra" | |||
| 7 | + | ) | |||
| 8 | + | ||||
| 9 | + | var versionCmd = &cobra.Command{ | |||
| 10 | + | Use: "version", | |||
| 11 | + | Short: "Show version information", | |||
| 12 | + | Long: `Display the Gordon version, commit hash, and build date.`, | |||
| 13 | + | Run: func(cmd *cobra.Command, args []string) { | |||
| 14 | + | fmt.Printf("Gordon %s\n", BuildVersion) | |||
| 15 | + | fmt.Printf("Commit: %s\n", BuildCommit) | |||
| 16 | + | fmt.Printf("Built: %s\n", BuildDate) | |||
| 17 | + | }, | |||
| 18 | + | } | |||
| 19 | + | ||||
| 20 | + | func init() { | |||
| 21 | + | rootCmd.AddCommand(versionCmd) | |||
| 22 | + | versionCmd.Flags().BoolP("short", "s", false, "Show only version number") | |||
| 23 | + | ||||
| 24 | + | // Override run function to handle short flag | |||
| 25 | + | originalRun := versionCmd.Run | |||
| 26 | + | versionCmd.Run = func(cmd *cobra.Command, args []string) { | |||
| 27 | + | if short, _ := cmd.Flags().GetBool("short"); short { | |||
| 28 | + | fmt.Println(BuildVersion) | |||
| 29 | + | return | |||
| 30 | + | } | |||
| 31 | + | originalRun(cmd, args) | |||
| 32 | + | } | |||
| 33 | + | }⏎̸ | |||
M main.go +10 -0
| @@ -6,7 +6,17 @@ import ( | |||
| 6 | 6 | "gordon/cmd" | |
| 7 | 7 | ) | |
| 8 | 8 | ||
| 9 | + | // Build information set by goreleaser via ldflags | |
| 10 | + | var ( | |
| 11 | + | version = "dev" | |
| 12 | + | commit = "unknown" | |
| 13 | + | date = "unknown" | |
| 14 | + | ) | |
| 15 | + | ||
| 9 | 16 | func main() { | |
| 17 | + | // Set version information for the CLI | |
| 18 | + | cmd.SetVersionInfo(version, commit, date) | |
| 19 | + | ||
| 10 | 20 | if err := cmd.Execute(); err != nil { | |
| 11 | 21 | os.Exit(1) | |
| 12 | 22 | } | |
| @@ -6,7 +6,17 @@ import ( | |||||
| 6 | "gordon/cmd" | 6 | "gordon/cmd" | ||
| 7 | ) | 7 | ) | ||
| 8 | 8 | ||||
| 9 | + | // Build information set by goreleaser via ldflags | |||
| 10 | + | var ( | |||
| 11 | + | version = "dev" | |||
| 12 | + | commit = "unknown" | |||
| 13 | + | date = "unknown" | |||
| 14 | + | ) | |||
| 15 | + | ||||
| 9 | func main() { | 16 | func main() { | ||
| 17 | + | // Set version information for the CLI | |||
| 18 | + | cmd.SetVersionInfo(version, commit, date) | |||
| 19 | + | ||||
| 10 | if err := cmd.Execute(); err != nil { | 20 | if err := cmd.Execute(); err != nil { | ||
| 11 | os.Exit(1) | 21 | os.Exit(1) | ||
| 12 | } | 22 | } | ||