1package server 2 3import ( 4 "github.com/urfave/cli/v2" 5 "golang.org/x/xerrors" 6 7 "github.com/aquasecurity/fanal/cache" 8 "github.com/aquasecurity/trivy-db/pkg/db" 9 "github.com/aquasecurity/trivy/internal/operation" 10 "github.com/aquasecurity/trivy/internal/server/config" 11 "github.com/aquasecurity/trivy/pkg/log" 12 "github.com/aquasecurity/trivy/pkg/rpc/server" 13 "github.com/aquasecurity/trivy/pkg/utils" 14) 15 16func Run(ctx *cli.Context) error { 17 return run(config.New(ctx)) 18} 19 20func run(c config.Config) (err error) { 21 if err = log.InitLogger(c.Debug, c.Quiet); err != nil { 22 return xerrors.Errorf("failed to initialize a logger: %w", err) 23 } 24 25 // initialize config 26 if err = c.Init(); err != nil { 27 return xerrors.Errorf("failed to initialize options: %w", err) 28 } 29 30 // configure cache dir 31 utils.SetCacheDir(c.CacheDir) 32 log.Logger.Debugf("cache dir: %s", utils.CacheDir()) 33 34 fsCache, err := cache.NewFSCache(utils.CacheDir()) 35 if err != nil { 36 return xerrors.Errorf("unable to initialize cache: %w", err) 37 } 38 39 // server doesn't have image cache 40 cacheOperation := operation.NewCache(fsCache) 41 if c.Reset { 42 return cacheOperation.ClearDB() 43 } 44 45 // download the database file 46 if err = operation.DownloadDB(c.AppVersion, c.CacheDir, true, false, c.SkipUpdate); err != nil { 47 return err 48 } 49 50 if c.DownloadDBOnly { 51 return nil 52 } 53 54 if err = db.Init(c.CacheDir); err != nil { 55 return xerrors.Errorf("error in vulnerability DB initialize: %w", err) 56 } 57 58 return server.ListenAndServe(c, fsCache) 59} 60