From b847c7f5a41ee69380ab40b080450a1066e1acc0 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 30 Oct 2023 09:16:48 +0100 Subject: [PATCH] implement runtime file selection Signed-off-by: Nicolas De Loof --- go.mod | 1 - go.sum | 2 -- internal/locker/pidfile.go | 5 ++-- internal/locker/runtime.go | 35 +++++++++++++++++++++++ internal/locker/runtime_darwin.go | 30 ++++++++++++++++++++ internal/locker/runtime_unix.go | 39 ++++++++++++++++++++++++++ internal/locker/runtime_windows.go | 45 ++++++++++++++++++++++++++++++ 7 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 internal/locker/runtime.go create mode 100644 internal/locker/runtime_darwin.go create mode 100644 internal/locker/runtime_unix.go create mode 100644 internal/locker/runtime_windows.go diff --git a/go.mod b/go.mod index 37099dfce..a07d56ab4 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c2122a662..5052371da 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/locker/pidfile.go b/internal/locker/pidfile.go index 9e1ec1e31..ea688fb66 100644 --- a/internal/locker/pidfile.go +++ b/internal/locker/pidfile.go @@ -19,8 +19,8 @@ package locker import ( "fmt" "os" + "path/filepath" - "github.com/adrg/xdg" "github.com/docker/docker/pkg/pidfile" ) @@ -29,10 +29,11 @@ type Pidfile struct { } func NewPidfile(projectName string) (*Pidfile, error) { - path, err := xdg.RuntimeFile(fmt.Sprintf("docker-compose.%s.pid", projectName)) + run, err := runDir() if err != nil { return nil, err } + path := filepath.Join(run, fmt.Sprintf("%s.pid", projectName)) return &Pidfile{path: path}, nil } diff --git a/internal/locker/runtime.go b/internal/locker/runtime.go new file mode 100644 index 000000000..e60db5cc1 --- /dev/null +++ b/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 + } + + path, err := osDependentRunDir() + if err != nil { + return "", err + } + err = os.MkdirAll(path, 0o700) + return path, err +} diff --git a/internal/locker/runtime_darwin.go b/internal/locker/runtime_darwin.go new file mode 100644 index 000000000..6a46fba88 --- /dev/null +++ b/internal/locker/runtime_darwin.go @@ -0,0 +1,30 @@ +/* + 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" +) + +func osDependentRunDir() (string, error) { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + return filepath.Join(home, "Library", "Application Support", "com.docker.compose"), nil +} diff --git a/internal/locker/runtime_unix.go b/internal/locker/runtime_unix.go new file mode 100644 index 000000000..f0a92e974 --- /dev/null +++ b/internal/locker/runtime_unix.go @@ -0,0 +1,39 @@ +//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" +) + +func osDependentRunDir() (string, error) { + run := filepath.Join("run", "user", strconv.Itoa(os.Getuid())) + if _, err := os.Stat(run); err == nil { + return run + } + + // /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 +} diff --git a/internal/locker/runtime_windows.go b/internal/locker/runtime_windows.go new file mode 100644 index 000000000..6db76518f --- /dev/null +++ b/internal/locker/runtime_windows.go @@ -0,0 +1,45 @@ +/* + 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" +) + +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 +}