Files
athens/pkg/module/module.go
Komu Wairagu d74e0640fe remove dead code (#267)
* remove spf13/cobra and inconshreveable/mousetrap

* remove user package

* remove user package

* remove unused types, vars etc
found by using github.com/dominikh/go-tools/tree/master/cmd/unused.running  unused $(go list github.com/gomods/athens/... | grep -v /vendor/)

* rm uneeded file
2018-07-19 08:34:16 -07:00

97 lines
2.3 KiB
Go

package module
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
ignore "github.com/sabhiram/go-gitignore"
"github.com/spf13/afero"
)
const (
gitIgnoreFilename = ".gitignore"
)
// MakeZip takes dir and module info and generates vgo valid zip
// the dir must end with a "/"
func MakeZip(fs afero.Fs, dir, module, version string) *io.PipeReader {
ignoreParser := getIgnoreParser(fs, dir)
pr, pw := io.Pipe()
go func() {
zw := zip.NewWriter(pw)
defer zw.Close()
walkFn := walkFunc(fs, zw, dir, module, version, ignoreParser)
err := afero.Walk(fs, dir, walkFn)
pw.CloseWithError(err)
}()
return pr
}
func walkFunc(fs afero.Fs, zw *zip.Writer, dir, module, version string, ignoreParser ignore.IgnoreParser) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil || info == nil || info.IsDir() {
return err
}
fileName := getFileName(path, dir, module, version)
if ignoreParser.MatchesPath(fileName) {
return nil
}
fileContent, err := afero.ReadFile(fs, path)
if err != nil {
return err
}
f, err := zw.Create(fileName)
if err != nil {
return err
}
_, err = f.Write(fileContent)
return err
}
}
func getIgnoreParser(fs afero.Fs, dir string) ignore.IgnoreParser {
gitFilePath := filepath.Join(dir, gitIgnoreFilename)
gitParser := compileIgnoreFileAndLines(fs, gitFilePath, gitIgnoreFilename)
dsStoreParser := dsStoreIgnoreParser{}
return newMultiIgnoreParser(gitParser, dsStoreParser)
}
func compileIgnoreFileAndLines(fs afero.Fs, fpath string, lines ...string) ignore.IgnoreParser {
buffer, err := afero.ReadFile(fs, fpath)
if err != nil {
return nil
}
s := strings.Split(string(buffer), "\n")
ip, err := ignore.CompileIgnoreLines(append(s, lines...)...)
if err != nil {
// if we return ip, then it won't be a nil interface,
// even if ip is a nil pointer.
return nil
}
return ip
}
// getFileName composes filename for zip to match standard specified as
// module@version/{filename}
func getFileName(path, dir, module, version string) string {
filename := strings.TrimPrefix(path, dir)
filename = strings.TrimLeftFunc(filename, func(r rune) bool { return r == os.PathSeparator })
moduleID := fmt.Sprintf("%s@%s", module, version)
return filepath.Join(moduleID, filename)
}