|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "encoding/json" |
| 6 | + log "github.com/sirupsen/logrus" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path" |
| 11 | + "path/filepath" |
| 12 | +) |
| 13 | + |
| 14 | +type ArchiveDownload struct { |
| 15 | + Url string `json:"url"` |
| 16 | + TargetPath string `json:"target_path"` |
| 17 | +} |
| 18 | + |
| 19 | +func DownloadCodeArchives(archives string) error { |
| 20 | + if archives == "" { |
| 21 | + log.Debugln("No code archives set. Skipping download.") |
| 22 | + return nil |
| 23 | + } |
| 24 | + var parsedArchives []ArchiveDownload |
| 25 | + err := json.Unmarshal([]byte(archives), &parsedArchives) |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + |
| 30 | + for _, downloadArchive := range parsedArchives { |
| 31 | + if err := DownloadCodeArchive(downloadArchive.Url, downloadArchive.TargetPath); err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + } |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +func DownloadCodeArchive(url string, targetPath string) error { |
| 39 | + // download and unzip code archive |
| 40 | + log.Infoln("Downloading code archive") |
| 41 | + // create tmp directory |
| 42 | + // empty string will make use of the default tmp directory |
| 43 | + tmpDir, err := os.MkdirTemp("", "localstack-code-archive") |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + // download code archive into tmp directory |
| 48 | + res, err := http.Get(url) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + defer res.Body.Close() |
| 53 | + tmp_file_path := path.Join(tmpDir, "code-archive.zip") |
| 54 | + tmp_file, err := os.OpenFile(tmp_file_path, os.O_WRONLY|os.O_CREATE, os.ModePerm) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + _, err = io.Copy(tmp_file, res.Body) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + err = tmp_file.Close() |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + // unzip into targetPath |
| 67 | + log.Infoln("Unzipping code archive") |
| 68 | + r, err := zip.OpenReader(tmp_file_path) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + defer r.Close() |
| 73 | + for _, f := range r.File { |
| 74 | + rc, err := f.Open() |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + // TODO: check if exists, otherwise build path |
| 79 | + target_file_name := path.Join(targetPath, f.Name) |
| 80 | + if f.FileInfo().IsDir() { |
| 81 | + err = os.MkdirAll(target_file_name, os.ModePerm) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + continue |
| 86 | + } |
| 87 | + if err := os.MkdirAll(filepath.Dir(target_file_name), os.ModePerm); err != nil { |
| 88 | + panic(err) |
| 89 | + } |
| 90 | + target_file, err := os.OpenFile(target_file_name, os.O_WRONLY|os.O_CREATE, os.ModePerm) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + _, err = io.Copy(target_file, rc) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + target_file.Close() |
| 99 | + rc.Close() |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
0 commit comments