Skip to content

Commit

Permalink
Merge pull request #136 from WillAbides/readcache2
Browse files Browse the repository at this point in the history
file-locking cache
  • Loading branch information
WillAbides authored Apr 12, 2023
2 parents 59b95e2 + a4c211d commit 2e2c39b
Show file tree
Hide file tree
Showing 24 changed files with 1,416 additions and 600 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
platform: [ ubuntu-20.04, macos-11 ]
platform: [ ubuntu-20.04, macos-11, windows-2019 ]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
Expand Down
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ linters:
- gofumpt
- revive
linters-settings:
gosec:
excludes:
- G204 # Subprocess launched with variable
- G301 # Expect directory permissions to be 0750 or less
- G302 # Expect file permissions to be 0600 or less
- G306 # Expect WriteFile permissions to be 0600 or less
gocritic:
enabled-tags:
- style
- diagnostic
- performance
disabled-checks:
- rangeValCopy
- ptrToRefParam
errcheck:
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ Commands:
checksums add add checksums to the config file
checksums prune remove unnecessary checksums from the config file
init create an empty config file
cache clear clear the cache
version show bindown version
install-completions install shell completions

Expand Down
15 changes: 15 additions & 0 deletions cmd/bindown/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

type cacheCmd struct {
Clear cacheClearCmd `kong:"cmd,help='clear the cache'"`
}

type cacheClearCmd struct{}

func (c *cacheClearCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, false)
if err != nil {
return err
}
return config.ClearCache()
}
50 changes: 50 additions & 0 deletions cmd/bindown/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/willabides/bindown/v3"
)

func Test_cacheClearCmd(t *testing.T) {
servePath := filepath.FromSlash("../../testdata/downloadables/fooinroot.tar.gz")
successServer := serveFile(t, servePath, "/foo/fooinroot.tar.gz", "")
depURL := successServer.URL + "/foo/fooinroot.tar.gz"

t.Run("removes populated cache", func(t *testing.T) {
runner := newCmdRunner(t)
// extract something to populate the cache
runner.writeConfig(&bindown.Config{
URLChecksums: map[string]string{
depURL: "27dcce60d1ed72920a84dd4bc01e0bbd013e5a841660e9ee2e964e53fb83c0b3",
},
Dependencies: map[string]*bindown.Dependency{
"foo": {URL: &depURL},
},
})
result := runner.run("extract", "foo")
extractDir := result.getExtractDir()
assert.FileExists(t, filepath.Join(extractDir, "foo"))
result = runner.run("cache", "clear")
result.assertState(resultState{})
assert.NoDirExists(t, extractDir)
})

t.Run("does nothing if cache is empty", func(t *testing.T) {
runner := newCmdRunner(t)
runner.writeConfig(&bindown.Config{})
result := runner.run("cache", "clear")
result.assertState(resultState{})
})

t.Run("errors on missing config", func(t *testing.T) {
runner := newCmdRunner(t)
result := runner.run("cache", "clear")
result.assertState(resultState{
exit: 1,
stderr: `no such file or directory`,
})
})
}
9 changes: 5 additions & 4 deletions cmd/bindown/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var kongVars = kong.Vars{
type rootCmd struct {
JSONConfig bool `kong:"name=json,help='treat config file as json instead of yaml'"`
Configfile string `kong:"type=path,help=${configfile_help},env='BINDOWN_CONFIG_FILE'"`
Cache string `kong:"type=path,help=${cache_help},env='BINDOWN_CACHE'"`
CacheDir string `kong:"name=cache,type=path,help=${cache_help},env='BINDOWN_CACHE'"`
TrustCache *bool `kong:"help=${trust_cache_help},env='BINDOWN_TRUST_CACHE'"`
Quiet bool `kong:"short='q',help='suppress output to stdout'"`

Expand All @@ -58,6 +58,7 @@ type rootCmd struct {
SupportedSystem supportedSystemCmd `kong:"cmd,help='manage supported systems'"`
Checksums checksumsCmd `kong:"cmd,help='manage checksums'"`
Init initCmd `kong:"cmd,help='create an empty config file'"`
Cache cacheCmd `kong:"cmd,help='manage the cache'"`

Version versionCmd `kong:"cmd,help='show bindown version'"`
InstallCompletions kongplete.InstallCompletions `kong:"cmd,help=${config_install_completions_help}"`
Expand Down Expand Up @@ -90,8 +91,8 @@ func loadConfigFile(ctx *runContext, noDefaultDirs bool) (*bindown.ConfigFile, e
if err != nil {
return nil, err
}
if ctx.rootCmd.Cache != "" {
configFile.Cache = ctx.rootCmd.Cache
if ctx.rootCmd.CacheDir != "" {
configFile.Cache = ctx.rootCmd.CacheDir
}
if ctx.rootCmd.TrustCache != nil {
configFile.TrustCache = *ctx.rootCmd.TrustCache
Expand Down Expand Up @@ -228,7 +229,7 @@ func (c *initCmd) Run(ctx *runContext) error {
type fmtCmd struct{}

func (c fmtCmd) Run(ctx *runContext, cli *rootCmd) error {
ctx.rootCmd.Cache = ""
ctx.rootCmd.CacheDir = ""
config, err := loadConfigFile(ctx, true)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 2e2c39b

Please sign in to comment.