Skip to content

Commit

Permalink
feat: add ability to clear cache on exit (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark deVilliers authored Jan 4, 2023
1 parent 9413264 commit ea41a72
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ test:

# The linting gods must be obeyed
.PHONY: lint
lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v$(GOLANGCI_LINT_VERSION)
lint: ./bin/golangci-lint
./bin/golangci-lint run

./bin/golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v$(GOLANGCI_LINT_VERSION)

# Generate the mocks (embedded via go generate)
.PHONY: mocks
mocks:
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmds/cmd_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ func imagesArgoCommand() *cli.Command {
Name: "mapping",
Usage: "path to a mapping file",
},
&cli.BoolFlag{
Name: "delete-cache-on-exit",
Usage: "deletes all caches on exit",
},
output.CLIOutputJSONFlag,
},
Action: func(c *cli.Context) error {
paths := c.Value("path").(cli.StringSlice)
argo := images.NewArgo(paths.Value()...)
deleteCache := c.Value("delete-cache-on-exit").(bool)
argo := images.NewArgo(deleteCache, paths.Value()...)
return getImages(c, argo)
},
}
Expand Down
18 changes: 14 additions & 4 deletions pkg/providers/images/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
)

type argoProvider struct {
paths []string
deleteCacheOnExit bool
paths []string
}

func NewArgo(paths ...string) *argoProvider {
func NewArgo(deleteCacheOnExit bool, paths ...string) *argoProvider {
return &argoProvider{
paths: paths,
deleteCacheOnExit: deleteCacheOnExit,
paths: paths,
}
}

Expand Down Expand Up @@ -50,6 +52,13 @@ func (a *argoProvider) Images(ctx context.Context) ([]mapping.Image, error) {

// use the temp dir as the root of any checkout
directory := os.TempDir()
directory = path.Join(directory, "scrng")

if a.deleteCacheOnExit {
defer func() {
os.RemoveAll(directory)
}()
}

for _, p := range a.paths {

Expand Down Expand Up @@ -130,7 +139,8 @@ func runHelm(root string, app ArgoApplication) (string, error) {
// clones the githubURL and checkouts the 'tagOrHead' returing the directory path or an error
func cachedGithubCheckout(directory string, githubURL, tagOrHead string) (string, error) {

folder := fmt.Sprintf("./%s-%s", githubURL, tagOrHead)
githubFolderName := strings.ReplaceAll(githubURL, "/", "_")
folder := fmt.Sprintf("./%s-%s", githubFolderName, tagOrHead)
p := path.Join(directory, folder)

if stat, err := os.Stat(p); err == nil && stat.IsDir() {
Expand Down
1 change: 0 additions & 1 deletion pkg/providers/images/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func resolveImages(probablyYaml, namespace string) ([]mapping.Image, error) { //
if err != nil {
return nil, errors.Wrap(err, "error running replicas xpath")
}

for _, element := range imageElements {
image, version := splitImageAndVersion(strings.TrimSpace(element.Value))
i := mapping.Image{
Expand Down

0 comments on commit ea41a72

Please sign in to comment.