-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache runx readme and config locally
So that we only need to perform a HEAD to get the digest of the reference from the user. This HEAD request and the fact we are using the digest as a key allow to invalidate cache each time the remote image change. Cache for a specific image with digest DIGEST contains these two items: - ~/.docker/runx/cache/sha256/sha256:DIGEST/README.md - ~/.docker/runx/cache/sha256/sha256:DIGEST/runx.yaml Fixes #1 Signed-off-by: Yves Brissaud <[email protected]>
- Loading branch information
Showing
3 changed files
with
192 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package runkit | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/docker/cli/cli/command" | ||
"github.com/eunomie/docker-runx/internal/constants" | ||
) | ||
|
||
const ( | ||
runxConfigFile = "runx.yaml" | ||
runxDocFile = "README.md" | ||
) | ||
|
||
var subCacheDir = filepath.Join(constants.SubCommandName, "cache", "sha256") | ||
|
||
type LocalCache struct { | ||
cacheDir string | ||
} | ||
|
||
func NewLocalCache(cli command.Cli) *LocalCache { | ||
rootDir := filepath.Dir(cli.ConfigFile().Filename) | ||
cacheDir := filepath.Join(rootDir, subCacheDir) | ||
|
||
return &LocalCache{ | ||
cacheDir: cacheDir, | ||
} | ||
} | ||
|
||
func (c *LocalCache) Get(digest string) (*RunKit, error) { | ||
rk := &RunKit{ | ||
Files: make(map[string]string), | ||
} | ||
found := false | ||
|
||
configFile := filepath.Join(c.cacheDir, digest, runxConfigFile) | ||
if runxConfig, err := os.ReadFile(configFile); err != nil { | ||
if !os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
} else { | ||
if err = decodeConfig(rk, digest, runxConfig); err != nil { | ||
return nil, err | ||
} | ||
found = true | ||
} | ||
|
||
readmeFile := filepath.Join(c.cacheDir, digest, runxDocFile) | ||
if runxDoc, err := os.ReadFile(readmeFile); err != nil { | ||
if !os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
} else { | ||
rk.Readme = string(runxDoc) | ||
found = true | ||
} | ||
|
||
if found { | ||
return rk, nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (c *LocalCache) Set(digest string, runxConfig, runxDoc []byte) error { | ||
digestDir := filepath.Join(c.cacheDir, digest) | ||
if err := os.MkdirAll(digestDir, 0o755); err != nil { | ||
return err | ||
} | ||
if len(runxConfig) > 0 { | ||
configFile := filepath.Join(c.cacheDir, digest, runxConfigFile) | ||
if err := os.WriteFile(configFile, runxConfig, 0o644); err != nil { | ||
return err | ||
} | ||
} | ||
if len(runxDoc) > 0 { | ||
readmeFile := filepath.Join(c.cacheDir, digest, runxDocFile) | ||
if err := os.WriteFile(readmeFile, runxDoc, 0o644); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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