-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 2954-poetry-package-tree
* main: chore(deps): update tools to latest versions (#2961) chore(deps): bump github/codeql-action from 3.25.9 to 3.25.10 (#2964) feat: index known CPEs for wordpress plugins and themes (#2963) fix(golang): improve version extraction from ldflags for pingcap TiDB (#2962) chore(deps): bump actions/checkout from 4.1.6 to 4.1.7 (#2955) chore(deps): bump github/codeql-action from 3.25.8 to 3.25.9 (#2956) fix: separate golang license caches from mod dir (#2852) chore(deps): bump github.com/vbatts/go-mtree from 0.5.3 to 0.5.4 (#2952) chore(deps): update tools to latest versions (#2949) chore(deps): bump modernc.org/sqlite from 1.30.0 to 1.30.1 (#2950)
- Loading branch information
Showing
50 changed files
with
13,388 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package options | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/adrg/xdg" | ||
"github.com/mitchellh/go-homedir" | ||
|
||
"github.com/anchore/clio" | ||
"github.com/anchore/syft/internal/cache" | ||
"github.com/anchore/syft/internal/log" | ||
) | ||
|
||
// Cache provides configuration for the Syft caching behavior | ||
type Cache struct { | ||
Dir string `yaml:"dir" mapstructure:"dir"` | ||
TTL string `yaml:"ttl" mapstructure:"ttl"` | ||
} | ||
|
||
func (c *Cache) DescribeFields(descriptions clio.FieldDescriptionSet) { | ||
descriptions.Add(&c.Dir, "root directory to cache any downloaded content") | ||
descriptions.Add(&c.TTL, "time to live for cached data") | ||
} | ||
|
||
func (c *Cache) PostLoad() error { | ||
if c.Dir != "" { | ||
ttl, err := parseDuration(c.TTL) | ||
if err != nil { | ||
log.Warnf("unable to parse duration '%v', using default (%s) due to: %v", c.TTL, durationToString(defaultTTL()), err) | ||
ttl = defaultTTL() | ||
} | ||
dir, err := homedir.Expand(c.Dir) | ||
if err != nil { | ||
log.Warnf("unable to expand cache directory %s: %v", c.Dir, err) | ||
cache.SetManager(cache.NewInMemory(ttl)) | ||
} else { | ||
m, err := cache.NewFromDir(dir, ttl) | ||
if err != nil { | ||
log.Warnf("unable to get filesystem cache at %s: %v", c.Dir, err) | ||
cache.SetManager(cache.NewInMemory(ttl)) | ||
} else { | ||
cache.SetManager(m) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
var _ interface { | ||
clio.PostLoader | ||
clio.FieldDescriber | ||
} = (*Cache)(nil) | ||
|
||
func DefaultCache() Cache { | ||
return Cache{ | ||
Dir: defaultDir(), | ||
TTL: durationToString(defaultTTL()), | ||
} | ||
} | ||
|
||
func defaultTTL() time.Duration { | ||
return 7 * 24 * time.Hour | ||
} | ||
|
||
func defaultDir() string { | ||
var err error | ||
cacheRoot := xdg.CacheHome | ||
if cacheRoot == "" { | ||
cacheRoot, err = homedir.Dir() | ||
if err != nil { | ||
cacheRoot = os.TempDir() | ||
log.Debugf("unable to get stable cache directory due to: %v, defaulting cache to temp dir: %s", err, cacheRoot) | ||
} else { | ||
cacheRoot = filepath.Join(cacheRoot, ".cache") | ||
} | ||
} | ||
|
||
return filepath.Join(cacheRoot, "syft") | ||
} | ||
|
||
func durationToString(duration time.Duration) string { | ||
days := int64(duration / (24 * time.Hour)) | ||
remain := duration % (24 * time.Hour) | ||
out := "" | ||
if days > 0 { | ||
out = fmt.Sprintf("%vd", days) | ||
} | ||
if remain != 0 { | ||
out += remain.String() | ||
} | ||
if out == "" { | ||
return "0" | ||
} | ||
return out | ||
} | ||
|
||
var whitespace = regexp.MustCompile(`\s+`) | ||
|
||
func parseDuration(duration string) (time.Duration, error) { | ||
duration = strings.ToLower(whitespace.ReplaceAllString(duration, "")) | ||
parts := strings.SplitN(duration, "d", 2) | ||
var days time.Duration | ||
var remain time.Duration | ||
var err error | ||
if len(parts) > 1 { | ||
numDays, daysErr := strconv.Atoi(parts[0]) | ||
if daysErr != nil { | ||
return 0, daysErr | ||
} | ||
days = time.Duration(numDays) * 24 * time.Hour | ||
remain, err = time.ParseDuration(parts[1]) | ||
} else { | ||
remain, err = time.ParseDuration(duration) | ||
} | ||
return days + remain, err | ||
} |
Oops, something went wrong.