adding stub for decompress

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2018-10-18 13:09:21 -04:00
parent cffd33a055
commit 9697b1ca42
Signed by: vbatts
GPG Key ID: 10937E57733F1362
1 changed files with 60 additions and 0 deletions

60
main.go
View File

@ -1,7 +1,13 @@
package main
import (
"compress/gzip"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"time"
"github.com/urfave/cli"
)
@ -78,5 +84,59 @@ func main() {
func defaultAction(c *cli.Context) error {
// TODO check whether stdin or its a pty terminal, if no args
if c.Bool("decompress") {
if c.NArg() == 0 {
grdr, err := gzip.NewReader(os.Stdin)
if err != nil {
return cli.NewExitError(err, 2)
}
defer grdr.Close()
if _, err = io.Copy(os.Stdout, grdr); err != nil {
return cli.NewExitError(err, 2)
}
return nil
}
for _, arg := range c.Args() {
err := func() error {
fh, err := os.OpenFile(arg, os.O_RDONLY, 0600)
if err != nil {
return err
}
defer fh.Close()
fi, err := fh.Stat()
if err != nil {
return err
}
newName := gzBasename(c, arg)
newFh, err := os.OpenFile(newName, os.O_CREATE|os.O_WRONLY, fi.Mode())
if err != nil {
return err
}
defer newFh.Close()
if _, err := io.Copy(newFh, fh); err != nil {
return err
}
return nil
}()
if err != nil {
return cli.NewExitError(err, 2)
}
}
return nil
}
return nil
}
func gzBasename(c *cli.Context, s string) string {
if filepath.Ext(s) == c.String("suffix") {
return filepath.Base(s)
}
return fmt.Sprint("%s.%d", s, rand.New(rand.NewSource(time.Now().UnixNano())).Intn(100))
}