|
| 1 | +package remote_build_helpers |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/sirupsen/logrus" |
| 10 | +) |
| 11 | + |
| 12 | +// TempFileManager manages temporary files created during image build. |
| 13 | +// It maintains a list of created temporary files and provides cleanup functionality |
| 14 | +// to ensure proper resource management. |
| 15 | +type TempFileManager struct { |
| 16 | + files []string |
| 17 | +} |
| 18 | + |
| 19 | +func NewTempFileManager() *TempFileManager { |
| 20 | + return &TempFileManager{} |
| 21 | +} |
| 22 | + |
| 23 | +func (t *TempFileManager) AddFile(filename string) { |
| 24 | + t.files = append(t.files, filename) |
| 25 | +} |
| 26 | + |
| 27 | +func (t *TempFileManager) Cleanup() { |
| 28 | + for _, file := range t.files { |
| 29 | + if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 30 | + logrus.Errorf("Failed to remove temp file %s: %v", file, err) |
| 31 | + } |
| 32 | + } |
| 33 | + t.files = t.files[:0] // Reset slice |
| 34 | +} |
| 35 | + |
| 36 | +// CreateTempFileFromReader creates a temporary file in the specified destination directory |
| 37 | +// with the given pattern, and copies content from the provided reader into the file. |
| 38 | +// The created temporary file is automatically added to the manager's cleanup list. |
| 39 | +// |
| 40 | +// Parameters: |
| 41 | +// - dest: The directory where the temporary file should be created |
| 42 | +// - pattern: The pattern for naming the temporary file |
| 43 | +// - reader: The io.Reader from which to read content to write into the temporary file |
| 44 | +// |
| 45 | +// Returns: |
| 46 | +// - string: The path to the created temporary file |
| 47 | +// - error: Any error encountered during the operation |
| 48 | +func (t *TempFileManager) CreateTempFileFromReader(dest string, pattern string, reader io.Reader) (string, error) { |
| 49 | + tmpFile, err := os.CreateTemp(dest, pattern) |
| 50 | + if err != nil { |
| 51 | + return "", fmt.Errorf("creating temp file: %w", err) |
| 52 | + } |
| 53 | + defer tmpFile.Close() |
| 54 | + |
| 55 | + t.AddFile(tmpFile.Name()) |
| 56 | + |
| 57 | + if _, err := io.Copy(tmpFile, reader); err != nil { |
| 58 | + return "", fmt.Errorf("copying stdin content: %w", err) |
| 59 | + } |
| 60 | + return tmpFile.Name(), nil |
| 61 | +} |
| 62 | + |
| 63 | +// CreateTempSecret creates a temporary copy of a secret file in the specified |
| 64 | +// context directory. The original secret file is copied to a new temporary file |
| 65 | +// which is automatically added to the manager's cleanup list. |
| 66 | +// |
| 67 | +// Parameters: |
| 68 | +// - secretPath: The path to the source secret file to copy |
| 69 | +// - contextDir: The directory where the temporary secret file should be created |
| 70 | +// |
| 71 | +// Returns: |
| 72 | +// - string: The path to the created temporary secret file |
| 73 | +// - error: Any error encountered during the operation |
| 74 | +func (t *TempFileManager) CreateTempSecret(secretPath, contextDir string) (string, error) { |
| 75 | + secretFile, err := os.Open(secretPath) |
| 76 | + if err != nil { |
| 77 | + return "", fmt.Errorf("opening secret file %s: %w", secretPath, err) |
| 78 | + } |
| 79 | + defer secretFile.Close() |
| 80 | + |
| 81 | + return t.CreateTempFileFromReader(contextDir, "podman-build-secret-*", secretFile) |
| 82 | +} |
0 commit comments