feat: add UMS CLI for binary packaging and system initialization

- Add Cobra-based CLI with ums init, ums serve, ums version commands
- ums init supports interactive prompts and non-interactive flags
- Generates secure JWT secrets and config.yaml automatically
- Extract server.Serve() function for reuse
- Add cross-platform build targets to Makefile
- Update README with CLI installation and usage instructions

New files:
- cmd/ums/main.go - CLI entry point
- cmd/ums/cmd/root.go - Root command
- cmd/ums/cmd/init.go - Interactive/non-interactive init
- cmd/ums/cmd/serve.go - Server command
- cmd/ums/cmd/version.go - Version command
- internal/server/server.go - Extracted Serve function
This commit is contained in:
2026-04-19 08:59:00 +08:00
parent 7b047e2f11
commit 8d9f157eb8
11 changed files with 1035 additions and 300 deletions

View File

@@ -1,4 +1,4 @@
.PHONY: help build run test clean vet tidy check run-check db-dir
.PHONY: help build build-cli build-cli-all run test clean vet tidy check run-check db-dir
help: ## 显示帮助信息
@echo "======================================"
@@ -7,6 +7,8 @@ help: ## 显示帮助信息
@echo "可用命令:"
@echo " make check - 全面检查(依赖+vet+编译+测试)"
@echo " make build - 构建应用"
@echo " make build-cli - 构建 UMS CLI"
@echo " make build-cli-all - 交叉编译所有平台"
@echo " make run - 运行应用"
@echo " make test - 运行测试"
@echo " make vet - 代码静态检查"
@@ -15,7 +17,17 @@ help: ## 显示帮助信息
@echo " make clean - 清理构建文件"
@echo ""
check: tidy vet build test ## 全面检查:依赖+静态检查+编译+测试
# CLI 构建配置
CLI_NAME = ums
VERSION = 1.0.0
COMMIT = $(shell git rev-parse --short HEAD 2>/dev/null || echo "dev")
BUILD_DATE = $(shell date -u '+%Y-%m-%d_%H:%M:%S')
LDFLAGS = -ldflags "-X github.com/user-management-system/cmd/ums/cmd.Version=$(VERSION) -X github.com/user-management-system/cmd/ums/cmd.Commit=$(COMMIT) -X github.com/user-management-system/cmd/ums/cmd.BuildDate=$(BUILD_DATE)"
# 平台列表
PLATFORMS = darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 windows/amd64
check: tidy vet build build-cli test ## 全面检查:依赖+静态检查+编译+测试
tidy: ## 整理Go模块依赖
@echo "整理依赖..."
@@ -30,6 +42,20 @@ build: db-dir ## 构建应用
@echo "构建应用..."
go build -o bin/server cmd/server/main.go
build-cli: ## 构建 UMS CLI当前平台
@echo "构建 UMS CLI..."
CGO_ENABLED=0 go build $(LDFLAGS) -o bin/$(CLI_NAME) cmd/ums/main.go
build-cli-all: $(PLATFORMS) ## 构建所有平台的 CLI
@echo "所有平台构建完成"
build-cli-%:
@platform=$(patsubst %/%,%,$@); \
os=$(platform%%/*); \
arch=$(platform##*/); \
echo "Building for $$os/$$arch"; \
CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build $(LDFLAGS) -o bin/$(CLI_NAME)-$$os-$$arch cmd/ums/main.go
run: db-dir ## 运行应用
@echo "运行应用..."
go run cmd/server/main.go