@@ -2,12 +2,17 @@ package main
22
33import (
44 "errors"
5+ "io"
56 "log"
67 "os"
8+ "path/filepath"
9+ "strings"
10+
11+ "github.com/go-sharp/color"
712)
813
914type 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