diff --git a/main.go b/main.go index 48aaa12..59582f0 100644 --- a/main.go +++ b/main.go @@ -37,22 +37,22 @@ func patternToRegex(pattern string) (string, error) { } func scanDirectory(root string, prefix string, ignoredDirs map[string]struct{}, style ConnectorStyle, excludePatterns []string, maxDepth, currentDepth int) (string, error) { - logrus.Infof("Scanning directory: %s with prefix: %s", root, prefix) + logrus.Debugf("Scanning directory: %s with prefix: %s", root, prefix) var result strings.Builder entries, err := os.ReadDir(root) if err != nil { return "", fmt.Errorf("error reading directory %s: %v", root, err) } - + filteredEntries := []os.DirEntry{} for _, entry := range entries { if _, ok := ignoredDirs[entry.Name()]; ok { - logrus.Infof("Skipping ignored directory: %s", entry.Name()) + logrus.Debugf("Skipping ignored directory: %s", entry.Name()) continue } - + excluded := false for _, pattern := range excludePatterns { regexPattern, err := patternToRegex(pattern) @@ -64,7 +64,7 @@ func scanDirectory(root string, prefix string, ignoredDirs map[string]struct{}, return "", fmt.Errorf("error matching pattern %s: %v", regexPattern, err) } if matched { - logrus.Infof("Skipping excluded file/directory: %s", entry.Name()) + logrus.Debugf("Skipping excluded file/directory: %s", entry.Name()) excluded = true break } @@ -73,8 +73,8 @@ func scanDirectory(root string, prefix string, ignoredDirs map[string]struct{}, filteredEntries = append(filteredEntries, entry) } } - + if maxDepth != -1 && currentDepth >= maxDepth { return "", nil } @@ -128,12 +128,12 @@ func readDirIgnore(root string) (map[string]struct{}, error) { } func generateMarkdown(dir string, structure string) string { - logrus.Infof("Generating Markdown for directory: %s", dir) + logrus.Debugf("Generating Markdown for directory: %s", dir) return fmt.Sprintf("# Directory structure of %s\n\n```\n%s```\n", dir, structure) } func writeToFile(filename string, content string) error { - logrus.Infof("Writing to file: %s", filename) + logrus.Debugf("Writing to file: %s", filename) f, err := os.Create(filename) if err != nil { return fmt.Errorf("error creating file %s: %v", filename, err) @@ -162,16 +162,14 @@ func main() { branch string exclude []string maxDepth int + verbose bool ) var rootCmd = &cobra.Command{ - Use: "dirscanner", + Use: "dirscanner [flags]", Short: "A CLI tool to scan directories and generate a Markdown file with the structure", + Args: cobra.MinimumNArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) < 2 { - return fmt.Errorf("usage: dirscanner ") - } - dir := args[0] output := ensureMdExtension(args[1]) @@ -187,6 +185,9 @@ func main() { Branch: branch, } + if verbose { + logrus.SetLevel(logrus.DebugLevel) + } structure, err := scanDirectory(dir, "", ignoredDirs, style, exclude, maxDepth, 0) if err != nil { return fmt.Errorf("error scanning directory: %v", err) @@ -209,6 +210,7 @@ func main() { rootCmd.Flags().StringVar(&branch, "branch", "│ ", "Branch for intermediate nodes") rootCmd.Flags().StringSliceVar(&exclude, "exclude", []string{}, "Exclude files or directories matching these patterns (e.g., '*.txt')") rootCmd.Flags().IntVar(&maxDepth, "depth", -1, "Limit the depth of the directory traversal (-1 for no limit)") + rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Whether or not to show debug messages") if err := rootCmd.Execute(); err != nil { logrus.Fatal(err)