-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (128 loc) · 3.19 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
)
const TodoFileName = "todoList.txt"
type Todo struct {
Filepath string
Files []string
}
/**
clear todoListFile
*/
func ClearTodoList(outputPath string) {
file, err := os.OpenFile(outputPath+TodoFileName, os.O_TRUNC|os.O_CREATE, 0666)
defer file.Close()
if err != nil {
log.Fatal("init file error: ", err)
}
}
/**
get fileNameList
*/
func (todo *Todo) getFiles() *Todo {
// Execute the ls command in the target directory.
out, err := exec.Command("find", "-f", todo.Filepath).Output()
if err != nil {
fmt.Println(err.Error())
fmt.Println(err)
log.Fatal(err.Error())
}
// Transfer the acquired file list from byte slice to string slice
var fileList []string
for _, v := range regexp.MustCompile("\r\n|\n\r|\n|\r").Split(string(out), -1) {
fileList = append(fileList, v)
}
return &Todo{Filepath: todo.Filepath, Files: fileList}
}
/**
Read the file line by line and find the "to do string"
*/
func BufioScanner(fileName string) (todoList []string) {
f, err := os.Open(fileName)
defer f.Close()
if err != nil {
log.Fatal("bufio error: ", err)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if strings.Contains(strings.ToUpper(strings.Replace(scanner.Text(), " ", "", -1)), "//TODO") {
todoList = append(todoList, scanner.Text())
}
}
return todoList
}
/**
write file
*/
func WriteTodoList(todoListFile, currentFileName string, todoMessages []string) {
file, err := os.OpenFile(todoListFile, os.O_WRONLY|os.O_APPEND, 0666)
defer file.Close()
if err != nil {
log.Fatal("write file error: ", err)
}
for _, todo := range todoMessages {
fmt.Fprintln(file, "["+currentFileName+"]\n"+todo)
}
}
/**
user input validation
*/
func ValidationOfUserInputInfo(path, outputDirFlag string) string {
// check dir
fInfo, _ := os.Stat(path)
if !fInfo.IsDir() {
log.Fatal("not directory: ", path)
}
// check suffix
newPath := path
// output file
if outputDirFlag == "1" {
if path[len(path)-1:] != "/" {
newPath += "/"
}
// input file
} else {
if path[len(path)-1:] == "/" {
newPath = path[:len(path)-1]
}
}
return newPath
}
func main() {
var inputPath string
var outputPath string
fmt.Println("####TODOリストをファイルに書き出します####")
// user output file path
fmt.Println("####書き出し先を入力してください####")
fmt.Scan(&outputPath)
// user input file path
fmt.Println("####読み込み先ディレクトリを入力してください####")
fmt.Scan(&inputPath)
// validation
outputPath = ValidationOfUserInputInfo(outputPath, "1")
inputPath = ValidationOfUserInputInfo(inputPath, "0")
v := &Todo{Filepath: inputPath}
fmt.Println("####", inputPath, "以下のTODOを書き出します####")
//clear or create to_do_list file
ClearTodoList(outputPath)
// get fileNameList
todo := v.getFiles()
// find to do string and write file
for _, file := range todo.Files {
fInfo, _ := os.Stat(file)
// For directories,continue
if fInfo == nil || fInfo.IsDir() {
continue
}
todoMessages := BufioScanner(file)
WriteTodoList(outputPath+TodoFileName, file, todoMessages)
}
fmt.Println(outputPath+TodoFileName, " へ書き出しました")
}