Skip to content

Commit

Permalink
re-implement cache folder detection
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Oct 26, 2023
1 parent 39008c5 commit 4c290e7
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -46,6 +46,7 @@ require (
go.uber.org/goleak v1.2.1
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
golang.org/x/sync v0.4.0
golang.org/x/sys v0.11.0
google.golang.org/grpc v1.59.0
gotest.tools/v3 v3.5.1
)
Expand Down Expand Up @@ -154,7 +155,6 @@ require (
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
28 changes: 28 additions & 0 deletions pkg/remote/cache.go
@@ -0,0 +1,28 @@
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package remote

import "os"

func cacheDir() (string, error) {
path, err := osDependentCacheDir()
if err != nil {
return "", err
}
err = os.MkdirAll(path, 0o700)
return path, err
}
35 changes: 35 additions & 0 deletions pkg/remote/cache_darwin.go
@@ -0,0 +1,35 @@
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package remote

import (
"os"
"path/filepath"
)

func osDependentCacheDir() (string, error) {
cache, ok := os.LookupEnv("XDG_CACHE_HOME")
if ok {
return filepath.Join(cache, "docker-compose"), nil
}

home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, "Library", "Caches", "docker-compose"), nil
}
37 changes: 37 additions & 0 deletions pkg/remote/cache_unix.go
@@ -0,0 +1,37 @@
//go:build linux

/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package remote

import (
"os"
"path/filepath"
)

func osDependentCacheDir() (string, error) {
cache, ok := os.LookupEnv("XDG_CACHE_HOME")
if ok {
return filepath.Join(cache, "docker-compose"), nil
}

home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".cache", "docker-compose"), nil
}
50 changes: 50 additions & 0 deletions pkg/remote/cache_windows.go
@@ -0,0 +1,50 @@
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package remote

import (
"os"
"path/filepath"

"golang.org/x/sys/windows"
)

func osDependentCacheDir() (string, error) {
cache, ok := os.LookupEnv("XDG_CACHE_HOME")
if ok {
return filepath.Join(cache, "docker-compose"), nil
}

flags := []uint32{windows.KF_FLAG_DEFAULT, windows.KF_FLAG_DEFAULT_PATH}
for _, flag := range flags {
p, _ := windows.KnownFolderPath(windows.FOLDERID_LocalAppData, flag|windows.KF_FLAG_DONT_VERIFY)
if p != "" {
return filepath.Join(p, "cache", "docker-compose"), nil
}
}

appData, ok := os.LookupEnv("LOCALAPPDATA")
if ok {
return filepath.Join(cache, "docker-compose"), nil
}

home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, "AppData", "Local", "docker-compose"), nil
}
10 changes: 2 additions & 8 deletions pkg/remote/git.go
Expand Up @@ -25,7 +25,6 @@ import (
"regexp"
"strconv"

"github.com/adrg/xdg"
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
Expand All @@ -47,15 +46,10 @@ func gitRemoteLoaderEnabled() (bool, error) {
}

func NewGitRemoteLoader(offline bool) (loader.ResourceLoader, error) {
// xdg.CacheFile creates the parent directories for the target file path
// and returns the fully qualified path, so use "git" as a filename and
// then chop it off after, i.e. no ~/.cache/docker-compose/git file will
// ever be created
cache, err := xdg.CacheFile(filepath.Join("docker-compose", "git"))
cache, err := cacheDir()
if err != nil {
return nil, fmt.Errorf("initializing git cache: %w", err)
return nil, fmt.Errorf("initializing remote resource cache: %w", err)
}
cache = filepath.Dir(cache)
return gitRemoteLoader{
cache: cache,
offline: offline,
Expand Down
11 changes: 3 additions & 8 deletions pkg/remote/oci.go
Expand Up @@ -25,7 +25,6 @@ import (
"strconv"
"strings"

"github.com/adrg/xdg"
"github.com/compose-spec/compose-go/loader"
"github.com/distribution/reference"
"github.com/docker/buildx/store/storeutil"
Expand All @@ -48,15 +47,11 @@ func ociRemoteLoaderEnabled() (bool, error) {
}

func NewOCIRemoteLoader(dockerCli command.Cli, offline bool) (loader.ResourceLoader, error) {
// xdg.CacheFile creates the parent directories for the target file path
// and returns the fully qualified path, so use "git" as a filename and
// then chop it off after, i.e. no ~/.cache/docker-compose/git file will
// ever be created
cache, err := xdg.CacheFile(filepath.Join("docker-compose", "oci"))
cache, err := cacheDir()
if err != nil {
return nil, fmt.Errorf("initializing git cache: %w", err)
return nil, fmt.Errorf("initializing remote resource cache: %w", err)
}
cache = filepath.Dir(cache)

return ociRemoteLoader{
cache: cache,
dockerCli: dockerCli,
Expand Down

0 comments on commit 4c290e7

Please sign in to comment.