-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.go
84 lines (69 loc) · 1.54 KB
/
io.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* IO
*/
package main
import (
"bufio"
"io/ioutil"
"log"
"os"
)
// Sio represents all IO functionality
type Sio struct {
Files map[string]*SrcFile
TmpFiles []string
Routes []Route
Models map[string]Model
}
// GetAllFilePaths recursively looks for golang files starting with a root directory at path `rootPath`
func (s *Sio) GetAllFilePaths(rootPath string) {
fileInfos, err := ioutil.ReadDir(rootPath)
if err != nil {
log.Fatal(err)
}
for _, fileInfo := range fileInfos {
fileName := fileInfo.Name()
switch mode := fileInfo.Mode(); {
case mode.IsRegular():
// Check for .go extension
if fileName[len(fileName)-3:] != ".go" {
continue
}
s.TmpFiles = append(s.TmpFiles, rootPath+"/"+fileName)
break
case mode.IsDir():
s.GetAllFilePaths(rootPath + "/" + fileName)
break
}
}
}
// @deprecated
func readDirFiles(dirPath string) (files []string) {
fileInfos, err := ioutil.ReadDir(dirPath)
if err != nil {
log.Fatal(err)
}
for _, fileInfo := range fileInfos {
switch mode := fileInfo.Mode(); {
// case mode.IsDir():
// fmt.Printf("DIR: %s\n", fileInfo.Name())
case mode.IsRegular():
files = append(files, dirPath+"/"+fileInfo.Name())
// fmt.Printf("File: %s\n", fileInfo.Name())
}
}
return
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}