do not resolve cache dir until remote resource is in use

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-10-31 08:38:01 +01:00 committed by Nicolas De loof
parent 0d905a896d
commit 71237ef62b
3 changed files with 18 additions and 28 deletions

View File

@ -256,15 +256,8 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
}
func (o *ProjectOptions) configureRemoteLoaders(dockerCli command.Cli, po []cli.ProjectOptionsFn) ([]cli.ProjectOptionsFn, error) {
git, err := remote.NewGitRemoteLoader(o.Offline)
if err != nil {
return nil, err
}
oci, err := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
if err != nil {
return nil, err
}
git := remote.NewGitRemoteLoader(o.Offline)
oci := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
po = append(po, cli.WithResourceLoader(git), cli.WithResourceLoader(oci))
return po, nil

View File

@ -45,19 +45,13 @@ func gitRemoteLoaderEnabled() (bool, error) {
return false, nil
}
func NewGitRemoteLoader(offline bool) (loader.ResourceLoader, error) {
cache, err := cacheDir()
if err != nil {
return nil, fmt.Errorf("initializing remote resource cache: %w", err)
}
func NewGitRemoteLoader(offline bool) loader.ResourceLoader {
return gitRemoteLoader{
cache: cache,
offline: offline,
}, err
}
}
type gitRemoteLoader struct {
cache string
offline bool
}
@ -106,7 +100,12 @@ func (g gitRemoteLoader) Load(ctx context.Context, path string) (string, error)
ref.Commit = sha
}
local := filepath.Join(g.cache, ref.Commit)
cache, err := cacheDir()
if err != nil {
return "", fmt.Errorf("initializing remote resource cache: %w", err)
}
local := filepath.Join(cache, ref.Commit)
if _, err := os.Stat(local); os.IsNotExist(err) {
if g.offline {
return "", nil

View File

@ -46,21 +46,14 @@ func ociRemoteLoaderEnabled() (bool, error) {
return false, nil
}
func NewOCIRemoteLoader(dockerCli command.Cli, offline bool) (loader.ResourceLoader, error) {
cache, err := cacheDir()
if err != nil {
return nil, fmt.Errorf("initializing remote resource cache: %w", err)
}
func NewOCIRemoteLoader(dockerCli command.Cli, offline bool) loader.ResourceLoader {
return ociRemoteLoader{
cache: cache,
dockerCli: dockerCli,
offline: offline,
}, err
}
}
type ociRemoteLoader struct {
cache string
dockerCli command.Cli
offline bool
}
@ -100,7 +93,12 @@ func (g ociRemoteLoader) Load(ctx context.Context, path string) (string, error)
return "", err
}
local := filepath.Join(g.cache, descriptor.Digest.Hex())
cache, err := cacheDir()
if err != nil {
return "", fmt.Errorf("initializing remote resource cache: %w", err)
}
local := filepath.Join(cache, descriptor.Digest.Hex())
composeFile := filepath.Join(local, "compose.yaml")
if _, err = os.Stat(local); os.IsNotExist(err) {
var manifest v1.Manifest