forked from laher/someutils
/
gunzip.go
184 lines (168 loc) · 3.91 KB
/
gunzip.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package some
import (
"compress/gzip"
"errors"
"fmt"
"github.com/nuvolaris/someutils"
"github.com/laher/uggo"
"io"
"os"
)
func init() {
someutils.RegisterPipable(func() someutils.CliPipable { return new(SomeGunzip) })
}
// SomeGunzip represents and performs `gunzip` invocations
type SomeGunzip struct {
IsTest bool
IsKeep bool
IsPipeOut bool
Filenames []string
}
// Name() returns the name of the util
func (gunzip *SomeGunzip) Name() string {
return "gunzip"
}
// ParseFlags parses flags from a commandline []string
func (gunzip *SomeGunzip) ParseFlags(call []string, errPipe io.Writer) (error, int) {
flagSet := uggo.NewFlagSetDefault("gunzip", "[options] file.gz [list...]", someutils.VERSION)
flagSet.SetOutput(errPipe)
flagSet.AliasedBoolVar(&gunzip.IsTest, []string{"t", "test"}, false, "test archive data")
flagSet.AliasedBoolVar(&gunzip.IsKeep, []string{"k", "keep"}, false, "keep gzip file")
flagSet.AliasedBoolVar(&gunzip.IsPipeOut, []string{"c", "stdout", "is-stdout"}, false, "output will go to the standard output")
err, code := flagSet.ParsePlus(call[1:])
if err != nil {
return err, code
}
args := flagSet.Args()
//TODO STDIN support
if len(args) > 0 {
//OK
} else if uggo.IsPipingStdin() {
//OK
} else {
return errors.New("No gzip filename given"), 1
}
gunzip.Filenames = args
return nil, 0
}
// Exec actually performs the gunzip
func (gunzip *SomeGunzip) Invoke(invocation *someutils.Invocation) (error, int) {
invocation.ErrPipe.Drain()
invocation.AutoHandleSignals()
if gunzip.IsTest {
err := TestGzipItems(gunzip.Filenames)
if err != nil {
return err, 1
}
} else {
err := gunzip.gunzipItems(invocation.MainPipe.In, invocation.MainPipe.Out, invocation.ErrPipe.Out)
if err != nil {
return err, 1
}
}
return nil, 0
}
func TestGzipItems(items []string) error {
for _, item := range items {
fh, err := os.Open(item)
if err != nil {
return err
}
err = TestGzipItem(fh)
if err != nil {
return err
}
}
return nil
}
//TODO: proper file checking (how to check validity?)
func TestGzipItem(item io.Reader) error {
r, err := gzip.NewReader(item)
if err != nil {
return err
}
defer r.Close()
return nil
}
func (gunzip *SomeGunzip) gunzipItems(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer) error {
if len(gunzip.Filenames) == 0 {
//in to out
err := gunzip.gunzipItem(inPipe, outPipe, errPipe, true)
if err != nil {
return err
}
} else {
for _, item := range gunzip.Filenames {
fh, err := os.Open(item)
if err != nil {
return err
}
err = gunzip.gunzipItem(fh, outPipe, errPipe, gunzip.IsPipeOut)
if err != nil {
return err
}
err = fh.Close()
if err != nil {
return err
}
if !gunzip.IsKeep {
err = os.Remove(item)
if err != nil {
return err
}
}
}
}
return nil
}
func (gunzip *SomeGunzip) gunzipItem(item io.Reader, outPipe io.Writer, errPipe io.Writer, toOut bool) error {
r, err := gzip.NewReader(item)
if err != nil {
return err
}
defer r.Close()
if toOut {
_, err = io.Copy(outPipe, r)
if err != nil {
return err
}
} else {
destFileName := r.Header.Name
fmt.Fprintln(errPipe, "Filename", destFileName)
destFile, err := os.Create(destFileName)
defer destFile.Close()
if err != nil {
return err
}
_, err = io.Copy(destFile, r)
if err != nil {
return err
}
err = destFile.Close()
if err != nil {
return err
}
}
err = r.Close()
return err
}
func GunzipToOut(args ...string) someutils.CliPipable {
gunzip := new(SomeGunzip)
gunzip.Filenames = args
gunzip.IsPipeOut = true
return (gunzip)
}
// Factory for *SomeGunzip
func Gunzip(args ...string) someutils.CliPipable {
gunzip := new(SomeGunzip)
gunzip.Filenames = args
if len(args) == 0 {
gunzip.IsPipeOut = true
}
return (gunzip)
}
// CLI invocation for *SomeGunzip
func GunzipCli(call []string) (error, int) {
util := new(SomeGunzip)
return someutils.StdInvoke((util), call)
}