feat: add Create, Open, Mkdir (by AI)

This commit is contained in:
Star 2026-05-12 14:33:33 +08:00
parent 2459bfd882
commit 8bb1b905b3
5 changed files with 229 additions and 9 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.ai/
.geminiignore
.gemini
/CODE-FULL.md

4
CHANGELOG-LATEST.md Normal file
View File

@ -0,0 +1,4 @@
## [1.0.7] - 2026-05-06
- **设计哲学对齐**:全面废除 `Must` 前缀函数(`MustRead`, `MustReadBytes`, `MustReadLines`, `MustReadDir`, `MustGzip`, `MustGunzip`, `MustZip`, `MustUnzip`),改为配合 `go/cast``As` 函数消除摩擦。
- **内部优化**:重构 `memory.go` 以移除对废弃 `Must` 函数的内部依赖。

206
CODE-SUMMARY.md Normal file
View File

@ -0,0 +1,206 @@
### file > object.go
```go
var fileLocksLock = sync.Mutex{}
var fileLocks = map[string]*sync.Mutex{}
func getLock(filename string) *sync.Mutex
// UnmarshalFile 从文件读取数据并映射到 to自动处理 YAML/JSON 格式判别及智能字段映射。
func UnmarshalFile(filename string, to any) error
// MarshalFile 将数据序列化并写入文件,支持 YAML/JSON 自动判别。
func MarshalFile(filename string, data any) error
// MarshalFilePretty 类似 MarshalFile但会输出带缩进的格式化内容。
func MarshalFilePretty(filename string, data any) error
func marshalFile(filename string, data any, indent bool) error
func PatchFile(filename string, patch any) error
```
### file > memory.go
```go
type MemFile struct {
Name string
AbsName string
ModTime time.Time
IsDir bool
Compressed bool
Size int64
Data []byte
SafeData *safe.SafeBuf
}
type MemFileB64 struct {
Name string
ModTime time.Time
IsDir bool
DataB64 []byte
Compressed bool
Size int64
Children []MemFileB64
}
var (
memFiles = make(map[string]*MemFile)
memFilesByDir = make(map[string][]MemFile)
memFilesLock sync.RWMutex
)
func (mf *MemFile) GetData() []byte
func (mf *MemFile) GetSafeData() *safe.SecretPlaintext
func GetAbsFilename(filename string) string
func AddFileToMemory(mf MemFile)
func ReadFileFromMemory(name string) *MemFile
func ReadDirFromMemory(name string) []MemFile
func LoadFileToMemory(filename string)
func SafeLoadFileToMemory(filename string)
func LoadFileToMemoryWithCompress(filename string)
func SafeLoadFileToMemoryWithCompress(filename string)
func loadFileToMemory(filename string, compress bool, isSafe bool)
func LoadFileToB64(filename string) *MemFileB64
func LoadFilesToMemoryFromB64(b64File *MemFileB64)
func LoadFilesToMemoryFromB64KeepGzip(b64File *MemFileB64)
func LoadFilesToMemoryFromJson(jsonFiles string)
```
### file > archive.go
```go
func Compress(data []byte, cType string) ([]byte, error)
func Decompress(data []byte, cType string) ([]byte, error)
func Extract(srcFile, destDir string, stripRoot bool) error
func extractTar(r io.Reader, dest string, strip bool) error
func extractZip(src, dest string, strip bool) error
func extractSingleFile(r io.Reader, destDir, fileName string) error
func writeEntry(destDir, name string, mode os.FileMode, isDir bool, linkPath string, r io.Reader, strip bool) error
func Archive(srcPath, destFile string) error
func walkAndAdd(srcPath string, addFn func(string, os.FileInfo, io.Reader) error) error
```
### file > file.go
```go
type FileInfo struct {
Name string
FullName string
IsDir bool
Size int64
ModTime int64
}
func EnsureParentDir(filename string)
func EnsureDirSuffix(path string) string
func Exists(filename string) bool
func GetFileInfo(filename string) *FileInfo
func ReadBytes(filename string) ([]byte, error)
func Read(filename string) (string, error)
func ReadLines(filename string) ([]string, error)
func WriteBytes(filename string, content []byte) error
func Write(filename string, content string) error
func Copy(from, to string) error
func CopyToFile(from io.Reader, to string) error
func Remove(path string) error
func Move(src, dst string) error
func Replace(filename, old, new string) error
func Search(dir, pattern string) []string
func ReadDir(filename string) ([]FileInfo, error)
func RunCommand(command string, args ...string) ([]string, error)
```

13
file.go
View File

@ -175,6 +175,19 @@ func Remove(path string) error {
return os.RemoveAll(path)
}
func Mkdir(path string) error {
return os.MkdirAll(path, 0755)
}
func Open(filename string) (*os.File, error) {
return os.Open(filename)
}
func Create(filename string) (*os.File, error) {
EnsureParentDir(filename)
return os.Create(filename)
}
func Move(src, dst string) error {
EnsureParentDir(dst)
return os.Rename(src, dst)

12
go.sum
View File

@ -1,11 +1,7 @@
apigo.cc/go/cast v1.2.10 h1:wa9/hz6GW6Z+5co6l7LftMn2Eo06WpVHHDCCQphnmH8=
apigo.cc/go/cast v1.2.10/go.mod h1:lGlwImiOvHxG7buyMWhFzcdvQzmSaoKbmr7bcDfUpHk=
apigo.cc/go/encoding v1.1.2 h1:reSrLkyYrtZsf4S91XPdyBY2AQpvA43n9q0Q9wz5uJA=
apigo.cc/go/encoding v1.1.2/go.mod h1:iLuvrYHEK8mLnk8jijx5Sv1tInFreny0yGNBouA1d20=
apigo.cc/go/rand v1.0.6 h1:p51rkaDrYUdZPIRbQAujZmQelWg2ipAMts33A/tG7QE=
apigo.cc/go/rand v1.0.6/go.mod h1:mZ/4Soa3bk+XvDaqPWJuUe1bfEi4eThBj1XmEAuYxsk=
apigo.cc/go/safe v1.0.7 h1:f0d+v9K2dHPyG5DNqhyddCmAmSiIqIfkPi/AMED/iQI=
apigo.cc/go/safe v1.0.7/go.mod h1:Hu7TVDWPe/I+nBZfYJH4mt+ROzG+rwk2D1zHTXj/2eE=
apigo.cc/go/cast v1.3.0 h1:ZTcLYijkqZjSWSCSpJUWMfzJYeJKbwKxquKkPrFsROQ=
apigo.cc/go/encoding v1.3.0 h1:8jqNHoZBR8vOU/BGsLFebfp1Txa1UxDRpd7YwzIFLJs=
apigo.cc/go/rand v1.3.0 h1:k+UFAhMySwXf+dq8Om9TniZV6fm6gAE0evbrqMEdwQU=
apigo.cc/go/safe v1.3.0 h1:uctdAUsphT9p60Tk4oS5xPCe0NoIdOHfsYv4PNS0Rok=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=