wombat/splitter/splitter.go

79 lines
1.4 KiB
Go
Raw Normal View History

2019-01-01 19:41:03 +01:00
package splitter
import (
"bufio"
"bytes"
"fmt"
2020-10-11 13:35:13 +02:00
goutil "git.gutmet.org/goutil.git/misc"
2019-01-01 19:41:03 +01:00
"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)
os.Remove(filename) // remove hardlink before writing
2019-01-01 19:41:03 +01:00
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()
}