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

fix: added filepath walk to traverse through files in sub-dirs #4

Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ go 1.22.5
require (
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"flag"
"fmt"
"os"

"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
)

func main() {
Expand All @@ -15,7 +16,7 @@ func main() {
var file_details []File_details
var folder_count int32
var is_git_initialized bool = false
files, err := os.ReadDir(".")
files, err := getFiles()
if err != nil {
fmt.Println("Error:", err)
return
Expand Down Expand Up @@ -74,3 +75,14 @@ func main() {
}
}
}

func getFiles() ([]os.FileInfo, error) {
var files []os.FileInfo

err := filepath.Walk(".", func(path string, f os.FileInfo, err error) error {
files = append(files, f)
logrus.Debugf("Visited: %s\n", path)
return nil
})
return files, err
}
Binary file added stto
Binary file not shown.
7 changes: 3 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"io/fs"
"os"
"strings"
)
Expand Down Expand Up @@ -44,7 +43,7 @@ func countLines(file_name string, ext string) (int32, int32, int32, int32) {
return code, gap, comments, (code + gap + comments)
}

func addNewEntry(file fs.DirEntry, ext string, file_details *[]File_details) {
func addNewEntry(file os.FileInfo, ext string, file_details *[]File_details) {
code, gap, comments, line_count := countLines(file.Name(), ext)
*file_details = append(*file_details, File_details{
ext: ext,
Expand All @@ -56,7 +55,7 @@ func addNewEntry(file fs.DirEntry, ext string, file_details *[]File_details) {
})
}

func updateExistingEntry(file fs.DirEntry, ext string, file_details *[]File_details, check *bool) {
func updateExistingEntry(file os.FileInfo, ext string, file_details *[]File_details, check *bool) {
for i := range *file_details {
if (*file_details)[i].ext == ext {
*check = true
Expand All @@ -71,7 +70,7 @@ func updateExistingEntry(file fs.DirEntry, ext string, file_details *[]File_deta
}
}

func getFileDetails(file fs.DirEntry, file_details *[]File_details, folder_count *int32, is_git_initialized *bool) {
func getFileDetails(file os.FileInfo, file_details *[]File_details, folder_count *int32, is_git_initialized *bool) {
if file.IsDir() {
if file.Name() == ".git" && *is_git_initialized == false {
*is_git_initialized = true
Expand Down