-
Notifications
You must be signed in to change notification settings - Fork 0
/
snp.go
48 lines (43 loc) · 838 Bytes
/
snp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"encoding/base64"
"flag"
"io/ioutil"
"log"
"os"
"github.com/golang/snappy"
)
var (
isDecode bool
outBinary bool
)
func init() {
flag.BoolVar(&isDecode, "d", false, "Decode from stdin")
flag.BoolVar(&outBinary, "b", false, "Output compressed data as binary instead of a base64-encoded string")
flag.Parse()
}
func main() {
var (
out []byte
err error
)
r, _ := ioutil.ReadAll(os.Stdin)
if isDecode {
// First attempt to decode base64
out, err = base64.StdEncoding.DecodeString(string(r))
if err != nil {
// Assume it isn't base64 and just move on
out = r
}
out, err = snappy.Decode(nil, out)
if err != nil {
log.Fatal(err)
}
} else {
out = snappy.Encode(nil, r)
if !outBinary {
out = []byte(base64.StdEncoding.EncodeToString(out))
}
}
os.Stdout.Write(out)
}