( ctx context.Context, outputDirPath string, owner string, repository string, ref string, )
| 56 | } |
| 57 | |
| 58 | func (a *archiveReader) GetArchive( |
| 59 | ctx context.Context, |
| 60 | outputDirPath string, |
| 61 | owner string, |
| 62 | repository string, |
| 63 | ref string, |
| 64 | ) (retErr error) { |
| 65 | a.lock.Lock() |
| 66 | defer a.lock.Unlock() |
| 67 | |
| 68 | outputDirPath = normalpath.Unnormalize(outputDirPath) |
| 69 | |
| 70 | // creates a file in the same parent directory as outputDirPath |
| 71 | unlocker, err := filelock.Lock( |
| 72 | ctx, |
| 73 | outputDirPath+".lock", |
| 74 | filelock.LockWithTimeout(filelockTimeout), |
| 75 | ) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | defer func() { |
| 80 | retErr = errors.Join(retErr, unlocker.Unlock()) |
| 81 | }() |
| 82 | |
| 83 | // check if already exists, if so, do nothing |
| 84 | // OK to use os.Stat here |
| 85 | if fileInfo, err := os.Stat(outputDirPath); err == nil { |
| 86 | if !fileInfo.IsDir() { |
| 87 | return fmt.Errorf("expected %s to be a directory", outputDirPath) |
| 88 | } |
| 89 | return nil |
| 90 | } |
| 91 | request, err := http.NewRequestWithContext( |
| 92 | ctx, |
| 93 | "GET", |
| 94 | fmt.Sprintf("https://github.com/%s/%s/archive/%s.tar.gz", owner, repository, ref), |
| 95 | nil, |
| 96 | ) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | response, err := a.httpClient.Do(request) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | defer func() { |
| 105 | retErr = errors.Join(retErr, response.Body.Close()) |
| 106 | }() |
| 107 | if response.StatusCode != http.StatusOK { |
| 108 | return fmt.Errorf("expected HTTP status code %d to be %d", response.StatusCode, http.StatusOK) |
| 109 | } |
| 110 | gzipReader, err := gzip.NewReader(response.Body) |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | defer func() { |
| 115 | retErr = errors.Join(retErr, gzipReader.Close()) |
nothing calls this directly
no test coverage detected