Skip to content

Commit 674cb84

Browse files
authored
Refactoring (#1)
1 parent 3bd15f4 commit 674cb84

File tree

11 files changed

+194
-202
lines changed

11 files changed

+194
-202
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

go.mod

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
module github.com/plexsystems/pacmod
22

3-
go 1.12
3+
go 1.13
44

5-
require (
6-
github.com/hugocarreira/go-decent-copy v0.0.0-20181018112419-9f482c9a2943
7-
github.com/spf13/cobra v0.0.5
8-
)
5+
require github.com/spf13/cobra v0.0.5

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
88
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
99
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
10-
github.com/hugocarreira/go-decent-copy v0.0.0-20181018112419-9f482c9a2943 h1:uuMUrGtG7EelzCDzq7VNfexyMnbWJzHK+lA2yEHfTk8=
11-
github.com/hugocarreira/go-decent-copy v0.0.0-20181018112419-9f482c9a2943/go.mod h1:LGbo2Li3Ub3ZNYZHIUFZ4qfwsFNvOI+If5lpEQNYJIU=
1210
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
1311
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
1412
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=

internal/commands/default.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package commands
22

3-
import (
4-
"github.com/spf13/cobra"
5-
)
3+
import "github.com/spf13/cobra"
64

7-
// NewDefaultCommand creates the default command
5+
// NewDefaultCommand creates a new default command
86
func NewDefaultCommand() *cobra.Command {
97

108
cmd := cobra.Command{
119
Use: "pacmod <subcommand>",
1210
Short: "Command line tool to assist with packaging Go modules",
1311
}
1412

15-
cmd.AddCommand(newPackCommand())
13+
cmd.AddCommand(NewPackCommand())
1614

1715
return &cmd
1816
}

internal/commands/pack.go

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package commands
22

33
import (
4-
"github.com/hugocarreira/go-decent-copy"
5-
"github.com/plexsystems/pacmod/pkg/pack"
6-
"github.com/spf13/cobra"
4+
"fmt"
75
"log"
86
"os"
97
"path/filepath"
8+
9+
"github.com/plexsystems/pacmod/pack"
10+
"github.com/spf13/cobra"
1011
)
1112

12-
func newPackCommand() *cobra.Command {
13+
// NewPackCommand creates a new pack command
14+
func NewPackCommand() *cobra.Command {
1315
cmd := cobra.Command{
14-
Use: "pack <module> <version>",
16+
Use: "pack <module> <version> <outputdirectory>",
1517
Short: "Package your Go module",
16-
Args: cobra.MinimumNArgs(2),
18+
Args: cobra.MinimumNArgs(3),
1719

1820
RunE: func(cmd *cobra.Command, args []string) error {
1921
return runPackCommand(args)
@@ -26,52 +28,23 @@ func newPackCommand() *cobra.Command {
2628
func runPackCommand(args []string) error {
2729
path, err := os.Getwd()
2830
if err != nil {
29-
return err
31+
return fmt.Errorf("could not get working directory: %w", err)
3032
}
3133

34+
path = filepath.ToSlash(path)
3235
name := args[0]
3336
version := args[1]
37+
outputDirectory := args[2]
3438

3539
module := pack.Module{
3640
Path: path,
3741
Name: name,
3842
Version: version,
3943
}
4044

41-
outputDirectory := filepath.Join(module.Path, module.Version)
42-
err = os.Mkdir(outputDirectory, 0777)
43-
if err != nil {
44-
return err
45-
}
46-
47-
log.Printf("Packing module %s into output directory %s", module.Name, outputDirectory)
48-
49-
log.Println("Creating archive...")
50-
err = module.ZipModule(outputDirectory)
51-
if err != nil {
52-
return err
53-
}
54-
55-
log.Println("Creating info...")
56-
info := pack.Info{
57-
Version: version,
58-
}
59-
err = info.CreateInfo(outputDirectory)
60-
if err != nil {
61-
return err
62-
}
63-
64-
log.Println("Copying mod...")
65-
return copyModuleFile(module.Path, outputDirectory)
66-
}
67-
68-
func copyModuleFile(source string, destination string) error {
69-
sourceModFile := filepath.Join(source, "go.mod")
70-
destinationModFile := filepath.Join(destination, "go.mod")
71-
72-
err := decentcopy.Copy(sourceModFile, destinationModFile)
73-
if err != nil {
74-
return err
45+
log.Printf("Packing module %s...", name)
46+
if err := module.PackageModule(outputDirectory); err != nil {
47+
return fmt.Errorf("could not package module: %w", err)
7548
}
7649

7750
return nil

cmd/pacmod/main.go renamed to main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package main
22

33
import (
4-
"github.com/plexsystems/pacmod/internal/commands"
54
"os"
5+
6+
"github.com/plexsystems/pacmod/internal/commands"
67
)
78

89
func main() {

pack/pack.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package pack
2+
3+
import (
4+
"archive/zip"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
"time"
12+
)
13+
14+
// Module represents a Go module
15+
type Module struct {
16+
Path string
17+
Name string
18+
Version string
19+
}
20+
21+
// PackageModule packs the module and outputs the result to the specified output path
22+
func (m Module) PackageModule(outputDirectory string) error {
23+
if err := m.createZipArchive(outputDirectory); err != nil {
24+
return fmt.Errorf("could not create zip archive: %w", err)
25+
}
26+
27+
if err := m.createInfoFile(outputDirectory); err != nil {
28+
return fmt.Errorf("could not create info file: %w", err)
29+
}
30+
31+
if err := m.copyModuleFile(outputDirectory); err != nil {
32+
return fmt.Errorf("could not copy module file: %w", err)
33+
}
34+
35+
return nil
36+
}
37+
38+
func (m Module) createZipArchive(outputDirectory string) error {
39+
outputPath := filepath.Join(outputDirectory, m.Version+".zip")
40+
41+
zipFile, err := os.Create(outputPath)
42+
if err != nil {
43+
return fmt.Errorf("unable to create empty zip file: %w", err)
44+
}
45+
defer zipFile.Close()
46+
47+
zipWriter := zip.NewWriter(zipFile)
48+
err = filepath.Walk(m.Path, func(currentFilePath string, fileInfo os.FileInfo, err error) error {
49+
if err != nil {
50+
return fmt.Errorf("unable to walk path: %w", err)
51+
}
52+
53+
if fileInfo.IsDir() && fileInfo.Name() == ".git" {
54+
return filepath.SkipDir
55+
}
56+
57+
if fileInfo.IsDir() || filepath.Ext(currentFilePath) == ".zip" {
58+
return nil
59+
}
60+
61+
file, err := os.Open(currentFilePath)
62+
if err != nil {
63+
return fmt.Errorf("unable to open file: %w", err)
64+
}
65+
defer file.Close()
66+
67+
zipPath := m.getZipPath(currentFilePath)
68+
zipFileWriter, err := zipWriter.Create(zipPath)
69+
if err != nil {
70+
return fmt.Errorf("unable to add file to zip archive: %w", err)
71+
}
72+
73+
if _, err := io.Copy(zipFileWriter, file); err != nil {
74+
return fmt.Errorf("unable to copy file to zip archive: %w", err)
75+
}
76+
77+
return nil
78+
})
79+
if err != nil {
80+
return fmt.Errorf("unable to zip all files: %w", err)
81+
}
82+
83+
return zipWriter.Close()
84+
}
85+
86+
func (m Module) getZipPath(currentFilePath string) string {
87+
fileName := strings.TrimPrefix(currentFilePath, m.Path)
88+
moduleName := fmt.Sprintf("%s@%s", m.Name, m.Version)
89+
90+
return filepath.Join(moduleName, fileName)
91+
}
92+
93+
func (m Module) createInfoFile(outputDirectory string) error {
94+
infoFilePath := filepath.Join(outputDirectory, m.Version+".info")
95+
file, err := os.Create(infoFilePath)
96+
if err != nil {
97+
return fmt.Errorf("could not create info file: %w", err)
98+
}
99+
defer file.Close()
100+
101+
infoBytes, err := json.Marshal(struct {
102+
Version string
103+
Time string
104+
}{
105+
Version: m.Version,
106+
Time: time.Now().Format("2006-01-02T15:04:05Z"),
107+
})
108+
if err != nil {
109+
return fmt.Errorf("could not marshal info file: %w", err)
110+
}
111+
112+
if _, err := file.Write(infoBytes); err != nil {
113+
return fmt.Errorf("could not write info file: %w", err)
114+
}
115+
116+
return nil
117+
}
118+
119+
func (m Module) copyModuleFile(outputDirectory string) error {
120+
sourcePath := filepath.Join(m.Path, "go.mod")
121+
destinationPath := filepath.Join(outputDirectory, "go.mod")
122+
123+
sourceModule, err := os.Open(sourcePath)
124+
if err != nil {
125+
return fmt.Errorf("could not open source module file: %w", err)
126+
}
127+
defer sourceModule.Close()
128+
129+
destinationModule, err := os.Create(destinationPath)
130+
if err != nil {
131+
return fmt.Errorf("could not create mod file: %w", err)
132+
}
133+
defer destinationModule.Close()
134+
135+
if _, err := io.Copy(destinationModule, sourceModule); err != nil {
136+
return fmt.Errorf("could not copy module contents: %w", err)
137+
}
138+
139+
return nil
140+
}

pack/pack_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package pack
2+
3+
import "testing"
4+
5+
func Test_GetZipPath_PathAndModulePathsAreSame(t *testing.T) {
6+
module := Module{
7+
Path: "/root/",
8+
Name: "root",
9+
Version: "v1.0.0",
10+
}
11+
12+
actual := module.getZipPath("/root/app.go")
13+
expected := "[email protected]/app.go"
14+
15+
if actual != expected {
16+
t.Errorf("expected %v, got %v", expected, actual)
17+
}
18+
}
19+
20+
func Test_GetZipPath_ModulePathChildOfPath(t *testing.T) {
21+
module := Module{
22+
Path: "/root/repository/username/app",
23+
Name: "repository/username/app",
24+
Version: "v1.0.0",
25+
}
26+
actual := module.getZipPath("/root/repository/username/app/app.go")
27+
expected := "repository/username/[email protected]/app.go"
28+
29+
if actual != expected {
30+
t.Errorf("expected %v, got %v", expected, actual)
31+
}
32+
}

pkg/pack/archive.go

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)