File: main.go

package info (click to toggle)
golang-github-golang-snappy 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 384 kB
  • sloc: asm: 1,180; cpp: 59; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 682 bytes parent folder | download | duplicates (5)
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
package main

import (
	"errors"
	"flag"
	"io/ioutil"
	"os"

	"github.com/golang/snappy"
)

var (
	decode = flag.Bool("d", false, "decode")
	encode = flag.Bool("e", false, "encode")
)

func run() error {
	flag.Parse()
	if *decode == *encode {
		return errors.New("exactly one of -d or -e must be given")
	}

	in, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return err
	}

	out := []byte(nil)
	if *decode {
		out, err = snappy.Decode(nil, in)
		if err != nil {
			return err
		}
	} else {
		out = snappy.Encode(nil, in)
	}
	_, err = os.Stdout.Write(out)
	return err
}

func main() {
	if err := run(); err != nil {
		os.Stderr.WriteString(err.Error() + "\n")
		os.Exit(1)
	}
}