github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/sharedaction/sharedaction_suite_test.go (about) 1 package sharedaction_test 2 3 import ( 4 "archive/zip" 5 "io" 6 "os" 7 "path/filepath" 8 "strings" 9 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 log "github.com/sirupsen/logrus" 13 14 "testing" 15 ) 16 17 func TestSharedAction(t *testing.T) { 18 RegisterFailHandler(Fail) 19 RunSpecs(t, "Shared Actions Suite") 20 } 21 22 var _ = BeforeEach(func() { 23 log.SetLevel(log.PanicLevel) 24 }) 25 26 // Thanks to Svett Ralchev 27 // http://blog.ralch.com/tutorial/golang-working-with-zip/ 28 func zipit(source, target, prefix string) error { 29 zipfile, err := os.Create(target) 30 if err != nil { 31 return err 32 } 33 defer zipfile.Close() 34 35 if prefix != "" { 36 _, err = io.WriteString(zipfile, prefix) 37 if err != nil { 38 return err 39 } 40 } 41 42 archive := zip.NewWriter(zipfile) 43 defer archive.Close() 44 45 err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error { 46 if err != nil { 47 return err 48 } 49 50 header, err := zip.FileInfoHeader(info) 51 if err != nil { 52 return err 53 } 54 55 header.Name = strings.TrimPrefix(path, source) 56 57 if info.IsDir() { 58 header.Name += string(os.PathSeparator) 59 } else { 60 header.Method = zip.Deflate 61 } 62 63 writer, err := archive.CreateHeader(header) 64 if err != nil { 65 return err 66 } 67 68 if info.IsDir() { 69 return nil 70 } 71 72 if info.Mode().IsRegular() { 73 file, err := os.Open(path) 74 if err != nil { 75 return err 76 } 77 defer file.Close() 78 79 _, err = io.Copy(writer, file) 80 return err 81 } else if info.Mode()&os.ModeSymlink != 0 { 82 pathInSymlink, err := os.Readlink(path) 83 if err != nil { 84 return err 85 } 86 symLinkContents := strings.NewReader(pathInSymlink) 87 if _, err := io.Copy(writer, symLinkContents); err != nil { 88 return err 89 } 90 } 91 return nil 92 }) 93 94 return err 95 }