-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnappy.go
153 lines (139 loc) · 3.17 KB
/
snappy.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"errors"
"github.com/cosiner/flag"
"github.com/golang/snappy"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
type Client struct {
Create bool `names:"-c" usage:"Compression file to snappy format"`
Extract bool `names:"-x" usage:"Decompression snappy format file"`
Verbose bool `names:"-v, --verbose" usage:"Verbose mode" default:"false"`
SourceFiles []string `names:"-f, --file" args:"true" usage:"Files to compression/decompression"`
OutputDir string `names:"-o, --output" usage:"Output directory"`
}
func (c *Client) Metadata() map[string]flag.Flag {
const (
usage = `
compression: snappy -c example.txt
decompression: snappy -x example.snappy
`
version = `
version: v1.0.0
commit: xxx
date: 2022-01-01 10:00:01
`
desc = `
snappy-client is a compression/decompression client for Google Snappy write by golang.
`
)
return map[string]flag.Flag{
"": {
Usage: usage,
Version: version,
Desc: desc,
},
}
}
func (c *Client) ParameterCheck() (bool, error) {
var errMsg []string
if c.Create && c.Extract {
errMsg = append(errMsg, "compression and decompression cannot be specified at the same time")
}
if !(c.Create || c.Extract) {
errMsg = append(errMsg, "compression or decompression must be specified one")
}
if len(c.SourceFiles) == 0 {
errMsg = append(errMsg, "missing files to snappy or unsnappy")
}
if len(errMsg) > 0 {
return false, errors.New(strings.Join(errMsg[:], "\n"))
} else {
return true, nil
}
}
func (c *Client) run() {
_, err := c.ParameterCheck()
if err != nil {
log.Fatal(err)
}
c.ParseOutputDir()
if c.Create {
c.Snappy()
} else if c.Extract {
c.Unsnappy()
} else {
log.Fatal("unsupported operation, exit")
}
}
func (c *Client) ParseOutputDir() {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if c.OutputDir == "" {
c.OutputDir = wd
}
_, err = os.Stat(c.OutputDir)
if os.IsNotExist(err) {
err = os.MkdirAll(c.OutputDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}
}
}
func (c *Client) GetOutputFilename(path string) string {
filename := filepath.Base(path)
ext := filepath.Ext(filename)
if c.Extract {
if ext == "" {
return strings.TrimSuffix(filename, ext) + ".unsnappy"
} else {
return strings.TrimSuffix(filename, ext)
}
} else if c.Create {
return filename + ".snappy"
} else {
return filename
}
}
func (c *Client) Snappy() {
for _, f := range c.SourceFiles {
content, err := ioutil.ReadFile(f)
if err != nil {
log.Error(err)
continue
}
encoded := snappy.Encode(nil, content)
fullFilename := path.Join(c.OutputDir, c.GetOutputFilename(f))
err = ioutil.WriteFile(fullFilename, encoded, 0644)
if err != nil {
log.Error(err)
continue
}
}
}
func (c *Client) Unsnappy() {
for _, f := range c.SourceFiles {
content, err := ioutil.ReadFile(f)
if err != nil {
log.Error(err)
continue
}
decoded, err := snappy.Decode(nil, content)
if err != nil {
log.Error(err)
continue
}
fullFilename := path.Join(c.OutputDir, c.GetOutputFilename(f))
err = ioutil.WriteFile(fullFilename, decoded, 0644)
if err != nil {
log.Error(err)
continue
}
}
}