added filetype check
This commit is contained in:
parent
0f9ceffeff
commit
f01ec4ca94
1 changed files with 93 additions and 10 deletions
89
cmd/root.go
89
cmd/root.go
|
|
@ -10,6 +10,8 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"strings"
|
||||||
|
"bufio"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -57,16 +59,97 @@ 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) {
|
||||||
var count int
|
var count int
|
||||||
fsys := os.DirFS(path)
|
fsys := os.DirFS(path)
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if file has .go extension
|
||||||
if filepath.Ext(p) == ".go" {
|
if filepath.Ext(p) == ".go" {
|
||||||
//TODO:add case statement to check for C style comments i.e. // and /* as well as luas -- and other langs #
|
// Read the file and check for comments
|
||||||
|
file, err := os.Open(filepath.Join(path, p))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Process each line in the file
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
|
||||||
|
// Check for C-style comments (// and /* */)
|
||||||
|
if strings.Contains(strings.TrimSpace(line), "//") {
|
||||||
|
count++
|
||||||
|
fmt.Printf("comment: %s | current comment count:%d\n",strings.TrimSpace(line),count)
|
||||||
|
}
|
||||||
|
if strings.Contains(line, "/*") && strings.Contains(line, "*/") {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if there was any error reading the file
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if filepath.Ext(p) == ".lua" {
|
||||||
|
// Read the file and check for comments
|
||||||
|
file, err := os.Open(filepath.Join(path, p))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Process each line in the file
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
|
||||||
|
// Check for Lua-style comments (--)
|
||||||
|
if strings.Contains(strings.TrimSpace(line), "--") {
|
||||||
|
count++
|
||||||
|
fmt.Printf("comment: %s | current comment count:%d\n",strings.TrimSpace(line),count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Check if there was any error reading the file
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if filepath.Ext(p) == ".py" {
|
||||||
|
// Read the file and check for comments
|
||||||
|
file, err := os.Open(filepath.Join(path, p))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Process each line in the file
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
|
||||||
|
// Check for Python-style comments (#)
|
||||||
|
if strings.Contains(strings.TrimSpace(line), "#") {
|
||||||
|
count++
|
||||||
|
fmt.Printf("comment: %s | current comment count:%d\n",strings.TrimSpace(line),count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Check if there was any error reading the file
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
fmt.Println(count)
|
|
||||||
|
// Print the number of comments found
|
||||||
|
fmt.Println("Comment count:", count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue