All checks were successful
continuous-integration/drone/push Build is passing
58 lines
1 KiB
Go
58 lines
1 KiB
Go
package main
|
|
|
|
import (
|
|
"archive/zip"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
type archiveFileCreator interface {
|
|
Create(name string) (io.Writer, error)
|
|
Close() error
|
|
}
|
|
|
|
type filesCloser []io.Closer
|
|
|
|
func (fds filesCloser) Close() error {
|
|
log.Println("Closing fd..")
|
|
for _, fd := range fds {
|
|
err := fd.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
OutputFormats["archive"] = func(args ...string) (func(string) (io.WriteCloser, error), io.Closer, error) {
|
|
if len(args) != 1 {
|
|
return nil, nil, errors.New("archive has 1 required argument: [destination-file]")
|
|
}
|
|
|
|
fd, err := os.Create(args[0])
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var w archiveFileCreator
|
|
if path.Ext(args[0]) == ".zip" {
|
|
w = zip.NewWriter(fd)
|
|
} else {
|
|
return nil, nil, errors.New("destination file has to have .zip extension")
|
|
}
|
|
|
|
return func(dest string) (io.WriteCloser, error) {
|
|
fw, err := w.Create(dest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return NopCloser(fw), nil
|
|
}, filesCloser{w, fd}, nil
|
|
}
|
|
}
|