MCPcopy Create free account
hub / github.com/astercloud/aster / demonstrateEnsureDir

Function demonstrateEnsureDir

examples/config-paths/main.go:89–131  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

87}
88
89func demonstrateEnsureDir() {
90 fmt.Println("\n📋 确保目录存在")
91 fmt.Println(repeatStr("-", 50))
92
93 // 创建临时目录用于测试
94 tmpDir, err := os.MkdirTemp("", "aster-config-demo")
95 if err != nil {
96 fmt.Printf(" ❌ 创建临时目录失败: %v\n", err)
97 return
98 }
99 defer func() { _ = os.RemoveAll(tmpDir) }()
100
101 // 测试目录
102 testPath := filepath.Join(tmpDir, "nested", "deep", "directory")
103
104 // 确保目录存在
105 err = config.EnsureDir(testPath)
106 if err != nil {
107 fmt.Printf(" ❌ 创建目录失败: %v\n", err)
108 return
109 }
110
111 // 验证目录已创建
112 if info, err := os.Stat(testPath); err == nil && info.IsDir() {
113 fmt.Printf(" ✓ 目录已创建: %s\n", testPath)
114 }
115
116 // 文件路径 - EnsureDir 会创建父目录
117 filePath := filepath.Join(tmpDir, "another", "path", "file.txt")
118 err = config.EnsureDir(filepath.Dir(filePath))
119 if err == nil {
120 fmt.Printf(" ✓ 父目录已创建: %s\n", filepath.Dir(filePath))
121 }
122
123 // 确保所有标准目录存在
124 fmt.Println("\n 确保所有标准目录存在:")
125 err = config.EnsureAllDirs()
126 if err != nil {
127 fmt.Printf(" ❌ 创建标准目录失败: %v\n", err)
128 } else {
129 fmt.Println(" ✓ 所有标准目录已创建")
130 }
131}
132
133func repeatStr(s string, n int) string {
134 result := ""

Callers 1

mainFunction · 0.85

Calls 6

EnsureDirFunction · 0.92
EnsureAllDirsFunction · 0.92
PrintfMethod · 0.80
repeatStrFunction · 0.70
StatMethod · 0.65
JoinMethod · 0.45

Tested by

no test coverage detected