Add clear-caches directive for reducing Docker image sizes#5
Add clear-caches directive for reducing Docker image sizes#5
Conversation
Co-authored-by: ericsuh <382805+ericsuh@users.noreply.github.com>
Co-authored-by: ericsuh <382805+ericsuh@users.noreply.github.com>
Co-authored-by: ericsuh <382805+ericsuh@users.noreply.github.com>
Co-authored-by: ericsuh <382805+ericsuh@users.noreply.github.com>
Fixed the GoReleaser CI failure by adding |
There was a problem hiding this comment.
Pull request overview
Adds a new clear-caches Aptfile directive to reduce final image/disk footprint by cleaning apt caches after all package installation has completed, aligning with common Docker optimization patterns.
Changes:
- Add
clear-cachesdirective parsing inaptfile(no args/options). - Defer cache-clearing execution in
processAptfileuntil afterinstallPackages. - Document the directive in the README and add a parser unit test.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| main.go | Collects clear-caches directives during parsing and runs a new clearCaches() step after installs. |
| aptfile/main.go | Introduces ClearCachesDirective and parsing logic for clear-caches. |
| aptfile/main_test.go | Adds a ParseLine unit test case for the new directive. |
| README.md | Documents clear-caches usage in the example Aptfile. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| pkgs := make([]aptfile.PackageDirective, 0) | ||
| clearCachesDirectives := make([]aptfile.ClearCachesDirective, 0) |
There was a problem hiding this comment.
clearCachesDirectives is only used as a presence check (len(...) > 0) and the directive struct is empty, so storing every occurrence is unnecessary. Consider replacing this slice with a simple boolean (e.g., clearCachesRequested) to better express intent and avoid redundant allocations/append calls.
| // Execute clear-caches once if any directives exist | ||
| if len(clearCachesDirectives) > 0 { | ||
| if err := clearCaches(dryRun); err != nil { | ||
| log.Fatalf("Failed to clear caches: %v", err) | ||
| } | ||
| } |
There was a problem hiding this comment.
New clear-caches behavior (deferral until after installPackages and idempotent execution) isn’t covered by tests. Since this file already has unit tests, add a test that runs processAptfile(..., dryRun=true) (or calls clearCaches(true)) while capturing stdout/stderr to assert the directive is executed exactly once and only after the install step.
Implementation Plan for clear-caches Feature
ClearCachesDirectivetype to aptfile packageparseClearCachesDirectivein aptfile/main.goParseLineswitch statement to handle "clear-caches" directiveclearCachesfunction in main.go to run apt-get clean and remove cache directoriesSummary
Successfully implemented the
clear-cachesdirective feature and fixed the unrelated CI build issue with GoReleaser that was failing because it expected a git tag on non-release builds.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.