Skip to content

Commit 0729642

Browse files
committed
Complete folder publisher
1 parent 8d9f64a commit 0729642

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717
.idea/
1818
.vscode/
19+
.DS_Store

publish.go

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package main
33
import (
44
"errors"
55
"io"
6+
"io/ioutil"
67
"log"
78
"os"
89
"path/filepath"
910
"strings"
11+
"sync"
1012

1113
"github.com/go-sharp/color"
1214
)
@@ -50,17 +52,26 @@ func (f FolderPublishCmd) Execute(args []string) error {
5052

5153
log.Println("processing files")
5254
dirPrefix := filepath.Join(workDir, "cache", "download")
55+
var wg sync.WaitGroup
5356
err = filepath.Walk(dirPrefix, func(path string, info os.FileInfo, err error) error {
5457
relPath := strings.TrimLeft(strings.TrimPrefix(path, dirPrefix), string(filepath.Separator))
5558

5659
if strings.HasPrefix(relPath, "sumdb") && !info.IsDir() {
57-
go f.handleCopyFile(path, relPath)
60+
wg.Add(1)
61+
go func() {
62+
f.handleCopyFile(path, relPath)
63+
wg.Done()
64+
}()
5865
return nil
5966
}
6067

6168
if info.IsDir() && strings.HasSuffix(relPath, "@v") {
62-
go f.handleModule(path, relPath)
63-
return nil
69+
wg.Add(1)
70+
go func() {
71+
f.handleModule(path, dirPrefix)
72+
wg.Done()
73+
}()
74+
return filepath.SkipDir
6475
}
6576

6677
return nil
@@ -69,13 +80,66 @@ func (f FolderPublishCmd) Execute(args []string) error {
6980
if err != nil {
7081
log.Println(errorRedPrefix, err)
7182
}
83+
wg.Wait()
7284

85+
ppath, _ := filepath.Abs(f.Output)
86+
log.Println("published archive to:", color.GreenString(ppath))
87+
log.Printf("hint: set GOPROXY to use folder for dependencies:\n\t%v\n", color.BlueString("go env -w GOPROXY=file:///%v", ppath))
88+
log.Printf("hint: in an air-gapped env set GOSUMDB to of:\n\t%v\n", color.BlueString("go env -w GOSUMDB=off"))
7389
return nil
7490
}
7591

76-
func (f FolderPublishCmd) handleModule(path, relPath string) {
77-
//dstDir := filepath.Join(f.Output, relPath)
92+
func (f FolderPublishCmd) handleModule(path, prefix string) {
93+
modD, err := os.Open(path)
94+
if err != nil {
95+
log.Println(errorRedPrefix, "failed to read module directory: ", err)
96+
return
97+
}
98+
defer modD.Close()
99+
100+
files, err := modD.Readdirnames(0)
101+
if err != nil {
102+
log.Println(errorRedPrefix, "failed to read module directory: ", err)
103+
return
104+
}
105+
106+
// Copy files
107+
for _, fi := range files {
108+
if fi == "list" || fi == "list.lock" || fi == "lock" {
109+
continue
110+
}
111+
112+
srcF := filepath.Join(path, fi)
113+
f.handleCopyFile(srcF, strings.TrimLeft(strings.TrimPrefix(srcF, prefix), string(filepath.Separator)))
114+
}
115+
116+
var version []string
117+
dstPath := filepath.Join(f.Output, strings.TrimLeft(strings.TrimPrefix(path, prefix), string(filepath.Separator)))
118+
dstF, err := os.Open(dstPath)
119+
if err != nil {
120+
log.Println(errorRedPrefix, "failed to update list file: ", err)
121+
return
122+
}
123+
defer dstF.Close()
78124

125+
modules, err := dstF.Readdirnames(0)
126+
if err != nil {
127+
log.Println(errorRedPrefix, "failed to update list file: ", err)
128+
return
129+
}
130+
131+
for _, v := range modules {
132+
if strings.HasSuffix(v, ".mod") {
133+
version = append(version, strings.TrimSuffix(v, ".mod"))
134+
}
135+
}
136+
137+
content := []byte(strings.Join(version, "\n"))
138+
content = append(content, '\n')
139+
if err := ioutil.WriteFile(filepath.Join(dstPath, "list"), content, 0664); err != nil {
140+
log.Println(errorRedPrefix, "failed to update list file: ", err)
141+
return
142+
}
79143
}
80144

81145
func (f FolderPublishCmd) handleCopyFile(path, relPath string) {

0 commit comments

Comments
 (0)