MCPcopy Create free account
hub / github.com/nhost/nhost / extractTarGz

Function extractTarGz

cli/software/sofware.go:134–175  ·  view source on GitHub ↗
(gzipStream io.Reader, dst io.Writer)

Source from the content-addressed store, hash-verified

132}
133
134func extractTarGz(gzipStream io.Reader, dst io.Writer) error {
135 uncompressedStream, err := gzip.NewReader(gzipStream)
136 if err != nil {
137 return fmt.Errorf("gzip reader failed: %w", err)
138 }
139
140 tarReader := tar.NewReader(uncompressedStream)
141
142 for {
143 header, err := tarReader.Next()
144
145 if errors.Is(err, io.EOF) {
146 break
147 }
148
149 if err != nil {
150 return fmt.Errorf("tar reader failed: %w", err)
151 }
152
153 switch header.Typeflag {
154 case tar.TypeDir:
155 return errors.New( //nolint:err113
156 "expected a file inside tarball, found a directory instead",
157 )
158 case tar.TypeReg:
159 if _, err := io.Copy(dst, tarReader); err != nil { //nolint:gosec
160 return fmt.Errorf("failed to copy file: %w", err)
161 }
162
163 return nil
164
165 default:
166 return fmt.Errorf( //nolint:err113
167 "unknown type: %b in %s",
168 header.Typeflag,
169 header.Name,
170 )
171 }
172 }
173
174 return errors.New("no file found in tarball") //nolint:err113
175}

Callers 1

DownloadAssetMethod · 0.85

Calls 2

ErrorfMethod · 0.80
NextMethod · 0.65

Tested by

no test coverage detected