From b1b4dbbf5ea86a1460b7be0f510648e7d747eb4d Mon Sep 17 00:00:00 2001 From: yh-noh Date: Wed, 2 Sep 2026 07:58:28 +0000 Subject: [PATCH] feat(cli): register a Version flag on the root command ./mcc --version has never worked - Cobra only registers --version when the root command's Version field is set, and it wasn't, so the flag simply didn't exist (docs telling users to run it as a smoke test always failed with "unknown flag"). Pull the version from the VCS info Go embeds automatically for any build done inside a git checkout (default since Go 1.18, no ldflags or build script changes needed) - the short commit hash, plus "-dirty" if the working tree had uncommitted changes at build time. Falls back to "dev" when build info isn't available (e.g. go run). --- src/cmd/root.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/cmd/root.go b/src/cmd/root.go index 2b4485f3..760a5d31 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "os" + "runtime/debug" "github.com/spf13/cobra" @@ -17,10 +18,40 @@ var RootCmd = &cobra.Command{ Use: "mcc", Short: "A tool to operate M-CMP system", Long: `The mcc is a tool to operate M-CMP system.`, + Version: buildVersion(), CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true}, //completion 옵션 출력 제거 // Uncomment the following line if your bare application } +// buildVersion reads the VCS revision Go embeds automatically when building +// from within a git checkout (default since Go 1.18, no ldflags required). +func buildVersion() string { + info, ok := debug.ReadBuildInfo() + if !ok { + return "dev" + } + var revision string + var dirty bool + for _, s := range info.Settings { + switch s.Key { + case "vcs.revision": + revision = s.Value + case "vcs.modified": + dirty = s.Value == "true" + } + } + if revision == "" { + return "dev" + } + if len(revision) > 12 { + revision = revision[:12] + } + if dirty { + revision += "-dirty" + } + return revision +} + // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() {