Skip to content

Commit 5e3d8f6

Browse files
committed
re-implement cache folder detection
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 6727908 commit 5e3d8f6

File tree

7 files changed

+149
-17
lines changed

7 files changed

+149
-17
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ require (
4646
go.uber.org/goleak v1.3.0
4747
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
4848
golang.org/x/sync v0.4.0
49+
golang.org/x/sys v0.11.0
4950
google.golang.org/grpc v1.59.0
5051
gotest.tools/v3 v3.5.1
5152
)
@@ -154,7 +155,6 @@ require (
154155
golang.org/x/mod v0.11.0 // indirect
155156
golang.org/x/net v0.14.0 // indirect
156157
golang.org/x/oauth2 v0.11.0 // indirect
157-
golang.org/x/sys v0.11.0 // indirect
158158
golang.org/x/term v0.11.0 // indirect
159159
golang.org/x/text v0.12.0 // indirect
160160
golang.org/x/time v0.3.0 // indirect

pkg/remote/cache.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package remote
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
)
23+
24+
func cacheDir() (string, error) {
25+
cache, ok := os.LookupEnv("XDG_CACHE_HOME")
26+
if ok {
27+
return filepath.Join(cache, "docker-compose"), nil
28+
}
29+
30+
path, err := osDependentCacheDir()
31+
if err != nil {
32+
return "", err
33+
}
34+
err = os.MkdirAll(path, 0o700)
35+
return path, err
36+
}

pkg/remote/cache_darwin.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package remote
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
)
23+
24+
func osDependentCacheDir() (string, error) {
25+
home, err := os.UserHomeDir()
26+
if err != nil {
27+
return "", err
28+
}
29+
return filepath.Join(home, "Library", "Caches", "docker-compose"), nil
30+
}

pkg/remote/cache_unix.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//go:build linux
2+
3+
/*
4+
Copyright 2020 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package remote
20+
21+
import (
22+
"os"
23+
"path/filepath"
24+
)
25+
26+
func osDependentCacheDir() (string, error) {
27+
home, err := os.UserHomeDir()
28+
if err != nil {
29+
return "", err
30+
}
31+
return filepath.Join(home, ".cache", "docker-compose"), nil
32+
}

pkg/remote/cache_windows.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package remote
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
23+
"golang.org/x/sys/windows"
24+
)
25+
26+
func osDependentCacheDir() (string, error) {
27+
flags := []uint32{windows.KF_FLAG_DEFAULT, windows.KF_FLAG_DEFAULT_PATH}
28+
for _, flag := range flags {
29+
p, _ := windows.KnownFolderPath(windows.FOLDERID_LocalAppData, flag|windows.KF_FLAG_DONT_VERIFY)
30+
if p != "" {
31+
return filepath.Join(p, "cache", "docker-compose"), nil
32+
}
33+
}
34+
35+
appData, ok := os.LookupEnv("LOCALAPPDATA")
36+
if ok {
37+
return filepath.Join(appData, "cache", "docker-compose"), nil
38+
}
39+
40+
home, err := os.UserHomeDir()
41+
if err != nil {
42+
return "", err
43+
}
44+
return filepath.Join(home, "AppData", "Local", "cache", "docker-compose"), nil
45+
}

pkg/remote/git.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"regexp"
2626
"strconv"
2727

28-
"github.com/adrg/xdg"
2928
"github.com/compose-spec/compose-go/cli"
3029
"github.com/compose-spec/compose-go/loader"
3130
"github.com/compose-spec/compose-go/types"
@@ -47,15 +46,10 @@ func gitRemoteLoaderEnabled() (bool, error) {
4746
}
4847

4948
func NewGitRemoteLoader(offline bool) (loader.ResourceLoader, error) {
50-
// xdg.CacheFile creates the parent directories for the target file path
51-
// and returns the fully qualified path, so use "git" as a filename and
52-
// then chop it off after, i.e. no ~/.cache/docker-compose/git file will
53-
// ever be created
54-
cache, err := xdg.CacheFile(filepath.Join("docker-compose", "git"))
49+
cache, err := cacheDir()
5550
if err != nil {
56-
return nil, fmt.Errorf("initializing git cache: %w", err)
51+
return nil, fmt.Errorf("initializing remote resource cache: %w", err)
5752
}
58-
cache = filepath.Dir(cache)
5953
return gitRemoteLoader{
6054
cache: cache,
6155
offline: offline,

pkg/remote/oci.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"strconv"
2626
"strings"
2727

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

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

0 commit comments

Comments
 (0)