Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements --verbose flag and incorporates Arg handling within cobra #1

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -162,16 +162,14 @@ func main() {
branch string
exclude []string
maxDepth int
verbose bool
)

var rootCmd = &cobra.Command{
Use: "dirscanner",
Use: "dirscanner <directory to scan> <output markdown file> [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 <directory to scan> <output markdown file>")
}

dir := args[0]
output := ensureMdExtension(args[1])

Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading