(writer io.Writer, localPath string, exclude []string)
| 52 | } |
| 53 | |
| 54 | func writeTar(writer io.Writer, localPath string, exclude []string) error { |
| 55 | absolute, err := filepath.Abs(localPath) |
| 56 | if err != nil { |
| 57 | return errors.Wrap(err, "absolute") |
| 58 | } |
| 59 | |
| 60 | // Check if target is there |
| 61 | stat, err := os.Stat(absolute) |
| 62 | if err != nil { |
| 63 | return errors.Wrap(err, "stat") |
| 64 | } |
| 65 | |
| 66 | // Compile ignore paths |
| 67 | ignoreMatcher, err := ignoreparser.CompilePaths(exclude, log.Discard) |
| 68 | if err != nil { |
| 69 | return errors.Wrap(err, "compile exclude paths") |
| 70 | } |
| 71 | |
| 72 | // Use compression |
| 73 | gw := gzip.NewWriter(writer) |
| 74 | defer gw.Close() |
| 75 | |
| 76 | // Create tar writer |
| 77 | tarWriter := tar.NewWriter(gw) |
| 78 | defer tarWriter.Close() |
| 79 | |
| 80 | // When its a file we copy the file to the toplevel of the tar |
| 81 | if !stat.IsDir() { |
| 82 | return sync.NewArchiver(filepath.Dir(absolute), tarWriter, ignoreMatcher).AddToArchive(filepath.Base(absolute)) |
| 83 | } |
| 84 | |
| 85 | // When its a folder we copy the contents and not the folder itself to the |
| 86 | // toplevel of the tar |
| 87 | return sync.NewArchiver(absolute, tarWriter, ignoreMatcher).AddToArchive("") |
| 88 | } |
no test coverage detected