go: implement splitting

This commit is contained in:
dave 2017-11-09 16:24:04 -08:00
parent f5c4a64ea1
commit 3bf8bc07ef
2 changed files with 36 additions and 1 deletions

View File

@ -34,6 +34,9 @@ var (
cmd_slice_raw = cmd_slice.Flag("all", "Export raw lines instead of archives").Bool()
cmd_split = kingpin.Command("split", "Split archives by date")
cmd_split_src = cmd_split.Flag("src", "Source archive file").Short('s').Required().ExistingFile()
cmd_split_dest = cmd_split.Flag("dest", "Dir to dump logs into").Short('d').Required().String()
)
type LogInfo struct {
@ -218,6 +221,18 @@ func cmd_slice_do(srcpath string, destpath string, starttime string, endtime str
}
}
// Split an archive back into original log files
func cmd_split_do(srcpath string, destdir string) {
log := &CombinedLogfile{
fpath: srcpath,
}
log.Parse()
logs_written, err := log.WriteOriginals(destdir)
check(err)
fmt.Printf("Wrote %v logs\n", logs_written)
}
func main() {
switch kingpin.Parse() {
case "import":
@ -227,6 +242,6 @@ func main() {
case "slice":
cmd_slice_do(*cmd_slice_src, *cmd_slice_dest, *cmd_slice_start, *cmd_slice_end, *cmd_slice_raw)
case "split":
// TODO
cmd_split_do(*cmd_split_src, *cmd_split_dest)
}
}

View File

@ -6,6 +6,7 @@ import (
"fmt"
"bufio"
"bytes"
"path/filepath"
"encoding/json"
"time"
"sort"
@ -104,6 +105,25 @@ func (self *CombinedLogfile) Write(destpath string) (error) {
return nil
}
func (self *CombinedLogfile) WriteOriginals(destdir string) (int, error) {
written := 0
for _, portion := range self.portions {
f, err := os.OpenFile(filepath.Join(destdir, portion.meta.Name), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
check(err)
w := bufio.NewWriter(f)
for _, line := range portion.lines {
for _, b := range line {
w.WriteByte(b)
}
w.WriteString("\n")
}
check(w.Flush())
f.Close()
written += 1
}
return written, nil
}
func (self *CombinedLogfile) ConvertMetaToJson(meta PortionMeta) string {
jmeta := JsonPortionMeta{
Channel: meta.Channel,