forked from mmadfox/go-crx3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip.go
52 lines (45 loc) · 1.03 KB
/
unzip.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
package commands
import (
"errors"
"os"
"strings"
crx3 "github.com/joelvaneenwyk/go-web-extensions"
"github.com/spf13/cobra"
)
type unzipOpts struct {
Outfile string
}
func (o unzipOpts) HasNotOutfile() bool {
return len(o.Outfile) == 0
}
func newUnzipCmd() *cobra.Command {
var opts unzipOpts
cmd := &cobra.Command{
Use: "unzip [extension.zip]",
Short: "Extract all files from the archive",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("extension is required")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
infile := args[0]
zipFile, err := os.Open(infile)
if err != nil {
return err
}
defer zipFile.Close()
stat, err := zipFile.Stat()
if err != nil {
return err
}
if opts.HasNotOutfile() {
opts.Outfile = strings.TrimSuffix(infile, ".zip")
}
return crx3.Unzip(zipFile, stat.Size(), opts.Outfile)
},
}
cmd.Flags().StringVarP(&opts.Outfile, "outfile", "o", "", "save to file")
return cmd
}