-
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.
Merge pull request #14 from eunomie/cache
feat: cache runx readme and config locally
- Loading branch information
Showing
3 changed files
with
180 additions
and
62 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