Skip to content

Commit

Permalink
Feature: add flag to ignore file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
zeddo123 committed Aug 31, 2023
1 parent 9beacff commit 31aa6c0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ go.work
# Output of csvcat
*.csv
csvcat
csvset
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Number of files found: 100
Usage of ./csvcat:
-batch int
Batch size (default 30)
-c Set to false to ignore checking extension (default true)
-columns string
Columns to be selected
-concurrency
Expand Down
5 changes: 3 additions & 2 deletions csvcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func main() {
batchSize := flag.Int("batch", 30, "Batch size")
concurrently := flag.Bool("concurrency", true, "Set flag to disable concurrency")
verbose := flag.Bool("v", false, "Set to true to have verbose output")
checkExtension := flag.Bool("c", true, "Set to false to ignore checking extension")
flag.Parse()

// Disabling the logger
Expand All @@ -31,9 +32,9 @@ func main() {

start := time.Now()
if *concurrently {
files.AsyncCompileFiles(*directory, *delim, *output_file, *csvColumns, *batchSize)
files.AsyncCompileFiles(*directory, *delim, *output_file, *csvColumns, *batchSize, *checkExtension)
} else {
files.CompileFiles(*directory, *delim, *output_file, *csvColumns)
files.CompileFiles(*directory, *delim, *output_file, *csvColumns, *checkExtension)
}
fmt.Println("============ Total", time.Now().Sub(start), "===================")
}
Expand Down
16 changes: 9 additions & 7 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"fmt"
)

func AsyncCompileFiles(directory string, csvDelimiter string, output string, csvColumns string, batchSize int) {
var files = getfilenames(directory)
func AsyncCompileFiles(directory string, csvDelimiter string, output string, csvColumns string, batchSize int, checkFileExtension bool) {
var files = getfilenames(directory, checkFileExtension)

targetCols := strings.Split(csvColumns, csvDelimiter)

Expand Down Expand Up @@ -54,9 +54,9 @@ func AsyncCompileFiles(directory string, csvDelimiter string, output string, csv
}
}

func CompileFiles(directory string, csvDelimiter string, output string, csvColumns string) {
func CompileFiles(directory string, csvDelimiter string, output string, csvColumns string, checkFileExtension bool) {
// get files in the directory
var files = getfilenames(directory)
var files = getfilenames(directory, checkFileExtension)

targetCols := strings.Split(csvColumns, csvDelimiter)

Expand All @@ -81,7 +81,7 @@ func CompileFiles(directory string, csvDelimiter string, output string, csvColum
}
}

func getfilenames(directory string) []string {
func getfilenames(directory string, checkFileExtension bool) []string {
var files []string

entries, err := os.ReadDir(directory)
Expand All @@ -92,8 +92,10 @@ func getfilenames(directory string) []string {

for _, e := range entries {
// check if entry is a file and has correct file type
if e.Type().IsRegular() && strings.HasSuffix(e.Name(), ".csv") {
files = append(files, filepath.Join(path, e.Name()))
if e.Type().IsRegular() {
if !checkFileExtension || strings.HasSuffix(e.Name(), ".csv") {
files = append(files, filepath.Join(path, e.Name()))
}
}
}

Expand Down

0 comments on commit 31aa6c0

Please sign in to comment.