From 50e9d48cef7f7b813eafad51318c733719d424ab Mon Sep 17 00:00:00 2001 From: BBQ <35603386+HoneyBBQ@users.noreply.github.com> Date: Sun, 1 Feb 2026 00:36:52 -0800 Subject: [PATCH] feat: implement unified version management (#20) --- cmd/agent/main.go | 2 ++ cmd/cli/main.go | 7 ++++++ cmd/mcp/Dockerfile | 2 +- cmd/mcp/main.go | 22 ++--------------- internal/version/version.go | 45 +++++++++++++++++++++++++++++++++++ packages/cli/src/cli/index.ts | 10 +++++++- 6 files changed, 66 insertions(+), 22 deletions(-) create mode 100644 internal/version/version.go diff --git a/cmd/agent/main.go b/cmd/agent/main.go index e91d2de1..d885905b 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -25,12 +25,14 @@ import ( "github.com/memohai/memoh/internal/settings" "github.com/memohai/memoh/internal/server" "github.com/memohai/memoh/internal/subagent" + "github.com/memohai/memoh/internal/version" "github.com/jackc/pgx/v5/pgtype" "golang.org/x/crypto/bcrypt" ) func main() { + log.Printf("Starting Memoh Agent %s", version.GetInfo()) ctx := context.Background() cfgPath := os.Getenv("CONFIG_PATH") cfg, err := config.Load(cfgPath) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 746f23bb..e51e8355 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -16,6 +16,7 @@ import ( "github.com/memohai/memoh/internal/chat" "github.com/memohai/memoh/internal/config" + "github.com/memohai/memoh/internal/version" "github.com/memohai/memoh/internal/logger" ) @@ -26,10 +27,15 @@ type cliOptions struct { timeout time.Duration apiBaseURL string jwtToken string + showVersion bool } func main() { opts := parseFlags() + if opts.showVersion { + fmt.Printf("Memoh CLI %s\n", version.GetInfo()) + return + } ctx := context.Background() cfg, err := config.Load(opts.configPath) @@ -97,6 +103,7 @@ func parseFlags() cliOptions { flag.StringVar(&opts.jwtToken, "jwt", "", "JWT token (optional)") flag.StringVar(&opts.apiBaseURL, "api-url", "", "API server base URL (e.g. http://127.0.0.1:8080)") flag.DurationVar(&opts.timeout, "timeout", 30*time.Second, "Request timeout") + flag.BoolVar(&opts.showVersion, "version", false, "Show version information") flag.Parse() return opts diff --git a/cmd/mcp/Dockerfile b/cmd/mcp/Dockerfile index fbaa82ee..13dfc15f 100644 --- a/cmd/mcp/Dockerfile +++ b/cmd/mcp/Dockerfile @@ -8,7 +8,7 @@ COPY . . ARG TARGETARCH ARG COMMIT_HASH=unknown RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH:-amd64} \ - go build -trimpath -ldflags "-s -w -X main.commitHash=${COMMIT_HASH}" -o /out/mcp ./cmd/mcp + go build -trimpath -ldflags "-s -w -X github.com/memohai/memoh/internal/version.CommitHash=${COMMIT_HASH}" -o /out/mcp ./cmd/mcp FROM busybox:latest COPY --from=build /out/mcp /mcp diff --git a/cmd/mcp/main.go b/cmd/mcp/main.go index 91fa74f1..78871162 100644 --- a/cmd/mcp/main.go +++ b/cmd/mcp/main.go @@ -7,31 +7,13 @@ import ( "github.com/memohai/memoh/internal/logger" "github.com/memohai/memoh/internal/mcp" + "github.com/memohai/memoh/internal/version" gomcp "github.com/modelcontextprotocol/go-sdk/mcp" ) -var ( - commitHash = "unknown" - version = "unknown" -) - func main() { - if version == "unknown" { - version = "v0.0.0-dev+" + commitHash - } - - logLevel := os.Getenv("LOG_LEVEL") - if logLevel == "" { - logLevel = "info" - } - logFormat := os.Getenv("LOG_FORMAT") - if logFormat == "" { - logFormat = "text" - } - logger.Init(logLevel, logFormat) - server := gomcp.NewServer( - &gomcp.Implementation{Name: "memoh-mcp", Version: version}, + &gomcp.Implementation{Name: "memoh-mcp", Version: version.GetInfo()}, nil, ) mcp.RegisterTools(server) diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 00000000..d1d9857e --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,45 @@ +package version + +import ( + "fmt" + "runtime/debug" +) + +var ( + // Version is the current version of the application. + // It can be overridden by ldflags at build time. + Version = "dev" + // CommitHash is the git commit hash at build time. + // It can be overridden by ldflags at build time. + CommitHash = "" + // BuildTime is the time when the application was built. + // It can be overridden by ldflags at build time. + BuildTime = "" +) + +// GetInfo returns a formatted version string including the version and commit hash. +func GetInfo() string { + if CommitHash == "" { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + CommitHash = setting.Value + } + if setting.Key == "vcs.time" { + BuildTime = setting.Value + } + } + } + } + + res := Version + if CommitHash != "" { + // Only use the first 7 characters of the commit hash if it's long + shortHash := CommitHash + if len(shortHash) > 7 { + shortHash = shortHash[:7] + } + res += fmt.Sprintf(" (%s)", shortHash) + } + return res +} diff --git a/packages/cli/src/cli/index.ts b/packages/cli/src/cli/index.ts index 3e128a49..70ecc154 100755 --- a/packages/cli/src/cli/index.ts +++ b/packages/cli/src/cli/index.ts @@ -7,6 +7,7 @@ import { table } from 'table' import readline from 'node:readline/promises' import { stdin as input, stdout as output } from 'node:process' +import packageJson from '../../package.json' import { apiRequest } from '../core/api' import { readConfig, @@ -68,7 +69,7 @@ const program = new Command() program .name('memoh') .description('Memoh CLI') - .version('0.1.0') + .version(packageJson.version) const ensureAuth = () => { const token = readToken() @@ -746,6 +747,13 @@ program rl.close() }) +program + .command('version') + .description('Show version information') + .action(() => { + console.log(`Memoh CLI v${packageJson.version}`) + }) + program.parseAsync(process.argv) const streamChat = async (query: string, token: TokenInfo) => {