code2md golang
D:\GolangTools\src\config\config.go
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
}
D:\GolangTools\src\input\inputHandler.go
package input
import (
"fmt"
"os"
"strings"
)
type IInputHandler interface {
HandleInputArgs()
}
type InputHandler struct {
function string
inputPath string
}
func (my *InputHandler) HandleInputArgs() error {
// os.Args 是一个字符串切片,存储了命令行参数
// 其中 os.Args[0] 是程序本身的名称,后续元素是传递的参数
args := os.Args
if len(args) < 3 {
return fmt.Errorf("参数不足,请输入两个参数")
}
for i, arg := range args {
if i == 0 {
continue
}
// fmt.Printf("参数 %d 是: %s\n", i, arg)
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
}
D:\GolangTools\src\path\pathHandler.go
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()
// 使用 os.ReadDir 读取目录
topDirs, err := my.GetTopDirs(inputPath)
if err != nil {
return nil, err
}
var producerWg sync.WaitGroup
var consumerWg sync.WaitGroup
// 创建一个容量为 3 的有缓冲的 int 类型 channel
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)
// break
}
consumerWg.Add(1)
go func() {
defer consumerWg.Done()
my.CollectDeepDirs(bufferedChannel, &consumerWg)
}()
// 关闭通道的 goroutine
go func() {
producerWg.Wait()
close(bufferedChannel)
}()
// 等待所有消费者 goroutines 完成
consumerWg.Wait()
// 关闭 channel
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() {
// fmt.Println("子文件夹:", entry.Name())
// fmt.Println("子文件夹:", path.Join(inputPath, entry.Name()))
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)
}
// folderPaths := make([]string, 0)
// 遍历目录中的条目
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
}
D:\GolangTools\src\utils\utils.go
package utils
func ExsitInSlice(slice []string, str string) bool {
for _, s := range slice {
if s == str {
return true
}
}
return false
}
D:\GolangTools\go.mod
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)
}
D:\GolangTools\run.bat
@REM set scriptPath=D:\DotnetTools
set scriptPath=%cd%
go run main.go --function=code2md --folderpath="%scriptPath%"
D:\GolangTools\zz_config_code2md.json
{"IncludeDirs": [],"IncludeFiles": [],"ExcludeDirs": [],"ExcludeFiles": []}
标签:return,string,code2md,golang,func,inputPath,GolangTools,my
From: https://www.cnblogs.com/zhuoss/p/18678923