Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-implement cache folder detection #11130

Merged
merged 5 commits into from Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 5 additions & 16 deletions cmd/compose/compose.go
Expand Up @@ -204,11 +204,7 @@ func (o *ProjectOptions) toProjectName(dockerCli command.Cli) (string, error) {

func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po ...cli.ProjectOptionsFn) (*types.Project, error) {
if !o.Offline {
var err error
po, err = o.configureRemoteLoaders(dockerCli, po)
if err != nil {
return nil, err
}
po = o.configureRemoteLoaders(dockerCli, po)
}

options, err := o.toProjectOptions(po...)
Expand Down Expand Up @@ -255,19 +251,12 @@ func (o *ProjectOptions) ToProject(dockerCli command.Cli, services []string, po
return project, err
}

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

po = append(po, cli.WithResourceLoader(git), cli.WithResourceLoader(oci))
return po, nil
return po
}

func (o *ProjectOptions) toProjectOptions(po ...cli.ProjectOptionsFn) (*cli.ProjectOptions, error) {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/Microsoft/go-winio v0.6.1
github.com/adrg/xdg v0.4.0
github.com/buger/goterm v1.0.4
github.com/compose-spec/compose-go v1.20.0
github.com/containerd/console v1.0.3
Expand Down Expand Up @@ -46,6 +45,7 @@ require (
go.uber.org/goleak v1.3.0
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 +154,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
2 changes: 0 additions & 2 deletions go.sum
Expand Up @@ -63,8 +63,6 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
github.com/Shopify/logrus-bugsnag v0.0.0-20170309145241-6dbc35f2c30d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d h1:UrqY+r/OJnIp5u0s1SbQ8dVfLCZJsnvazdBP5hS4iRs=
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092 h1:aM1rlcoLz8y5B2r4tTLMiVTrMtpfY0O8EScKJxaSaEc=
Expand Down
5 changes: 3 additions & 2 deletions internal/locker/pidfile.go
Expand Up @@ -19,8 +19,8 @@
import (
"fmt"
"os"
"path/filepath"

"github.com/adrg/xdg"
"github.com/docker/docker/pkg/pidfile"
)

Expand All @@ -29,10 +29,11 @@
}

func NewPidfile(projectName string) (*Pidfile, error) {
path, err := xdg.RuntimeFile(fmt.Sprintf("docker-compose.%s.pid", projectName))
run, err := runDir()

Check warning on line 32 in internal/locker/pidfile.go

View check run for this annotation

Codecov / codecov/patch

internal/locker/pidfile.go#L32

Added line #L32 was not covered by tests
if err != nil {
return nil, err
}
path := filepath.Join(run, fmt.Sprintf("%s.pid", projectName))

Check warning on line 36 in internal/locker/pidfile.go

View check run for this annotation

Codecov / codecov/patch

internal/locker/pidfile.go#L36

Added line #L36 was not covered by tests
return &Pidfile{path: path}, nil
}

Expand Down
35 changes: 35 additions & 0 deletions internal/locker/runtime.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 locker

import (
"os"
)

func runDir() (string, error) {
run, ok := os.LookupEnv("XDG_RUNTIME_DIR")
if ok {
return run, nil
}

Check warning on line 27 in internal/locker/runtime.go

View check run for this annotation

Codecov / codecov/patch

internal/locker/runtime.go#L23-L27

Added lines #L23 - L27 were not covered by tests

path, err := osDependentRunDir()
if err != nil {
return "", err
}
err = os.MkdirAll(path, 0o700)
return path, err

Check warning on line 34 in internal/locker/runtime.go

View check run for this annotation

Codecov / codecov/patch

internal/locker/runtime.go#L29-L34

Added lines #L29 - L34 were not covered by tests
}
34 changes: 34 additions & 0 deletions internal/locker/runtime_darwin.go
@@ -0,0 +1,34 @@
/*
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 locker

import (
"os"
"path/filepath"
)

// Based on https://github.com/adrg/xdg
// Licensed under MIT License (MIT)
// Copyright (c) 2014 Adrian-George Bostan <adrg@epistack.com>

func osDependentRunDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, "Library", "Application Support", "com.docker.compose"), nil
}
43 changes: 43 additions & 0 deletions internal/locker/runtime_unix.go
@@ -0,0 +1,43 @@
//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 locker

import (
"os"
"path/filepath"
"strconv"
)

// Based on https://github.com/adrg/xdg
// Licensed under MIT License (MIT)
// Copyright (c) 2014 Adrian-George Bostan <adrg@epistack.com>

func osDependentRunDir() (string, error) {
run := filepath.Join("run", "user", strconv.Itoa(os.Getuid()))
if _, err := os.Stat(run); err == nil {
return run, nil
}

Check warning on line 35 in internal/locker/runtime_unix.go

View check run for this annotation

Codecov / codecov/patch

internal/locker/runtime_unix.go#L31-L35

Added lines #L31 - L35 were not covered by tests

// /run/user/$uid is set by pam_systemd, but might not be present, especially in containerized environments
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".docker", "docker-compose"), nil

Check warning on line 42 in internal/locker/runtime_unix.go

View check run for this annotation

Codecov / codecov/patch

internal/locker/runtime_unix.go#L38-L42

Added lines #L38 - L42 were not covered by tests
}
49 changes: 49 additions & 0 deletions internal/locker/runtime_windows.go
@@ -0,0 +1,49 @@
/*
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 locker

import (
"os"
"path/filepath"

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

// Based on https://github.com/adrg/xdg
// Licensed under MIT License (MIT)
// Copyright (c) 2014 Adrian-George Bostan <adrg@epistack.com>

func osDependentRunDir() (string, error) {
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, "docker-compose"), nil
}
}

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

home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, "AppData", "Local", "docker-compose"), nil
}
36 changes: 36 additions & 0 deletions pkg/remote/cache.go
@@ -0,0 +1,36 @@
/*
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 cacheDir() (string, error) {
cache, ok := os.LookupEnv("XDG_CACHE_HOME")
if ok {
return filepath.Join(cache, "docker-compose"), nil
}

Check warning on line 28 in pkg/remote/cache.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/cache.go#L24-L28

Added lines #L24 - L28 were not covered by tests

path, err := osDependentCacheDir()
if err != nil {
return "", err
}
err = os.MkdirAll(path, 0o700)
return path, err

Check warning on line 35 in pkg/remote/cache.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/cache.go#L30-L35

Added lines #L30 - L35 were not covered by tests
}
34 changes: 34 additions & 0 deletions pkg/remote/cache_darwin.go
@@ -0,0 +1,34 @@
/*
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"
)

// Based on https://github.com/adrg/xdg
// Licensed under MIT License (MIT)
// Copyright (c) 2014 Adrian-George Bostan <adrg@epistack.com>

func osDependentCacheDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, "Library", "Caches", "docker-compose"), nil
}
36 changes: 36 additions & 0 deletions pkg/remote/cache_unix.go
@@ -0,0 +1,36 @@
//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"
)

// Based on https://github.com/adrg/xdg
// Licensed under MIT License (MIT)
// Copyright (c) 2014 Adrian-George Bostan <adrg@epistack.com>

func osDependentCacheDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".cache", "docker-compose"), nil

Check warning on line 35 in pkg/remote/cache_unix.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/cache_unix.go#L30-L35

Added lines #L30 - L35 were not covered by tests
}