Skip to content

Commit 8d9f64a

Browse files
committed
Implment handlers
1 parent 70a5750 commit 8d9f64a

File tree

8 files changed

+90
-44
lines changed

8 files changed

+90
-44
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
1616

17-
,idea/
17+
.idea/
18+
.vscode/

.idea/.gitignore

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

.idea/go-offline-packager.iml

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

.idea/modules.xml

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

.idea/vcs.xml

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

main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package main
33
import (
44
"archive/zip"
55
"errors"
6-
"github.com/go-sharp/color"
7-
"github.com/jessevdk/go-flags"
86
"io"
97
"io/ioutil"
108
"log"
119
"os"
1210
"os/exec"
1311
"path/filepath"
1412
"strings"
13+
14+
"github.com/go-sharp/color"
15+
"github.com/jessevdk/go-flags"
1516
)
1617

1718
var commonOpts options
@@ -65,7 +66,7 @@ func createTempWorkDir() (wd string, cleanFn func()) {
6566
func removeContent(dir string) {
6667
defer func() {
6768
if err := os.Remove(dir); err != nil {
68-
verboseF("can't remove directory %v: %v\n", err)
69+
verboseF("can't remove directory: %v\n", err)
6970
}
7071
}()
7172

@@ -99,7 +100,7 @@ func removeContent(dir string) {
99100

100101
_ = os.Chmod(fpath, 0666)
101102
if err := os.Remove(fpath); err != nil {
102-
verboseF("can't remove directory %v: %v\n", err)
103+
verboseF("can't remove directory: %v\n", err)
103104
}
104105
}
105106
}
@@ -143,7 +144,7 @@ func extractZipArchive(src, dst string) error {
143144
return nil
144145
}
145146

146-
func extractToFile(f *zip.File, dst string) {
147+
func extractToFile(f *zip.File, dst string) {
147148
destF, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0666)
148149
if err != nil {
149150
log.Println(errorRedPrefix, "failed to extract file", f.Name, ":", err)

pack.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package main
22

33
import (
4-
"github.com/go-sharp/color"
54
"io/ioutil"
65
"log"
76
"os"
87
"os/exec"
98
"path/filepath"
9+
10+
"github.com/go-sharp/color"
1011
)
1112

1213
type PackCmd struct {
@@ -31,7 +32,7 @@ func (p *PackCmd) Execute(args []string) error {
3132
defer cleanFn()
3233

3334
modCache := filepath.Join(workDir, "modcache")
34-
if err := os.Mkdir(modCache, 0777); err != nil {
35+
if err := os.Mkdir(modCache, 0774); err != nil {
3536
log.Fatalf("%v: failed to create mod cache directory: %v\n", color.RedString("error"), err)
3637
}
3738

@@ -41,12 +42,12 @@ func (p *PackCmd) Execute(args []string) error {
4142
if err != nil {
4243
log.Fatalf("failed to copy go.mod file: %v\n", color.RedString(err.Error()))
4344
}
44-
if err := ioutil.WriteFile(filepath.Join(workDir, "go.mod"), modContent, 0666); err != nil {
45+
if err := ioutil.WriteFile(filepath.Join(workDir, "go.mod"), modContent, 0664); err != nil {
4546
log.Fatalf("failed to copy go.mod file: %v\n", color.RedString(err.Error()))
4647
}
4748
} else {
4849
verboseF("processing modules\n")
49-
if err := ioutil.WriteFile(filepath.Join(workDir, "go.mod"), []byte(gomodTemp), 0666); err != nil {
50+
if err := ioutil.WriteFile(filepath.Join(workDir, "go.mod"), []byte(gomodTemp), 0664); err != nil {
5051
log.Fatalf("failed to write go.mod file: %v\n", color.RedString(err.Error()))
5152
}
5253

@@ -66,7 +67,7 @@ func (p *PackCmd) Execute(args []string) error {
6667
cmd := exec.Command(commonOpts.GoBinPath, "mod", "download", "all")
6768
cmd.Dir = workDir
6869
cmd.Env = append(os.Environ(), "GOMODCACHE="+modCache)
69-
if err := cmd.Run(); err != nil {
70+
if err := cmd.Run(); err != nil {
7071
log.Fatalln("failed to download dependencies:", color.RedString(err.Error()))
7172
}
7273

publish.go

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package main
22

33
import (
44
"errors"
5+
"io"
56
"log"
67
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/go-sharp/color"
712
)
813

914
type publishCmd struct {
10-
PosArgs struct{
15+
PosArgs struct {
1116
Archive string `positional-arg-name:"ARCHIVE" description:"Path to archive with dependencies. " default:"gop_dependencies.zip"`
1217
} `positional-args:"yes" required:"1"`
1318
}
@@ -23,24 +28,93 @@ func (f FolderPublishCmd) Execute(args []string) error {
2328
workDir, cleanFn := createTempWorkDir()
2429
defer cleanFn()
2530

31+
log.Println("extracting archive")
32+
2633
defaultErrStr := errorRedPrefix + " failed to extract archive:"
2734
if err := extractZipArchive(f.PosArgs.Archive, workDir); err != nil {
2835
log.Fatalln(defaultErrStr, err)
2936
}
3037

38+
// Prepare output folder
3139
fi, err := os.Stat(f.Output)
3240
if err != nil {
3341
if !errors.Is(err, os.ErrNotExist) {
3442
log.Fatalln(defaultErrStr, err)
3543
}
36-
if err := os.MkdirAll(f.Output, 0777); err != nil {
44+
if err := os.MkdirAll(f.Output, 0774); err != nil {
3745
log.Fatalln(defaultErrStr, err)
3846
}
3947
} else if !fi.IsDir() {
4048
log.Fatalln(errorRedPrefix, "output is not a directory:", f.Output)
4149
}
4250

51+
log.Println("processing files")
52+
dirPrefix := filepath.Join(workDir, "cache", "download")
53+
err = filepath.Walk(dirPrefix, func(path string, info os.FileInfo, err error) error {
54+
relPath := strings.TrimLeft(strings.TrimPrefix(path, dirPrefix), string(filepath.Separator))
55+
56+
if strings.HasPrefix(relPath, "sumdb") && !info.IsDir() {
57+
go f.handleCopyFile(path, relPath)
58+
return nil
59+
}
60+
61+
if info.IsDir() && strings.HasSuffix(relPath, "@v") {
62+
go f.handleModule(path, relPath)
63+
return nil
64+
}
65+
66+
return nil
67+
})
68+
69+
if err != nil {
70+
log.Println(errorRedPrefix, err)
71+
}
72+
4373
return nil
4474
}
4575

76+
func (f FolderPublishCmd) handleModule(path, relPath string) {
77+
//dstDir := filepath.Join(f.Output, relPath)
4678

79+
}
80+
81+
func (f FolderPublishCmd) handleCopyFile(path, relPath string) {
82+
dstPath := filepath.Join(f.Output, relPath)
83+
if _, err := os.Stat(dstPath); !errors.Is(err, os.ErrNotExist) {
84+
reason := "file exists"
85+
if err != nil {
86+
reason = err.Error()
87+
}
88+
verboseF("skipping file %v: %v\n", color.YellowString(relPath), reason)
89+
return
90+
}
91+
92+
dstDir := filepath.Dir(dstPath)
93+
if st, err := os.Stat(dstDir); errors.Is(err, os.ErrNotExist) {
94+
// We don't care if we can't create dir, it will fail when we try to copy the file
95+
_ = os.MkdirAll(dstDir, 0774)
96+
} else if !st.IsDir() {
97+
log.Println(errorRedPrefix, "failed to copy file destination is not a directory: ", dstDir)
98+
return
99+
}
100+
101+
srcF, err := os.Open(path)
102+
if err != nil {
103+
log.Println(errorRedPrefix, "failed to read src:", err)
104+
return
105+
}
106+
defer srcF.Close()
107+
108+
dstF, err := os.OpenFile(dstPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0664)
109+
if err != nil {
110+
log.Println(errorRedPrefix, "failed to create file:", err)
111+
return
112+
}
113+
defer dstF.Close()
114+
115+
if _, err := io.Copy(dstF, srcF); err != nil {
116+
117+
log.Println(errorRedPrefix, "failed to copy file:", err)
118+
return
119+
}
120+
}

0 commit comments

Comments
 (0)