停用历史业务命令并明确CLI运维范围

This commit is contained in:
xuans committed 2026-09-27 17:31:09 +08:00
1 parent 25fd4cf89c
commit d90ddaadbb
9 files changed
+202 -25

No files matched your search

+1 -1
View File
@@ -66,7 +66,7 @@ func TestSetConfigValueUpdatesAndValidatesAllowedFields(t *testing.T) {
func TestRootIncludesOperationalCommands(t *testing.T) {
root := newRootCommand()
for _, name := range []string{"certificate", "renew", "deploy", "logs", "config", "backup", "restore"} {
for _, name := range []string{"certificate", "logs", "config", "backup", "restore", "service", "dns"} {
if root.CommandPath() == name || root.Commands() == nil {
t.Fatalf("根命令状态异常:%s", name)
}
+20 -4
View File
@@ -64,7 +64,7 @@ func Execute() {
}
func newRootCommand() *cobra.Command {
root := &cobra.Command{Use: "scert", Short: "SSL 证书管理服务", SilenceUsage: true}
root := &cobra.Command{Use: "scert", Short: "SSL 证书服务与运维工具,业务管理请使用 Web", SilenceUsage: true}
root.CompletionOptions.DisableDefaultCmd = true
root.SetHelpCommand(newHelpCommand())
root.SetHelpTemplate(helpTemplate)
@@ -72,14 +72,30 @@ func newRootCommand() *cobra.Command {
root.PersistentFlags().StringVar(&baseDir, "base-dir", "", "Windows 默认安装根目录")
root.AddCommand(
&cobra.Command{Use: "version", Short: "显示版本信息", Run: func(cmd *cobra.Command, args []string) { cmd.Printf("Scert %s\n", version) }},
newInitCommand(), newStatusCommand(), newServeCommand(), newDomainCommand(), newCertificateCommand(),
newRenewCommand(), newDeployCommand(), newLogsCommand(), newConfigCommand(), newBackupCommand(), newRestoreCommand(),
newServerCommand(), newSSHKeyCommand(), newServiceCommand(), newDNSCommand(),
newInitCommand(), newStatusCommand(), newServeCommand(), newCertificateCommand(),
newLogsCommand(), newConfigCommand(), newBackupCommand(), newRestoreCommand(),
newServiceCommand(), newDNSCommand(),
)
for _, name := range []string{"domain", "server", "ssh-key", "renew", "deploy"} {
root.AddCommand(newRetiredBusinessCommand(name))
}
localizeHelpFlags(root)
return root
}
// 历史业务命令只保留迁移提示,不读取配置、凭据或数据库,也不执行远程操作。
func newRetiredBusinessCommand(name string) *cobra.Command {
return &cobra.Command{
Use: name,
Short: "业务命令已迁移至 Web 管理页面",
Hidden: true,
DisableFlagParsing: true,
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("此业务命令已停用,请使用 Web 管理页面;CLI 仅保留初始化、服务运行和运维诊断、数据库维护、备份恢复及系统服务管理")
},
}
}
func newHelpCommand() *cobra.Command {
return &cobra.Command{
Use: "help [命令]",
+12 -12
View File
@@ -72,29 +72,29 @@ func TestHelpSupportsSubcommand(t *testing.T) {
var output bytes.Buffer
root.SetOut(&output)
root.SetErr(&output)
root.SetArgs([]string{"help", "domain"})
root.SetArgs([]string{"help", "certificate"})
if err := root.Execute(); err != nil {
t.Fatalf("查看子命令帮助失败:%v", err)
}
text := output.String()
if !strings.Contains(text, "管理托管域名") || !strings.Contains(text, "添加托管域名") {
if !strings.Contains(text, "查询和校验证书") || !strings.Contains(text, "校验当前证书") {
t.Fatalf("子命令帮助内容不正确:%s", text)
}
}
func TestHelpIncludesServerManagementCommands(t *testing.T) {
func TestHelpIncludesSystemServiceCommands(t *testing.T) {
root := newRootCommand()
var output bytes.Buffer
root.SetOut(&output)
root.SetErr(&output)
root.SetArgs([]string{"help", "server"})
root.SetArgs([]string{"help", "service"})
if err := root.Execute(); err != nil {
t.Fatalf("查看服务器管理帮助失败:%v", err)
t.Fatalf("查看系统服务帮助失败:%v", err)
}
text := output.String()
for _, expected := range []string{"管理部署服务器", "添加部署服务器", "编辑部署服务器", "删除部署服务器"} {
for _, expected := range []string{"管理系统服务", "注册 Scert 系统服务", "取消注册 Scert 系统服务"} {
if !strings.Contains(text, expected) {
t.Fatalf("服务器管理帮助缺少 %q:%s", expected, text)
t.Fatalf("系统服务帮助缺少 %q:%s", expected, text)
}
}
}
@@ -117,19 +117,19 @@ func TestServerCommandUsesDefaultCertificatePathTemplates(t *testing.T) {
}
}
func TestHelpIncludesSSHKeyManagementCommands(t *testing.T) {
func TestHelpIncludesDNSDiagnosticCommand(t *testing.T) {
root := newRootCommand()
var output bytes.Buffer
root.SetOut(&output)
root.SetErr(&output)
root.SetArgs([]string{"help", "ssh-key"})
root.SetArgs([]string{"help", "dns"})
if err := root.Execute(); err != nil {
t.Fatalf("查看 SSH 私钥管理帮助失败:%v", err)
t.Fatalf("查看 DNS 诊断帮助失败:%v", err)
}
text := output.String()
for _, expected := range []string{"管理 SSH 部署私钥", "安全录入 SSH 私钥", "删除未被服务器使用的 SSH 私钥"} {
for _, expected := range []string{"检查指定域名的阿里云 RAM 权限", "验证当前权限"} {
if !strings.Contains(text, expected) {
t.Fatalf("SSH 私钥管理帮助缺少 %q:%s", expected, text)
t.Fatalf("DNS 诊断帮助缺少 %q:%s", expected, text)
}
}
}
+87
View File
@@ -0,0 +1,87 @@
package cli
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
func TestRootExposesOnlyOperationalCommands(t *testing.T) {
allowed := map[string]bool{
"version": true, "init": true, "status": true, "serve": true,
"certificate": true, "logs": true, "config": true,
"backup": true, "restore": true, "service": true, "dns": true,
}
root := newRootCommand()
for _, command := range root.Commands() {
if command.Hidden {
continue
}
if !allowed[command.Name()] {
t.Errorf("正式 CLI 暴露了范围外命令:%s", command.Name())
}
delete(allowed, command.Name())
}
if len(allowed) != 0 {
t.Fatalf("正式 CLI 缺少运维命令:%v", allowed)
}
var output bytes.Buffer
root.SetOut(&output)
root.SetArgs([]string{"help"})
if err := root.Execute(); err != nil {
t.Fatal(err)
}
for _, name := range []string{"domain", "server", "ssh-key", "renew", "deploy"} {
if strings.Contains(output.String(), "\n "+name+" ") {
t.Errorf("正式帮助仍展示历史业务命令:%s", name)
}
}
}
func TestRetiredBusinessCommandsRejectBeforeAccessingData(t *testing.T) {
cases := [][]string{
{"domain", "list"},
{"domain", "add", "--name", "example.com"},
{"domain", "issue", "--id", "example"},
{"domain", "import", "--private-key", "not-a-real-key.pem"},
{"server", "add", "--password", "test-secret-not-real"},
{"server", "delete", "--id", "example"},
{"ssh-key", "add", "--name", "example"},
{"ssh-key", "delete", "--id", "example"},
{"renew"},
{"renew", "--id", "example"},
{"deploy", "--id", "example"},
}
for _, args := range cases {
t.Run(strings.Join(args[:min(2, len(args))], "_"), func(t *testing.T) {
directory := t.TempDir()
path := filepath.Join(directory, "invalid.json")
original := []byte("invalid-config-sentinel")
if err := os.WriteFile(path, original, 0o600); err != nil {
t.Fatal(err)
}
root := newRootCommand()
var output bytes.Buffer
root.SetOut(&output)
root.SetErr(&output)
root.SetArgs(append([]string{"--config", path, "--base-dir", directory}, args...))
err := root.Execute()
if err == nil || !strings.Contains(err.Error(), "此业务命令已停用,请使用 Web 管理页面") {
t.Fatalf("旧入口未在读取无效配置前拒绝:%v", err)
}
if strings.Contains(output.String()+err.Error(), "test-secret-not-real") {
t.Fatal("迁移提示泄露了旧命令中的凭据参数")
}
content, readErr := os.ReadFile(path)
if readErr != nil || !bytes.Equal(content, original) {
t.Fatalf("被停用命令修改了配置:%v", readErr)
}
entries, readErr := os.ReadDir(directory)
if readErr != nil || len(entries) != 1 {
t.Fatalf("被停用命令创建了运行数据:%v", readErr)
}
})
}
}