mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-22 07:10:41 -08:00
Clear cache (fanal#8)
This commit is contained in:
23
cache/cache.go
vendored
23
cache/cache.go
vendored
@@ -5,20 +5,13 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/knqyf263/fanal/utils"
|
||||||
|
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
d, err := os.UserCacheDir()
|
|
||||||
if err != nil {
|
|
||||||
d = os.TempDir()
|
|
||||||
}
|
|
||||||
cacheDir = filepath.Join(d, "fanal")
|
|
||||||
os.MkdirAll(cacheDir, os.ModePerm)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cacheDir string
|
cacheDir = utils.CacheDir()
|
||||||
)
|
)
|
||||||
|
|
||||||
func Get(key string) io.Reader {
|
func Get(key string) io.Reader {
|
||||||
@@ -32,6 +25,9 @@ func Get(key string) io.Reader {
|
|||||||
|
|
||||||
func Set(key string, file io.Reader) (io.Reader, error) {
|
func Set(key string, file io.Reader) (io.Reader, error) {
|
||||||
filePath := filepath.Join(cacheDir, key)
|
filePath := filepath.Join(cacheDir, key)
|
||||||
|
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
|
||||||
|
return nil, xerrors.Errorf("failed to mkdir all: %w", err)
|
||||||
|
}
|
||||||
cacheFile, err := os.Create(filePath)
|
cacheFile, err := os.Create(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return file, xerrors.Errorf("failed to create cache file: %w", err)
|
return file, xerrors.Errorf("failed to create cache file: %w", err)
|
||||||
@@ -40,3 +36,10 @@ func Set(key string, file io.Reader) (io.Reader, error) {
|
|||||||
tee := io.TeeReader(file, cacheFile)
|
tee := io.TeeReader(file, cacheFile)
|
||||||
return tee, nil
|
return tee, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Clear() error {
|
||||||
|
if err := os.RemoveAll(utils.CacheDir()); err != nil {
|
||||||
|
return xerrors.New("failed to remove cache")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
|
"github.com/knqyf263/fanal/cache"
|
||||||
|
|
||||||
"github.com/knqyf263/fanal/analyzer"
|
"github.com/knqyf263/fanal/analyzer"
|
||||||
_ "github.com/knqyf263/fanal/analyzer/library/bundler"
|
_ "github.com/knqyf263/fanal/analyzer/library/bundler"
|
||||||
_ "github.com/knqyf263/fanal/analyzer/library/composer"
|
_ "github.com/knqyf263/fanal/analyzer/library/composer"
|
||||||
@@ -33,8 +37,15 @@ func main() {
|
|||||||
func run() (err error) {
|
func run() (err error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tarPath := flag.String("f", "-", "layer.tar path")
|
tarPath := flag.String("f", "-", "layer.tar path")
|
||||||
|
clearCache := flag.Bool("clear", false, "clear cache")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
if *clearCache {
|
||||||
|
if err = cache.Clear(); err != nil {
|
||||||
|
return xerrors.Errorf("error in cache clear: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
|
||||||
var files extractor.FileMap
|
var files extractor.FileMap
|
||||||
|
|||||||
@@ -1,5 +1,19 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CacheDir() string {
|
||||||
|
cacheDir, err := os.UserCacheDir()
|
||||||
|
if err != nil {
|
||||||
|
cacheDir = os.TempDir()
|
||||||
|
}
|
||||||
|
dir := filepath.Join(cacheDir, "fanal")
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
||||||
func StringInSlice(a string, list []string) bool {
|
func StringInSlice(a string, list []string) bool {
|
||||||
for _, b := range list {
|
for _, b := range list {
|
||||||
if b == a {
|
if b == a {
|
||||||
|
|||||||
Reference in New Issue
Block a user