-
Notifications
You must be signed in to change notification settings - Fork 8
/
zip.go
48 lines (40 loc) · 912 Bytes
/
zip.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
package commands
import (
"errors"
"os"
crx3 "github.com/mediabuyerbot/go-crx3"
"github.com/spf13/cobra"
)
type zipOpts struct {
Outfile string
}
func (o zipOpts) HasOutfile() bool {
return len(o.Outfile) > 0
}
func newZipCmd() *cobra.Command {
var opts zipOpts
cmd := &cobra.Command{
Use: "zip [filepath]",
Short: "Add unpacked extension to archive",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("filepath is required")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) (err error) {
infile := args[0]
if !opts.HasOutfile() {
opts.Outfile = infile + ".zip"
}
zipFile, err := os.Create(opts.Outfile)
if err != nil {
return err
}
defer zipFile.Close()
return crx3.Zip(zipFile, infile)
},
}
cmd.Flags().StringVarP(&opts.Outfile, "outfile", "o", "", "save to file")
return cmd
}