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) { func (o *ProjectOptions) configureRemoteLoaders(dockerCli command.Cli, po []cli.ProjectOptionsFn) ([]cli.ProjectOptionsFn, error) {
git, err := remote.NewGitRemoteLoader(o.Offline) git := remote.NewGitRemoteLoader(o.Offline)
if err != nil { oci := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
return nil, err
}
oci, err := remote.NewOCIRemoteLoader(dockerCli, o.Offline)
if err != nil {
return nil, err
}
po = append(po, cli.WithResourceLoader(git), cli.WithResourceLoader(oci)) po = append(po, cli.WithResourceLoader(git), cli.WithResourceLoader(oci))
return po, nil return po, nil

View File

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

View File

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