Skip to content

Commit a963a7d

Browse files
committed
Use mod/zip package
1 parent ff8a822 commit a963a7d

File tree

6 files changed

+68
-111
lines changed

6 files changed

+68
-111
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/plexsystems/pacmod
22

33
go 1.13
44

5-
require github.com/spf13/cobra v0.0.5
5+
require (
6+
github.com/spf13/cobra v0.0.5
7+
golang.org/x/mod v0.2.0
8+
)

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,20 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
2626
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
2727
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
2828
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
29+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
30+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
31+
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
32+
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
33+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
34+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
35+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2936
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
37+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
38+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3039
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
40+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
41+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
42+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
43+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3144
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3245
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

pack/pack.go

Lines changed: 36 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,85 @@
11
package pack
22

33
import (
4-
"archive/zip"
5-
"bufio"
4+
"bytes"
65
"encoding/json"
76
"fmt"
8-
"io"
97
"io/ioutil"
108
"os"
119
"path/filepath"
12-
"strings"
1310
"time"
11+
12+
"golang.org/x/mod/modfile"
13+
"golang.org/x/mod/zip"
1414
)
1515

1616
// Module packs the module at the given path and version then
1717
// outputs the result to the specified output directory
1818
func Module(path string, version string, outputDirectory string) error {
19-
moduleName, err := getModuleName(path)
19+
moduleFile, err := getModuleFile(path, version)
2020
if err != nil {
21-
return fmt.Errorf("get module name: %w", err)
21+
return fmt.Errorf("get module file: %w", err)
2222
}
2323

24-
if err := createZipArchive(path, moduleName, version, outputDirectory); err != nil {
24+
if err := createZipArchive(path, moduleFile, outputDirectory); err != nil {
2525
return fmt.Errorf("create zip archive: %w", err)
2626
}
2727

28-
if err := createInfoFile(version, outputDirectory); err != nil {
28+
if err := createInfoFile(moduleFile, outputDirectory); err != nil {
2929
return fmt.Errorf("create info file: %w", err)
3030
}
3131

32-
if err := copyModuleFile(path, outputDirectory); err != nil {
32+
if err := copyModuleFile(path, moduleFile, outputDirectory); err != nil {
3333
return fmt.Errorf("copy module file: %w", err)
3434
}
3535

3636
return nil
3737
}
3838

39-
func getModuleName(path string) (string, error) {
40-
moduleFilePath := filepath.Join(path, "go.mod")
41-
file, err := os.Open(moduleFilePath)
39+
func getModuleFile(path string, version string) (*modfile.File, error) {
40+
path = filepath.Join(path, "go.mod")
41+
file, err := os.Open(path)
4242
if err != nil {
43-
return "", fmt.Errorf("unable to open module file: %w", err)
43+
return nil, fmt.Errorf("open module file: %w", err)
4444
}
4545
defer file.Close()
4646

47-
moduleFileScanner := bufio.NewScanner(file)
48-
moduleFileScanner.Scan()
49-
50-
moduleHeaderParts := strings.Split(moduleFileScanner.Text(), " ")
51-
if len(moduleHeaderParts) <= 1 {
52-
return "", fmt.Errorf("parse module header: %w", err)
53-
}
54-
55-
return moduleHeaderParts[1], nil
56-
}
57-
58-
func createZipArchive(path string, moduleName string, version string, outputDirectory string) error {
59-
filePathsToArchive, err := getFilePathsToArchive(path)
47+
moduleBytes, err := ioutil.ReadAll(file)
6048
if err != nil {
61-
return fmt.Errorf("get files to archive: %w", err)
49+
return nil, fmt.Errorf("read module file: %w", err)
6250
}
6351

64-
outputPath := filepath.Join(outputDirectory, version+".zip")
65-
zipFile, err := os.Create(outputPath)
52+
moduleFile, err := modfile.Parse(path, moduleBytes, nil)
6653
if err != nil {
67-
return fmt.Errorf("create zip file: %w", err)
54+
return nil, fmt.Errorf("parse module file: %w", err)
6855
}
69-
defer zipFile.Close()
70-
71-
zipWriter := zip.NewWriter(zipFile)
72-
defer zipWriter.Close()
73-
74-
for _, filePath := range filePathsToArchive {
75-
fileToZip, err := os.Open(filePath)
76-
if err != nil {
77-
return fmt.Errorf("open file: %w", err)
78-
}
79-
80-
zippedFilePath := getZipPath(path, filePath, moduleName, version)
81-
zippedFileWriter, err := zipWriter.Create(zippedFilePath)
82-
if err != nil {
83-
return fmt.Errorf("add file to zip archive: %w", err)
84-
}
85-
86-
if _, err := io.Copy(zippedFileWriter, fileToZip); err != nil {
87-
return fmt.Errorf("copy file contents to zip archive: %w", err)
88-
}
8956

90-
fileToZip.Close()
57+
if moduleFile.Module == nil {
58+
return nil, fmt.Errorf("parsing module returned nil module")
9159
}
9260

93-
return nil
94-
}
95-
96-
func getFilePathsToArchive(path string) ([]string, error) {
97-
var files []string
98-
err := filepath.Walk(path, func(currentFilePath string, fileInfo os.FileInfo, err error) error {
99-
if err != nil {
100-
return fmt.Errorf("walk path: %w", err)
101-
}
61+
moduleFile.Module.Mod.Version = version
10262

103-
if fileInfo.IsDir() && fileInfo.Name() == ".git" {
104-
return filepath.SkipDir
105-
}
106-
107-
if fileInfo.IsDir() {
108-
return nil
109-
}
63+
return moduleFile, nil
64+
}
11065

111-
files = append(files, currentFilePath)
66+
func createZipArchive(path string, moduleFile *modfile.File, outputDirectory string) error {
67+
outputPath := filepath.Join(outputDirectory, moduleFile.Module.Mod.Version+".zip")
11268

113-
return nil
114-
})
115-
if err != nil {
116-
return nil, err
69+
var zipContents bytes.Buffer
70+
if err := zip.CreateFromDir(&zipContents, moduleFile.Module.Mod, path); err != nil {
71+
return fmt.Errorf("create zip from dir: %w", err)
11772
}
11873

119-
return files, nil
120-
}
74+
if err := ioutil.WriteFile(outputPath, zipContents.Bytes(), 0644); err != nil {
75+
return fmt.Errorf("writing zip file: %w", err)
76+
}
12177

122-
func getZipPath(path string, currentFilePath string, moduleName string, version string) string {
123-
filePath := strings.TrimPrefix(currentFilePath, path)
124-
return filepath.Join(fmt.Sprintf("%s@%s", moduleName, version), filePath)
78+
return nil
12579
}
12680

127-
func createInfoFile(version string, outputDirectory string) error {
128-
infoFilePath := filepath.Join(outputDirectory, version+".info")
81+
func createInfoFile(moduleFile *modfile.File, outputDirectory string) error {
82+
infoFilePath := filepath.Join(outputDirectory, moduleFile.Module.Mod.Version+".info")
12983
file, err := os.Create(infoFilePath)
13084
if err != nil {
13185
return fmt.Errorf("create info file: %w", err)
@@ -139,7 +93,7 @@ func createInfoFile(version string, outputDirectory string) error {
13993

14094
currentTime := getInfoFileFormattedTime(time.Now())
14195
info := infoFile{
142-
Version: version,
96+
Version: moduleFile.Module.Mod.Version,
14397
Time: currentTime,
14498
}
14599

@@ -160,13 +114,13 @@ func getInfoFileFormattedTime(currentTime time.Time) string {
160114
return currentTime.Format(infoFileTimeFormat)
161115
}
162116

163-
func copyModuleFile(path string, outputDirectory string) error {
117+
func copyModuleFile(path string, moduleFile *modfile.File, outputDirectory string) error {
164118
if outputDirectory == "." {
165119
return nil
166120
}
167121

168122
sourcePath := filepath.Join(path, "go.mod")
169-
destinationPath := filepath.Join(outputDirectory, "go.mod")
123+
destinationPath := filepath.Join(outputDirectory, moduleFile.Module.Mod.Version+".mod")
170124

171125
if sourcePath == destinationPath {
172126
return nil

pack/pack_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,3 @@ func Test_GetInfoFile_ReturnsCorrectTimeFormat(t *testing.T) {
1414
t.Errorf("invalid infofile time format: expected %v actual %v", expected, actual)
1515
}
1616
}
17-
18-
func Test_GetZipPath_PathAndModulePathsAreSame(t *testing.T) {
19-
modulePath := "/root/"
20-
currentFilePath := "/root/app.go"
21-
name := "root"
22-
version := "v1.0.0"
23-
24-
actual := getZipPath(modulePath, currentFilePath, name, version)
25-
expected := "[email protected]/app.go"
26-
27-
if expected != actual {
28-
t.Errorf("invalid zip path: expected %v actual %v", expected, actual)
29-
}
30-
}
31-
32-
func Test_GetZipPath_ModulePathIsChildOfPath(t *testing.T) {
33-
modulePath := "/root/repository/username/app"
34-
currentFilePath := "/root/repository/username/app/app.go"
35-
name := "repository/username/app"
36-
version := "v1.0.0"
37-
38-
actual := getZipPath(modulePath, currentFilePath, name, version)
39-
expected := "repository/username/[email protected]/app.go"
40-
41-
if expected != actual {
42-
t.Errorf("invalid zip path: expected %v actual %v", expected, actual)
43-
}
44-
}

test/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
module github.com/foo/bar
3+
4+
go 1.13
5+
6+
require (
7+
golang.org/x/mod v0.2.0
8+
)

test/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package test
2+
3+
import "fmt"
4+
5+
func foo() {
6+
fmt.Println("bar")
7+
}

0 commit comments

Comments
 (0)