MCPcopy Create free account
hub / github.com/conforma/cli / tarGzipFile

Function tarGzipFile

acceptance/kubernetes/kind/image.go:422–462  ·  view source on GitHub ↗

Tar and gzip a file. Used with trusted artifacts.

(source, target string)

Source from the content-addressed store, hash-verified

420
421// Tar and gzip a file. Used with trusted artifacts.
422func tarGzipFile(source, target string) error {
423 srcFile, err := os.Open(source)
424 if err != nil {
425 return fmt.Errorf("opening source file: %w", err)
426 }
427 defer srcFile.Close()
428
429 outFile, err := os.Create(target)
430 if err != nil {
431 return fmt.Errorf("creating target file: %w", err)
432 }
433 defer outFile.Close()
434
435 gzw := gzip.NewWriter(outFile)
436 defer gzw.Close()
437
438 tw := tar.NewWriter(gzw)
439 defer tw.Close()
440
441 info, err := srcFile.Stat()
442 if err != nil {
443 return fmt.Errorf("getting source file info: %w", err)
444 }
445
446 header := &tar.Header{
447 Name: filepath.Base(source),
448 Mode: int64(info.Mode()),
449 Size: info.Size(),
450 ModTime: info.ModTime(),
451 }
452
453 if err := tw.WriteHeader(header); err != nil {
454 return fmt.Errorf("writing tar header: %w", err)
455 }
456
457 if _, err := io.Copy(tw, srcFile); err != nil {
458 return fmt.Errorf("copying file content into tar: %w", err)
459 }
460
461 return nil
462}

Callers 1

BuildSnapshotArtifactMethod · 0.85

Calls 4

ErrorfMethod · 0.65
CloseMethod · 0.65
OpenMethod · 0.45
SizeMethod · 0.45

Tested by

no test coverage detected