Skip to content

Commit b847c7f

Browse files
committed
implement runtime file selection
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 5e3d8f6 commit b847c7f

File tree

7 files changed

+152
-5
lines changed

7 files changed

+152
-5
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.21
55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.7
77
github.com/Microsoft/go-winio v0.6.1
8-
github.com/adrg/xdg v0.4.0
98
github.com/buger/goterm v1.0.4
109
github.com/compose-spec/compose-go v1.20.0
1110
github.com/containerd/console v1.0.3

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
6363
github.com/Shopify/logrus-bugsnag v0.0.0-20170309145241-6dbc35f2c30d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
6464
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d h1:UrqY+r/OJnIp5u0s1SbQ8dVfLCZJsnvazdBP5hS4iRs=
6565
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
66-
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
67-
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
6866
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
6967
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
7068
github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092 h1:aM1rlcoLz8y5B2r4tTLMiVTrMtpfY0O8EScKJxaSaEc=

internal/locker/pidfile.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package locker
1919
import (
2020
"fmt"
2121
"os"
22+
"path/filepath"
2223

23-
"github.com/adrg/xdg"
2424
"github.com/docker/docker/pkg/pidfile"
2525
)
2626

@@ -29,10 +29,11 @@ type Pidfile struct {
2929
}
3030

3131
func NewPidfile(projectName string) (*Pidfile, error) {
32-
path, err := xdg.RuntimeFile(fmt.Sprintf("docker-compose.%s.pid", projectName))
32+
run, err := runDir()
3333
if err != nil {
3434
return nil, err
3535
}
36+
path := filepath.Join(run, fmt.Sprintf("%s.pid", projectName))
3637
return &Pidfile{path: path}, nil
3738
}
3839

internal/locker/runtime.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 locker
18+
19+
import (
20+
"os"
21+
)
22+
23+
func runDir() (string, error) {
24+
run, ok := os.LookupEnv("XDG_RUNTIME_DIR")
25+
if ok {
26+
return run, nil
27+
}
28+
29+
path, err := osDependentRunDir()
30+
if err != nil {
31+
return "", err
32+
}
33+
err = os.MkdirAll(path, 0o700)
34+
return path, err
35+
}

internal/locker/runtime_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 locker
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
)
23+
24+
func osDependentRunDir() (string, error) {
25+
home, err := os.UserHomeDir()
26+
if err != nil {
27+
return "", err
28+
}
29+
return filepath.Join(home, "Library", "Application Support", "com.docker.compose"), nil
30+
}

internal/locker/runtime_unix.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 locker
20+
21+
import (
22+
"os"
23+
"path/filepath"
24+
"strconv"
25+
)
26+
27+
func osDependentRunDir() (string, error) {
28+
run := filepath.Join("run", "user", strconv.Itoa(os.Getuid()))
29+
if _, err := os.Stat(run); err == nil {
30+
return run
31+
}
32+
33+
// /run/user/$uid is set by pam_systemd, but might not be present, especially in containerized environments
34+
home, err := os.UserHomeDir()
35+
if err != nil {
36+
return "", err
37+
}
38+
return filepath.Join(home, ".docker", "docker-compose"), nil
39+
}

internal/locker/runtime_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 locker
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
23+
"golang.org/x/sys/windows"
24+
)
25+
26+
func osDependentRunDir() (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, "docker-compose"), nil
32+
}
33+
}
34+
35+
appData, ok := os.LookupEnv("LOCALAPPDATA")
36+
if ok {
37+
return filepath.Join(appData, "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", "docker-compose"), nil
45+
}

0 commit comments

Comments
 (0)