Skip to content

Commit

Permalink
将打印和错误处理改为使用log包
Browse files Browse the repository at this point in the history
- 将fmt包替换为log包进行日志输出- 移除了os包,不再使用os.Exit直接退出程序
- 在出现错误时使用log.Printf记录错误并继续执行
- 注释掉了部分调试代码
  • Loading branch information
weibaohui committed Oct 15, 2024
1 parent e3073e0 commit 9075cb4
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions internal/kubectl/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/weibaohui/k8m/internal/utils"
Expand Down Expand Up @@ -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 {
Expand All @@ -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

Expand All @@ -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)
}
Expand Down

0 comments on commit 9075cb4

Please sign in to comment.