78 lines
1.4 KiB
Go
78 lines
1.4 KiB
Go
package splitter
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"git.gutmet.org/goutil.git"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
func writeMetadata(filename string, metadata string) {
|
|
metapath := goutil.TrimExt(filename) + ".desc"
|
|
goutil.WriteFile(metapath, metadata)
|
|
}
|
|
|
|
func readData(scanner *bufio.Scanner) string {
|
|
var data bytes.Buffer
|
|
for scanner.Scan() {
|
|
data.WriteString(scanner.Text())
|
|
data.WriteString("\n")
|
|
}
|
|
return data.String()
|
|
}
|
|
|
|
func readMetadata(scanner *bufio.Scanner) string {
|
|
var metadata bytes.Buffer
|
|
firstLine := true
|
|
for scanner.Scan() {
|
|
text := scanner.Text()
|
|
if firstLine {
|
|
if !strings.HasPrefix(text, "---") {
|
|
break
|
|
}
|
|
firstLine = false
|
|
continue
|
|
}
|
|
if strings.HasPrefix(text, "---") {
|
|
break
|
|
} else {
|
|
metadata.WriteString(text)
|
|
metadata.WriteString("\n")
|
|
}
|
|
}
|
|
return metadata.String()
|
|
}
|
|
|
|
func split(filename string) {
|
|
var metadata string
|
|
var data string
|
|
f, err := os.Open(filename)
|
|
if err == nil {
|
|
defer f.Close()
|
|
scanner := bufio.NewScanner(f)
|
|
metadata = readMetadata(scanner)
|
|
data = readData(scanner)
|
|
} else {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
}
|
|
if len(strings.TrimSpace(metadata)) > 0 {
|
|
writeMetadata(filename, metadata)
|
|
goutil.WriteFile(filename, data)
|
|
}
|
|
}
|
|
|
|
func SplitAll(files []string) {
|
|
var wg sync.WaitGroup
|
|
wg.Add(len(files))
|
|
for _, f := range files {
|
|
go func(file string) {
|
|
defer wg.Done()
|
|
split(file)
|
|
}(f)
|
|
}
|
|
wg.Wait()
|
|
}
|