osrm-backend/traffic_updater/go/snappy_command/snappy_command.go
Jay 927eb455ef
chore: format our golang codes by 'goreturns -l -w .' (#47)
be noted that we don't need to format generated codes, e.g. generated by thrift
2019-07-22 13:06:31 +08:00

66 lines
1.2 KiB
Go

package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/golang/snappy"
)
const snappySuffix string = ".snappy"
var flags struct {
input string
output string
suffix string
}
func init() {
flag.StringVar(&flags.input, "i", "", "Input file.")
flag.StringVar(&flags.output, "o", "", "Output file.")
}
func main() {
flag.Parse()
inputCompressed := strings.HasSuffix(flags.input, snappySuffix)
outputCompressed := strings.HasSuffix(flags.output, snappySuffix)
if inputCompressed == outputCompressed {
fmt.Printf("Error. Input and output must have one with .snappy suffix and one without.\n")
return
}
fi, err1 := os.Open(flags.input)
if err1 != nil {
fmt.Printf("Open input file failed.\n")
return
}
defer fi.Close()
fo, err2 := os.OpenFile(flags.output, os.O_RDWR|os.O_CREATE, 0755)
if err2 != nil {
fmt.Printf("Open output file failed.\n")
return
}
defer fo.Close()
defer fo.Sync()
buf := make([]byte, 128*1024)
if inputCompressed {
_, err := io.CopyBuffer(fo, snappy.NewReader(fi), buf)
if err != nil {
fmt.Printf("Decompression failed\n")
}
} else {
_, err := io.CopyBuffer(snappy.NewWriter(fo), fi, buf)
if err != nil {
fmt.Printf("Compression failed\n")
}
}
}