update at 2020-07-20 18:16:20 by ehlxr

This commit is contained in:
ehlxr
2020-07-20 18:16:20 +08:00
parent 796e503959
commit 61ff5e9e8e
4 changed files with 57 additions and 23 deletions

View File

@@ -57,7 +57,21 @@ func main() {
if err != nil {
fmt.Printf("genDesc file: %s failed: %v\n", file, err)
}
fmt.Printf("file: %s desc: %s\n", file, desc)
bytes, err := genNew(file, string(desc))
if err != nil {
fmt.Printf("genNew file: %s failed: %v\n", file, err)
}
if len(bytes) > 0 {
err = writeFile(file, bytes)
if err != nil {
fmt.Printf("writeFile file: %s failed: %v\n", file, err)
continue
}
fmt.Printf("deal file: %s done!!!\n", file)
}
}
}
@@ -133,7 +147,7 @@ func genDesc(path string) ([]byte, error) {
break
}
if identifier < 2 && strings.Index(line, "---") > -1 {
if identifier < 2 && line == "---" {
identifier++
continue
}
@@ -142,6 +156,7 @@ func genDesc(path string) ([]byte, error) {
output = append(output, line...)
output = append(output, []byte("\n")...)
output = append(output, []byte("\n")...)
output = append(output, []byte("\n")...)
}
}
@@ -153,6 +168,44 @@ func genDesc(path string) ([]byte, error) {
return output, nil
}
func genNew(path string, des string) ([]byte, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
log.Printf("cannot open text file: %s, err: [%v]", path, err)
return nil, err
}
defer file.Close()
// 按行读取文件
scanner := bufio.NewScanner(file)
var identifier int
output := make([]byte, 0)
for scanner.Scan() {
line := scanner.Text()
if identifier < 2 && line == "---" {
identifier++
}
if identifier == 2 && line == "---" {
identifier++
output = append(output, fmt.Sprintf("description: \"%s\"", des)...)
output = append(output, []byte("\n")...)
}
output = append(output, line...)
output = append(output, []byte("\n")...)
}
if err := scanner.Err(); err != nil {
log.Printf("cannot scanner text file: %s, err: [%v]", path, err)
return nil, err
}
return output, nil
}
func writeFile(path string, b []byte) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
if err != nil {

View File

@@ -1,4 +0,0 @@
[?25l
✔ Input your source file path: █