Reimplement --cache-dir option (#114)

This commit is contained in:
Teppei Fukuda
2019-08-12 22:04:45 -10:00
committed by GitHub
parent 11bc00d629
commit a7d991f3cc
5 changed files with 32 additions and 6 deletions

View File

@@ -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
```

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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")

View File

@@ -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 {