-
-
Save rberenguel/0c7b4b46b881b11f4f0542c23a0ba618 to your computer and use it in GitHub Desktop.
Gotllo, it's like Motllo but in Go and does less things.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
"strings" | |
) | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Println("Usage: gotllo <path1> [<path2> ...] [-tree=<path-for-tree>] [-exclude=<excluded file names or paths>]") | |
return | |
} | |
treePath := "" | |
excludePaths := []string{} | |
for _, arg := range os.Args[1:] { | |
if strings.HasPrefix(arg, "-tree=") { | |
treePath = strings.TrimPrefix(arg, "-tree=") | |
} else if strings.HasPrefix(arg, "-exclude=") { | |
excludePaths = append(excludePaths, strings.Split(strings.TrimPrefix(arg, "-exclude="), ",")...) | |
} | |
} | |
for _, path := range os.Args[1:] { | |
if strings.HasPrefix(path, "-tree=") { | |
continue | |
} | |
filepath.Walk(path, func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
return err | |
} | |
if !info.IsDir() { | |
for _, excludedGlob := range excludePaths { | |
matched, err := filepath.Match(excludedGlob, filepath.Base(path)) | |
//fmt.Println(excludedGlob, filepath.Base(path)) | |
if err != nil { | |
return err // Handle the error appropriately | |
} | |
if matched { | |
return nil // Skip this file | |
} | |
} | |
matched, _ := filepath.Match("README.md", filepath.Base(path)) | |
//if !matched { | |
fmt.Printf("# %s\n", path) | |
//} | |
content, err := os.ReadFile(path) | |
if err != nil { | |
return err | |
} | |
if matched { | |
if treePath != "" { | |
treeOutput := &strings.Builder{} | |
printTree(treePath, "", excludePaths, treeOutput) // Pass treeOutput to printTree | |
content = []byte(strings.Replace(string(content), "{{ tree }}", treeOutput.String(), 1)) | |
} | |
} | |
if !matched { | |
fmt.Println("\n```") | |
} | |
fmt.Println(string(content)) | |
if !matched { | |
fmt.Println("```") | |
} | |
fmt.Println() | |
} | |
return nil | |
}) | |
} | |
/*if treePath != "" { | |
fmt.Println("## Directory Tree for:", treePath) | |
printTree(treePath, "", excludePaths) | |
}*/ | |
} | |
func printTree(path string, prefix string, excludePaths []string, output *strings.Builder) { | |
files, err := os.ReadDir(path) | |
if err != nil { | |
fmt.Println("Error reading directory:", err) | |
return | |
} | |
for i, file := range files { | |
fullPath := filepath.Join(path, file.Name()) | |
// Check if the current path should be excluded | |
excluded := false | |
for _, excludePath := range excludePaths { | |
if strings.HasPrefix(fullPath, excludePath) { | |
excluded = true | |
break | |
} | |
} | |
if excluded { | |
continue | |
} | |
if file.Name()[0] == '.' { | |
continue | |
} | |
if i == len(files)-1 { | |
output.WriteString(prefix + "└── " + file.Name() + "\n") | |
} else { | |
output.WriteString(prefix + "├── " + file.Name() + "\n") | |
} | |
if file.IsDir() { | |
newPrefix := prefix | |
if i == len(files)-1 { | |
newPrefix += " " | |
} else { | |
newPrefix += "│ " | |
} | |
printTree(filepath.Join(path, file.Name()), newPrefix, excludePaths, output) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment