got Comment objects returning properly

This commit is contained in:
specCon18 2025-03-17 13:37:24 -04:00
parent 1722e3128f
commit 164d84c391
2 changed files with 32 additions and 10 deletions

View file

@ -11,10 +11,17 @@ import (
"path/filepath" "path/filepath"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"strings" "strings"
"strconv"
"bufio" "bufio"
"regexp" "regexp"
) )
type Comment struct {
FilePath string
LineNumber int
Priority int
Task string
}
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@ -39,8 +46,8 @@ Usage:
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
path, _ := cmd.Flags().GetString("path") path, _ := cmd.Flags().GetString("path")
if path != "" { if path != "" {
//TODO: recursivly read each file in the path and store content as string in slice comments := readFileTree(path)
readFileTree(path) fmt.Println(comments)
} else { } else {
fmt.Println("ERROR: No path provided, please provide path using the -p flag") fmt.Println("ERROR: No path provided, please provide path using the -p flag")
os.Exit(1) os.Exit(1)
@ -60,8 +67,9 @@ func Execute() {
func init() { func init() {
rootCmd.PersistentFlags().StringP("path", "p", "","assign the path to get comments from.") rootCmd.PersistentFlags().StringP("path", "p", "","assign the path to get comments from.")
} }
func readFileTree(path string) { func readFileTree(path string) []Comment {
fsys := os.DirFS(path) fsys := os.DirFS(path)
var comments []Comment
// Walk through all files in the directory // Walk through all files in the directory
fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error { fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
@ -80,11 +88,13 @@ func readFileTree(path string) {
// Process each line in the file // Process each line in the file
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
ln := 0
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
ln++
//TODO: P:0 add support for TODO's not at start of line //TODO: P:0 add support for TODO's not at start of line
// Check for C-style comments (// and /* */) //TODO: P:0 add multiline support /* */
// Check for C-style comments (// and )
if strings.HasPrefix(strings.TrimSpace(line), "//TODO:") { if strings.HasPrefix(strings.TrimSpace(line), "//TODO:") {
// Regular expression to match "P:" followed by a digit // Regular expression to match "P:" followed by a digit
re := regexp.MustCompile(`P:(\d)`) re := regexp.MustCompile(`P:(\d)`)
@ -100,9 +110,20 @@ func readFileTree(path string) {
if len(matches) > 1 { if len(matches) > 1 {
// Remove the "P:<digit>" from the beginning of the line // Remove the "P:<digit>" from the beginning of the line
trimmedLine = strings.Replace(trimmedLine, matches[0], "", 1) // Replace only the first occurrence trimmedLine = strings.Replace(trimmedLine, matches[0], "", 1) // Replace only the first occurrence
fmt.Printf("comment: %s | priority: %s \n", strings.TrimSpace(trimmedLine), matches[1]) // Store the file_path, line_number, comment, and priority as vars to be used in constructing a Comment object.
priority,err := strconv.Atoi(matches[1])
if err != nil {
return err
}
file,task := filepath.Join(path, p), strings.TrimSpace(trimmedLine)
comment := Comment{file,ln,priority,task}
comments = append(comments,comment)
} else { } else {
fmt.Printf("comment: %s | priority: 9 \n", trimmedLine) file,task := filepath.Join(path, p), strings.TrimSpace(trimmedLine)
comment := Comment{file,ln,9,task}
comments = append(comments,comment)
} }
} }
@ -115,4 +136,5 @@ func readFileTree(path string) {
} }
return nil return nil
}) })
return comments
} }

View file

@ -3,7 +3,7 @@ Copyright © 2025 Steven Carpenter <steven.carpenter@skdevstudios.com>
*/ */
package main package main
//TODO: P:4 testing todo in other file
import "speccon18/yunodo/v2/cmd" import "speccon18/yunodo/v2/cmd"
func main() { func main() {