Skip to content

Commit 683efcf

Browse files
committed
first commit
0 parents  commit 683efcf

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
This is a simple program I made because I am lazy.
2+
It grabs all the source files in a given directory
3+
recursively, grabbing any source files in child
4+
directories and outputting them to be taken as the
5+
input into gcc.
6+
7+
Usage:
8+
sourcefiles <path> <File extension 1> [File extension 2 ...]
9+
10+
Example:
11+
$ sourcefiles ~/Projects/lmc/ h cpp
12+
> /home/ben/Projects/lmc/src/cpu.h /home/ben/Projects/lmc/src/main.cpp /home/ben/Projects/lmc/src/main.h
13+
14+
Peak lazyness but it works

cmd/sourcefiles/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module sourcefiles/cmd/sourcefiles
2+
3+
go 1.15

cmd/sourcefiles/main.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
func contains(s []string, e string) bool {
11+
for _, a := range s {
12+
if a == e {
13+
return true
14+
}
15+
}
16+
return false
17+
}
18+
19+
func getFilesInDirectory(path string, extensions []string) []string {
20+
var outputSlice []string
21+
err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
22+
// Check if the file is a directory, if it is recurse and grab it's files
23+
// If its a file check its extension, if it matches one of the provided
24+
// file extensions, append it to the output array
25+
if info.IsDir() {
26+
if filePath != path {
27+
outputSlice = append(outputSlice, getFilesInDirectory(filePath, extensions)...)
28+
}
29+
} else {
30+
if contains(extensions, strings.Replace(filepath.Ext(filePath), ".", "", 1)) {
31+
outputSlice = append(outputSlice, filePath)
32+
}
33+
}
34+
return nil
35+
})
36+
37+
if err != nil {
38+
panic(err)
39+
}
40+
return outputSlice
41+
}
42+
43+
func main() {
44+
args := os.Args[1:]
45+
extensions := os.Args[2:]
46+
if len(args) <= 0 {
47+
fmt.Printf("Not enough arguments\n")
48+
return
49+
}
50+
if len(extensions) <= 0 {
51+
fmt.Printf("Not enough extensions\n")
52+
return
53+
}
54+
var files []string
55+
var filesNoDuplicates []string
56+
files = getFilesInDirectory(args[0], extensions)
57+
for i := 0; i < len(files); i++ {
58+
if contains(filesNoDuplicates, files[i]) == false {
59+
filesNoDuplicates = append(filesNoDuplicates, files[i])
60+
}
61+
}
62+
var output string
63+
for i := 0; i < len(filesNoDuplicates); i++ {
64+
output += filesNoDuplicates[i] + " "
65+
}
66+
fmt.Printf(output + "\n")
67+
}

0 commit comments

Comments
 (0)