From 9075cb4acb3372144a9583dca6a39898a81348d3 Mon Sep 17 00:00:00 2001 From: weibaohui Date: Tue, 15 Oct 2024 17:17:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E6=89=93=E5=8D=B0=E5=92=8C=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?log=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将fmt包替换为log包进行日志输出- 移除了os包,不再使用os.Exit直接退出程序 - 在出现错误时使用log.Printf记录错误并继续执行 - 注释掉了部分调试代码 --- internal/kubectl/doc.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/internal/kubectl/doc.go b/internal/kubectl/doc.go index f1376c3..3e5b07d 100644 --- a/internal/kubectl/doc.go +++ b/internal/kubectl/doc.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "log" - "os" "strings" "github.com/weibaohui/k8m/internal/utils" @@ -213,15 +212,15 @@ func buildPropertyNode(prop Property) *TreeNode { // printTree 递归打印 TreeNode func printTree(node *TreeNode, level int) { indent := strings.Repeat(" ", level) - fmt.Printf("%s%s (ID: %s)\n", indent, node.Label, node.ID) + log.Printf("%s%s (ID: %s)\n", indent, node.Label, node.ID) if node.Description != "" { - fmt.Printf("%s Description: %s\n", indent, node.Description) + log.Printf("%s Description: %s\n", indent, node.Description) } if node.Type != "" { - fmt.Printf("%s Type: %s\n", indent, node.Type) + log.Printf("%s Type: %s\n", indent, node.Type) } if node.Ref != "" { - fmt.Printf("%s Ref: %s\n", indent, node.Ref) + log.Printf("%s Ref: %s\n", indent, node.Ref) } for _, child := range node.Children { @@ -235,25 +234,25 @@ func initDoc() { // 获取 OpenAPI Schema openAPISchema, err := kubectl.client.DiscoveryClient.OpenAPISchema() if err != nil { - fmt.Printf("Error fetching OpenAPI schema: %v\n", err) - os.Exit(1) + log.Printf("Error fetching OpenAPI schema: %v\n", err) + return } // 将 OpenAPI Schema 转换为 JSON 字符串 schemaBytes, err := json.Marshal(openAPISchema) if err != nil { - fmt.Printf("Error marshaling OpenAPI schema to JSON: %v\n", err) - os.Exit(1) + log.Printf("Error marshaling OpenAPI schema to JSON: %v\n", err) + return } // os.WriteFile("def.json", schemaBytes, 0644) // 打印部分 Schema 以供调试 - // fmt.Println(string(schemaBytes)) + // log.Println(string(schemaBytes)) root := &RootDefinitions{} err = json.Unmarshal(schemaBytes, root) if err != nil { - fmt.Printf("Error unmarshaling OpenAPI schema: %v\n", err) - os.Exit(1) + log.Printf("Error unmarshaling OpenAPI schema: %v\n", err) + return } definitionList := root.Definitions.AdditionalProperties @@ -263,8 +262,8 @@ func initDoc() { // 解析 Schema 并构建树形结构 treeRoot, err := parseOpenAPISchema(str) if err != nil { - fmt.Printf("Error parsing OpenAPI schema: %v\n", err) - os.Exit(1) + log.Printf("Error parsing OpenAPI schema: %v\n", err) + continue } trees = append(trees, treeRoot) }