Skip to content

Commit

Permalink
create a unique tmp path every-time package scanner runs (#70)
Browse files Browse the repository at this point in the history
create a unique tmp path everytime package scanner runs
  • Loading branch information
gnmahanth authored Apr 4, 2024
1 parent 13b81b2 commit 35c590e
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,28 @@ func main() {
if dirErr != nil {
log.Fatal(dirErr)
}
tmpPath := path.Join(cacheDir, "package-scanner")

tmpPath, tmpErr := os.MkdirTemp(cacheDir, "package-scanner-*")
if tmpErr != nil {
log.Fatal(tmpErr)
}

// remove tmpPath on exit
defer func() {
log.Debugf("remove tools cache %s", tmpPath)
if err := os.RemoveAll(tmpPath); err != nil {
log.Fatal(err)
}
}()

syftBinPath := path.Join(tmpPath, "syft")
grypeBinPath := path.Join(tmpPath, "grype")
grypeConfigPath := path.Join(tmpPath, "grype.yaml")
log.Debugf("user cache dir: %s", cacheDir)

log.Debugf("tools cache dir: %s", tmpPath)
log.Debugf("tools paths: %s %s %s", syftBinPath, grypeBinPath, grypeConfigPath)

// extract embedded binaries
if err := os.MkdirAll(tmpPath, 0755); err != nil {
log.Fatal(err)
}
if err := os.WriteFile(syftBinPath, tools.SyftBin, 0755); err != nil {
log.Fatal(err)

Check failure on line 119 in main.go

View workflow job for this annotation

GitHub Actions / lint

exitAfterDefer: log.Fatal will exit, and `defer func(){...}(...)` will not run (gocritic)
}
Expand All @@ -113,13 +124,6 @@ func main() {
if err := os.WriteFile(grypeConfigPath, grypeYaml, 0755); err != nil {
log.Fatal(err)
}
// remove on exit
defer func() {
log.Debugf("remove tools cache %s", tmpPath)
if err := os.RemoveAll(tmpPath); err != nil {
log.Fatal(err)
}
}()

// make sure logs come to stdout in other modes except local
// local logs go to stderr to keep stdout clean for redirecting to file
Expand Down

0 comments on commit 35c590e

Please sign in to comment.