Skip to content

Commit b5127c3

Browse files
committed
Check if go.mod requires a copy operation
1 parent 6e7b7f7 commit b5127c3

File tree

2 files changed

+67
-60
lines changed

2 files changed

+67
-60
lines changed

internal/commands/pack.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
"path/filepath"
78

89
"github.com/plexsystems/pacmod/pack"
910
"github.com/spf13/cobra"
@@ -14,7 +15,7 @@ import (
1415
func NewPackCommand() *cobra.Command {
1516
cmd := cobra.Command{
1617
Use: "pack <version> <outputdirectory>",
17-
Short: "Package your Go module",
18+
Short: "Package your Go modules",
1819
Args: cobra.MinimumNArgs(2),
1920

2021
RunE: func(cmd *cobra.Command, args []string) error {
@@ -32,9 +33,18 @@ func runPackCommand(args []string) error {
3233
}
3334

3435
version := args[0]
35-
outputDirectory := args[1]
3636

37-
log.Printf("Packing module in path %s...", path)
37+
path, err = filepath.Abs(path)
38+
if err != nil {
39+
return fmt.Errorf("could not get abs path of module path: %w", err)
40+
}
41+
42+
outputDirectory, err := filepath.Abs(args[1])
43+
if err != nil {
44+
return fmt.Errorf("could not get abs path of output directory: %w", err)
45+
}
46+
47+
log.Printf("Packing module in path %s...", outputDirectory)
3848
if err := pack.Module(path, version, outputDirectory); err != nil {
3949
return fmt.Errorf("could not package module: %w", err)
4050
}

pack/pack.go

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"io/ioutil"
910
"os"
1011
"path/filepath"
1112
"strings"
@@ -37,28 +38,61 @@ func Module(path string, version string, outputDirectory string) error {
3738

3839
func getModuleName(path string) (string, error) {
3940
moduleFilePath := filepath.Join(path, "go.mod")
40-
4141
file, err := os.Open(moduleFilePath)
4242
if err != nil {
4343
return "", fmt.Errorf("unable to open module file: %w", err)
4444
}
4545
defer file.Close()
4646

47-
moduleFileReader := bufio.NewReader(file)
48-
moduleHeader, err := moduleFileReader.ReadString('\n')
49-
if err != nil {
50-
return "", fmt.Errorf("unable to read module header: %w", err)
51-
}
47+
moduleFileScanner := bufio.NewScanner(file)
48+
moduleFileScanner.Scan()
5249

53-
moduleHeaderParts := strings.Split(moduleHeader, " ")
50+
moduleHeaderParts := strings.Split(moduleFileScanner.Text(), " ")
5451
if len(moduleHeaderParts) <= 1 {
5552
return "", fmt.Errorf("unable to parse module header: %w", err)
5653
}
5754

5855
return moduleHeaderParts[1], nil
5956
}
6057

61-
func getFilesToArchive(path string) ([]string, error) {
58+
func createZipArchive(path string, moduleName string, version string, outputDirectory string) error {
59+
filePathsToArchive, err := getFilePathsToArchive(path)
60+
if err != nil {
61+
return fmt.Errorf("unable to get files to archive: %w", err)
62+
}
63+
64+
outputPath := filepath.Join(outputDirectory, version+".zip")
65+
zipFile, err := os.Create(outputPath)
66+
if err != nil {
67+
return fmt.Errorf("unable to create zip file: %w", err)
68+
}
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("unable to open file: %w", err)
78+
}
79+
defer fileToZip.Close()
80+
81+
zippedFilePath := getZipPath(path, filePath, moduleName, version)
82+
zippedFileWriter, err := zipWriter.Create(zippedFilePath)
83+
if err != nil {
84+
return fmt.Errorf("unable to add file to zip archive: %w", err)
85+
}
86+
87+
if _, err := io.Copy(zippedFileWriter, fileToZip); err != nil {
88+
return fmt.Errorf("unable to copy file contents to zip archive: %w", err)
89+
}
90+
}
91+
92+
return nil
93+
}
94+
95+
func getFilePathsToArchive(path string) ([]string, error) {
6296
var files []string
6397
err := filepath.Walk(path, func(currentFilePath string, fileInfo os.FileInfo, err error) error {
6498
if err != nil {
@@ -88,44 +122,6 @@ func getFilesToArchive(path string) ([]string, error) {
88122
return files, nil
89123
}
90124

91-
func createZipArchive(path string, moduleName string, version string, outputDirectory string) error {
92-
outputPath := filepath.Join(outputDirectory, version+".zip")
93-
94-
zipFile, err := os.Create(outputPath)
95-
if err != nil {
96-
return fmt.Errorf("unable to create zip file: %w", err)
97-
}
98-
defer zipFile.Close()
99-
100-
zipWriter := zip.NewWriter(zipFile)
101-
defer zipWriter.Close()
102-
103-
filesToArchive, err := getFilesToArchive(path)
104-
if err != nil {
105-
return fmt.Errorf("unable to get files to archive: %w", err)
106-
}
107-
108-
for _, file := range filesToArchive {
109-
zippedFilePath := getZipPath(path, file, moduleName, version)
110-
zippedFileWriter, err := zipWriter.Create(zippedFilePath)
111-
if err != nil {
112-
return fmt.Errorf("unable to add file to zip archive: %w", err)
113-
}
114-
115-
fileToZip, err := os.Open(file)
116-
if err != nil {
117-
return fmt.Errorf("unable to open file: %w", err)
118-
}
119-
defer fileToZip.Close()
120-
121-
if _, err := io.Copy(zippedFileWriter, fileToZip); err != nil {
122-
return fmt.Errorf("unable to copy file contents to zip archive: %w", err)
123-
}
124-
}
125-
126-
return nil
127-
}
128-
129125
func getZipPath(path string, currentFilePath string, moduleName string, version string) string {
130126
filePath := strings.TrimPrefix(currentFilePath, path)
131127
return filepath.Join(fmt.Sprintf("%s@%s", moduleName, version), filePath)
@@ -167,24 +163,25 @@ func getInfoFileFormattedTime(currentTime time.Time) string {
167163
return currentTime.Format(infoFileTimeFormat)
168164
}
169165

170-
func copyModuleFile(modulePath string, outputDirectory string) error {
171-
sourcePath := filepath.Join(modulePath, "go.mod")
166+
func copyModuleFile(path string, outputDirectory string) error {
167+
if outputDirectory == "." {
168+
return nil
169+
}
170+
171+
sourcePath := filepath.Join(path, "go.mod")
172172
destinationPath := filepath.Join(outputDirectory, "go.mod")
173173

174-
sourceModule, err := os.Open(sourcePath)
175-
if err != nil {
176-
return fmt.Errorf("could not open source module file: %w", err)
174+
if sourcePath == destinationPath {
175+
return nil
177176
}
178-
defer sourceModule.Close()
179177

180-
destinationModule, err := os.Create(destinationPath)
178+
moduleContents, err := ioutil.ReadFile(sourcePath)
181179
if err != nil {
182-
return fmt.Errorf("could not create mod file: %w", err)
180+
return fmt.Errorf("unable to read module file: %w", err)
183181
}
184-
defer destinationModule.Close()
185182

186-
if _, err := io.Copy(destinationModule, sourceModule); err != nil {
187-
return fmt.Errorf("could not copy module contents: %w", err)
183+
if err := ioutil.WriteFile(destinationPath, moduleContents, 0644); err != nil {
184+
return fmt.Errorf("unable to write module file: %w", err)
188185
}
189186

190187
return nil

0 commit comments

Comments
 (0)