code2md golang
| |
| package config |
| |
| type ConfigHandler struct { |
| includeDirNames []string |
| includeFileNames []string |
| excludeDirNames []string |
| excludeFileNames []string |
| } |
| |
| func NewConfigHandler(includeDirNames []string, includeFileNames []string, excludeDirNames []string, excludeFileNames []string) *ConfigHandler { |
| return &ConfigHandler{ |
| includeDirNames: includeDirNames, |
| includeFileNames: includeFileNames, |
| excludeDirNames: excludeDirNames, |
| excludeFileNames: excludeFileNames, |
| } |
| } |
| |
| func (my *ConfigHandler) SetIncludeDirNames(includeDirNames []string) { |
| my.includeDirNames = includeDirNames |
| } |
| func (my *ConfigHandler) SetIncludeFileNames(includeFileNames []string) { |
| my.includeFileNames = includeFileNames |
| } |
| func (my *ConfigHandler) SetExcludeDirNames(excludeDirNames []string) { |
| my.excludeDirNames = excludeDirNames |
| } |
| func (my *ConfigHandler) SetExcludeFileNames(excludeFileNames []string) { |
| my.excludeFileNames = excludeFileNames |
| } |
| |
| func (my *ConfigHandler) GetIncludeDirNames() []string { |
| return my.includeDirNames |
| } |
| func (my *ConfigHandler) GetIncludeFileNames() []string { |
| return my.includeFileNames |
| } |
| func (my *ConfigHandler) GetExcludeDirNames() []string { |
| return my.excludeDirNames |
| } |
| func (my *ConfigHandler) GetExcludeFileNames() []string { |
| return my.excludeFileNames |
| } |
| |
| |
| package input |
| |
| import ( |
| "fmt" |
| "os" |
| "strings" |
| ) |
| |
| type IInputHandler interface { |
| HandleInputArgs() |
| } |
| |
| type InputHandler struct { |
| function string |
| inputPath string |
| } |
| |
| func (my *InputHandler) HandleInputArgs() error { |
| |
| |
| args := os.Args |
| if len(args) < 3 { |
| return fmt.Errorf("参数不足,请输入两个参数") |
| } |
| for i, arg := range args { |
| if i == 0 { |
| continue |
| } |
| |
| parts := strings.Split(arg, "=") |
| if len(parts) != 2 { |
| return fmt.Errorf("参数格式错误,请使用格式: key=value") |
| } |
| switch parts[0] { |
| case "--function": |
| my.function = parts[1] |
| continue |
| case "--folderpath": |
| my.inputPath = parts[1] |
| continue |
| default: |
| return fmt.Errorf("未知参数: %s", parts[0]) |
| } |
| } |
| return nil |
| } |
| |
| func NewInputHandler() *InputHandler { |
| return &InputHandler{} |
| } |
| |
| func (my *InputHandler) GetFunction() string { |
| return my.function |
| } |
| |
| func (my *InputHandler) GetInputPath() string { |
| return my.inputPath |
| } |
| |
| func (my *InputHandler) SetFunction(function string) { |
| my.function = function |
| } |
| |
| func (my *InputHandler) SetInputPath(inputPath string) { |
| my.inputPath = inputPath |
| } |
| |
| |
| package path |
| |
| import ( |
| "GolangTools/src/config" |
| "GolangTools/src/input" |
| "GolangTools/src/utils" |
| "fmt" |
| "os" |
| "path" |
| "path/filepath" |
| "sync" |
| ) |
| |
| type IPathHandler interface { |
| GetAllCodeFiles() []string |
| } |
| type PathHandler struct { |
| allFilePaths []string |
| inputHandler *input.InputHandler |
| configHandler *config.ConfigHandler |
| } |
| |
| func NewPathHandler(inputHandler *input.InputHandler, configHandler *config.ConfigHandler) *PathHandler { |
| return &PathHandler{ |
| inputHandler: inputHandler, |
| configHandler: configHandler, |
| } |
| } |
| func (my *PathHandler) GetAllCodeFiles() ([]string, error) { |
| |
| inputPath := my.inputHandler.GetInputPath() |
| |
| topDirs, err := my.GetTopDirs(inputPath) |
| if err != nil { |
| return nil, err |
| } |
| |
| var producerWg sync.WaitGroup |
| var consumerWg sync.WaitGroup |
| |
| |
| bufferedChannel := make(chan string, 100) |
| |
| for _, topDir := range topDirs { |
| folderName := filepath.Base(topDir) |
| if utils.ExsitInSlice(my.configHandler.GetExcludeDirNames(), folderName) { |
| if !utils.ExsitInSlice(my.configHandler.GetIncludeDirNames(), folderName) { |
| continue |
| } else { |
| println("skip:", path.Join(inputPath, folderName)) |
| } |
| } |
| producerWg.Add(1) |
| go func(topDir string) { |
| defer producerWg.Done() |
| my.TraverseDir(topDir, bufferedChannel, &producerWg) |
| }(topDir) |
| |
| } |
| |
| consumerWg.Add(1) |
| go func() { |
| defer consumerWg.Done() |
| my.CollectDeepDirs(bufferedChannel, &consumerWg) |
| }() |
| |
| go func() { |
| producerWg.Wait() |
| close(bufferedChannel) |
| }() |
| |
| consumerWg.Wait() |
| |
| println("收集完毕Done!!!") |
| println(len(my.allFilePaths)) |
| return nil, nil |
| } |
| |
| func (my *PathHandler) GetTopDirs(inputPath string) ([]string, error) { |
| entries, err := os.ReadDir(inputPath) |
| if err != nil { |
| return nil, fmt.Errorf("读取目录:%s出错:%v", inputPath, err) |
| } |
| folderPaths := make([]string, 0) |
| |
| for _, entry := range entries { |
| if entry.IsDir() { |
| |
| |
| folderPaths = append(folderPaths, path.Join(inputPath, entry.Name())) |
| } |
| } |
| return folderPaths, nil |
| } |
| |
| func (my *PathHandler) TraverseDir(inputPath string, c chan<- string, wg *sync.WaitGroup) error { |
| entries, err := os.ReadDir(inputPath) |
| if err != nil { |
| return fmt.Errorf("读取目录:%s出错:%v", inputPath, err) |
| } |
| |
| |
| for _, entry := range entries { |
| if entry.IsDir() { |
| folderName := entry.Name() |
| if !utils.ExsitInSlice(my.configHandler.GetExcludeDirNames(), folderName) { |
| my.TraverseDir(path.Join(inputPath, folderName), c, wg) |
| continue |
| } |
| if !utils.ExsitInSlice(my.configHandler.GetIncludeDirNames(), folderName) { |
| println("skip:", path.Join(inputPath, folderName)) |
| continue |
| } |
| my.TraverseDir(path.Join(inputPath, folderName), c, wg) |
| } else { |
| fileName := entry.Name() |
| if !utils.ExsitInSlice(my.configHandler.GetExcludeFileNames(), fileName) { |
| println("copy:", path.Join(inputPath, fileName)) |
| c <- path.Join(inputPath, fileName) |
| continue |
| } |
| if !utils.ExsitInSlice(my.configHandler.GetIncludeFileNames(), fileName) { |
| println("skip:", path.Join(inputPath, fileName)) |
| continue |
| } |
| c <- path.Join(inputPath, entry.Name()) |
| } |
| } |
| return nil |
| } |
| |
| func (my *PathHandler) CollectDeepDirs(c <-chan string, wg *sync.WaitGroup) error { |
| for data := range c { |
| my.allFilePaths = append(my.allFilePaths, data) |
| } |
| return nil |
| } |
| |
| |
| package utils |
| |
| func ExsitInSlice(slice []string, str string) bool { |
| for _, s := range slice { |
| if s == str { |
| return true |
| } |
| } |
| return false |
| } |
| |
| |
| module GolangTools |
| |
| go 1.23.0 |
| |
D:\GolangTools\main.go
| |
| package main |
| |
| import ( |
| "GolangTools/src/config" |
| "GolangTools/src/input" |
| "GolangTools/src/path" |
| "fmt" |
| "time" |
| ) |
| |
| func main() { |
| |
| start := time.Now() |
| var includeDirNames []string = []string{"", ""} |
| var includeFileNames []string = []string{"", ""} |
| var excludeDirNames []string = []string{".git", "obj", "bin", ".vscode"} |
| var excludeFileNames []string = []string{"", ""} |
| configHandler := config.NewConfigHandler(includeDirNames, includeFileNames, excludeDirNames, excludeFileNames) |
| inputHandler := input.NewInputHandler() |
| err := inputHandler.HandleInputArgs() |
| if err != nil { |
| panic(err) |
| } |
| pathHandler := path.NewPathHandler(inputHandler, configHandler) |
| pathHandler.GetAllCodeFiles() |
| duration := time.Since(start) |
| fmt.Printf("程序执行时间: %v\n", duration) |
| } |
| |
| |
| @REM set scriptPath=D:\DotnetTools |
| set scriptPath=%cd% |
| go run main.go --function=code2md --folderpath="%scriptPath%" |
| |
| |
| |
| {"IncludeDirs": [],"IncludeFiles": [],"ExcludeDirs": [],"ExcludeFiles": []} |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
2024-01-19 convert code 2 markdown