mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-05 20:40:16 -08:00
Reimplement --cache-dir option (#114)
This commit is contained in:
@@ -1020,6 +1020,12 @@ Total: 0 (UNKNOWN: 0, LOW: 0, MEDIUM: 0, HIGH: 0, CRITICAL: 0)
|
||||
|
||||
</details>
|
||||
|
||||
### Specify cache directory
|
||||
|
||||
```
|
||||
$ trivy --cache-dir /tmp/trivy/ python:3.4-alpine3.9
|
||||
```
|
||||
|
||||
### Clear image caches
|
||||
|
||||
The `--clear-cache` option removes image caches. This option is useful if the image which has the same tag is updated (such as when using `latest` tag).
|
||||
@@ -1249,6 +1255,7 @@ OPTIONS:
|
||||
--auto-refresh refresh DB automatically when updating version of trivy
|
||||
--debug, -d debug mode
|
||||
--vuln-type value comma-separated list of vulnerability types (os,library)
|
||||
--cache-dir value cache directory (default: "/path/to/cache")
|
||||
--help, -h show help
|
||||
--version, -v print the version
|
||||
```
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/knqyf263/trivy/pkg/utils"
|
||||
|
||||
"github.com/knqyf263/trivy/pkg/vulnsrc/vulnerability"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
@@ -106,6 +108,11 @@ OPTIONS:
|
||||
Value: "os,library",
|
||||
Usage: "comma-separated list of vulnerability types (os,library)",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "cache-dir",
|
||||
Value: utils.DefaultCacheDir(),
|
||||
Usage: "cache directory",
|
||||
},
|
||||
}
|
||||
|
||||
app.Action = pkg.Run
|
||||
|
||||
@@ -16,10 +16,11 @@ import (
|
||||
|
||||
var (
|
||||
db *bolt.DB
|
||||
dbDir = filepath.Join(utils.CacheDir(), "db")
|
||||
dbDir string
|
||||
)
|
||||
|
||||
func Init() (err error) {
|
||||
dbDir = filepath.Join(utils.CacheDir(), "db")
|
||||
if err = os.MkdirAll(dbDir, 0700); err != nil {
|
||||
return xerrors.Errorf("failed to mkdir: %w", err)
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ func Run(c *cli.Context) (err error) {
|
||||
if err = log.InitLogger(debug); err != nil {
|
||||
l.Fatal(err)
|
||||
}
|
||||
|
||||
utils.SetCacheDir(c.String("cache-dir"))
|
||||
log.Logger.Debugf("cache dir: %s", utils.CacheDir())
|
||||
|
||||
reset := c.Bool("reset")
|
||||
|
||||
@@ -12,13 +12,22 @@ import (
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
func CacheDir() string {
|
||||
cacheDir, err := os.UserCacheDir()
|
||||
var cacheDir string
|
||||
|
||||
func DefaultCacheDir() string {
|
||||
tmpDir, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
cacheDir = os.TempDir()
|
||||
tmpDir = os.TempDir()
|
||||
}
|
||||
dir := filepath.Join(cacheDir, "trivy")
|
||||
return dir
|
||||
return filepath.Join(tmpDir, "trivy")
|
||||
}
|
||||
|
||||
func CacheDir() string {
|
||||
return cacheDir
|
||||
}
|
||||
|
||||
func SetCacheDir(dir string) {
|
||||
cacheDir = dir
|
||||
}
|
||||
|
||||
func FileWalk(root string, targetFiles map[string]struct{}, walkFn func(r io.Reader, path string) error) error {
|
||||
|
||||
Reference in New Issue
Block a user